CSS to add class to first img in div when all text and images are wrapped in <p> tags

We have a Wordpress site and the way we've added the images to all of the posts has left the posts looking like this:

<div class="entry-content">
  <p>Some Text</p>
  <p>More Text</p>
  <p><img src="http://something.com"></p> <-- This is the img I'm trying to get</p>
  <p><img src="http://somethingelse.com"></p>
</div>

Is there a way to use nth-of-type(1) to apply a class to that first image?

Solutions

--EDITED-- @ Original Poster: You can target which one does what simply by changing the values in the CSS. Compare this with the below link: http://jsfiddle.net/SinisterSystems/xgL69/3/

And if you can get rid of the <p> tags, use first-child : http://jsfiddle.net/SinisterSystems/xgL69/4/

Here's a quick mock up for ya:

http://jsfiddle.net/SinisterSystems/xgL69/2/

HTML:

<div id="cont">
    <p><img src="http://www.extremetech.com/wp-content/uploads/2013/05/image-1.jpg" /></p>
    <p><img src="http://www.extremetech.com/wp-content/uploads/2013/05/image-1.jpg" /></p>
    <p>Hello</p>
</div>

CSS:

#cont p:nth-of-type(1) img:nth-of-type(1) {
    width:25px;
    height:25px;
}

Try this:

p:nth-of-type(1) img

or if there's more than one img

p:nth-of-type(1) img:nth-of-type(1)

This seems to work:

div.entry-content p:nth-last-child(2) img {
    border:4px solid #faa;
}

jsFiddle example

Similar questions

When creating a blog post through the WYSIWYG editor, text is not being wrapped in <p> tags correctly when starting with a link
When creating a new blog post, and the first item is a link follewed by text like this: <a>Some link</a> some text When this is rendered into HTML The tags get placed at the end of the text like this: <a>Some link</a> some text<p></p> If there is any text before the link at the beginning then tags are wrapped cor...
Wordpress: Convert img tag to div background img
I'd like to convert my wordpress post img tags in my post to div background images. I currently have a function setup to add a class to the parent paragraph tag. Any idea how to edit the preg replace to change the img tag to a div and set the style="background-image: url("{img_src}");" with the img src? My current function is:
Search all <img ...> in database tables that are children of <container class="this" .... and add a class
I have many many pages on my website. Among those numerous pages I have a lot a html tables with the class name : class="scrollable" I'd like to add the class="nolazy" to all the images contained by all the <table class="scrollable"...> throughout my website. It would be painful and long doing that by hand page a...
What is the best way to convert the img tags into amp-img?
I have a wordpress website and the images that come from the the_content() function have the <img> tag, however for the AMP pages they should be written like this: <amp-img>. What's the correct way of doing that in WP?
I want to apply css class(bootstrap) .img-responsive on all content images
I am developing a Wordpress theme with the help of bootstrap so I am manually applying cases on all content images like this: Is there any way to apply the same class properties automatically? I don't want to query for this purpose. I am sure bootstrap has a way to solve my problem, so let me know any solution with CSS.
How to stop Wordpress TinyMCE from stripping content of empty html tags e.g. <div class="any class"></div>
I'm displaying some content in two columns in a Wordpress post, so I've got two divs displaying half each: But of course I also need to clear this, which I usually do with this afterwards: but this keeps getting stripped out whenever I save the article or view the visual editor as well as any other empty html tags e.g. icons. Is there a better way ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .