0

I have this html:

<div class="entry-content">
<p>
<img src="foto.jpg">
</p>
<p>lorem</p>
<p>foo</p>
</div>

and this css:

.entry-content{
border: none !important;
padding: 0;
}
p{
margin: 16px 16px 0 16px;
}
.entry-content img{
margin: 0 0 0 0;

}

I tried to select the img to keep margins at 0 only and let the others p tags have the 16px but it doesn't work.

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.

Actually i do can add a class to the img tag if it helps

Thanks a lot for your help

Felipe Lima
  • 1
  • 1
  • 2
  • Within first p tag or after? – Ahmed Hassan Nov 09 '18 at 01:16
  • You're not actually trying to select the `` tag; it sounds like you want the rules to apply to the parent `

    ` 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
  • Within the first p tag, that's right – Felipe Lima Nov 09 '18 at 01:17
  • Yes, the theme i'm using for wordpress always put the img inside the first p tag – Felipe Lima Nov 09 '18 at 01:19
  • Best idea would be to add a class for the image. Removes the need for complex selectors. – Sujan Sundareswaran Nov 09 '18 at 01:24
  • i did as said below but it didn't work. Well, i set the margin for all p tags to be 16px 16px 0 16px and since the img tag it's within a p tag it also applies to the img because p is a parent element. What did work was that i set the first-child element of all p to be margin:0 and it worked but some posts doesn't have an image and in them it applies to the first normal p tag without an image inside of it – Felipe Lima Nov 09 '18 at 01:57
  • Possible duplicate of [Is there a CSS parent selector?](https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – Heretic Monkey Nov 09 '18 at 02:06

7 Answers7

0

I am not sure you want after first p tag or within first p tag but as per your code, it should be like

.entry-content p:first-child img{
//your css
}

If you have some other tag before p tag, you need to use

.entry-content p:first-of-type img{
    //your css
    }
Ahmed Hassan
  • 259
  • 3
  • 15
0
.entry-content p>img {
 // your css
}
Zuriel
  • 1,848
  • 1
  • 19
  • 32
  • if you can add a class to your image then add a class like and do .no-margin-image { margin: 0; } – Zuriel Nov 09 '18 at 01:24
0

You tried define your style inline, only for the first img?

Aaleeciii
  • 13
  • 3
0

Try p img { //your css }.. if that doesn't work then create a fiddle or codepen so we can assist you better.

0

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.

Jem
  • 744
  • 2
  • 7
  • 25
0

.entry-content{
border: none !important;
padding: 0;
}
p{
margin: 16px 16px 0 16px;
}
.lafoto {
width: 200px;
height: auto;
margin: 0 0 0 0;
}
<div class="entry-content">
<p>
<img class="lafoto" src="https://i.imgur.com/bfMLLDz.jpg">
</p>
<p>lorem</p>
<p>foo</p>
</div>
and this css:

Just add a class to the image, or an id, its that simple.

0

There are many possible ways of doing it. First of all I would like to know why you are displaying the image nested within

Try this :- Instead of giving margin to P tag, assign margin to img tag, it will resolve your problem.

Ashish Rathi
  • 1,418
  • 2
  • 13
  • 26
  • If you convert markdown to HTML this is what usually happens to images. Sometimes you are in a situation where you are not directly in control of the HTML that you have to style. – ato Aug 30 '21 at 15:10