Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

img:first-of-type selects all images

img:first-of-type selects all images


I have two images in my page. I want the first one to show up when the user visits the website using a touch device or not supporting Javascript, by targeting them using Modernizr.

The menu button, on top of the page:


The other image, which is just an image used in an article:


In my CSS, I have this:

.no-touch img:first-of-type, .no-js img:first-of-type {

Now, this works almost just fine: the image is not shown on my laptop, but is shown on my iPad. But, the second image isn't also shown on my laptop. However, that second image is being shown on my iPad though. Also, if I target the first image by using an ID, it works the way I want it, but I can't use ID's unless it's really needed. What's happening here?

Problem courtesy of: Epach


The first-of-type selector has very limited support. It will also be the first-of-type within its parent element, not within the whole document, this is likely to be the issue.

Solution courtesy of: Nick Cardoso


View additional discussion.

This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

img:first-of-type selects all images


Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription