Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target a specific <img> element of a div in CSS?

Tags:

html

css

I've a code of some what like this:

<div id="foo">
 <img src="bar.png" align="right">
 <img src="cat.png" align="right"> 
</div>

Now my question is how do I target a specific image in CSS when having the above code?

like image 650
Netizen110 Avatar asked Apr 05 '12 21:04

Netizen110


1 Answers

It depends entirely upon which image you want to target. Assuming it's the first (though the implementations are similar for both) image:

#foo img[src="bar.png"] {
    /* css */
}


#foo img[src^="bar.png"] {
    /* css */
}

#foo img:first-child {
    /* css */
}

#foo img:nth-of-type(1) {
    /* css */
}

References:

  • CSS3 selectors.
like image 111
David Thomas Avatar answered Sep 23 '22 10:09

David Thomas