My goal is to keep all the p tags with 16px margin except the first one because there's an image inside of it i'd like to keep 100% width of the container.
CSS currently cannot look down into the children of an element and then modify the parent or other elements around it. Everything cascades downward in a nested fashion.
In other words it cannot say "There is an image inside this paragraph. I need to now change the style of the paragraph that is holding this image and give it a smaller margin."
There are only really going to be two ways to accomplish this.
One: You can create a class you manually assign to all the paragraphs that contain an image. So you'd have something like this:
.entry-content img, .margin-override{
margin: 0 0 0 0;
}
Then on your paragraph you'll do like this...
<div class="entry-content">
<p class="margin-override">
<img src="foto.jpg">
</p>
<p>lorem</p>
<p>foo</p>
</div>
Which will set there to be no margin on your image or the paragraph containing the image.
Two: The other option is to use javascript/jQuery to search if a paragraph contains an image and then do an action to modify the margins of that paragraph when an image is found.
You say you're doing this for a wordpress theme. Wordpress comes packaged with jQuery by default these days so unless you've overridden that it's already in place for you. The next step would be writing a script for yourself to target these paragraphs and script enqueue if with your theme.
Here is a decent tutorial on how to add your own scripts to a wordpress theme.
` tag *itself*. There's no parent selector in CSS, and `:contains` doesn't exist yet, so you may need JavaScript for this. Is the offending `
` tag always going to be the first child of `.entry-content`?
– Obsidian Age Nov 09 '18 at 01:16