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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!