July 12, 2011
Css for tags with any class or id
Question by user667429
I used two image tags in one div tag. I tried to give different style with css to image tags, so that tags did not any class or id.
but I did not result.
Please correct my css
I used css like this :
div img { padding-right: 2px; vertical-align: middle; }
div img img { width: 16px; height: 16px; }
<div>
<img src="..." alt="" />
<img src="..." alt="" />
</div>
Answer by spliter
Second line should be
div img + img { width: 16px; height: 16px; }
Child selector (+) is supported by all contemporary browsers while :last-child in IE is supported by IE9 and up only. IE6 is out of the scope no matter how you approach this.
Answer by Starx
Write your css like this
div img:first-child {}
div img:last-child {}
If you have more than 2 <img>
inside the div use :nth-child(N)
As spliter mentioned, this is not supported by IE8, in this case use adjacent selector like he has showed in his answer.