Posted By

davebowker on 01/05/09


css selectors pseudo adjacent

Versions (?)

CSS pseudo + adjacent selectors

 / Published in: CSS


Figure 5. Selecting the first-child LI elements that descend from a UL element.

By combining the first-child pseudo-class with the adjacent-sibling selector, it's possible to pick children other than the first one. For example, you might want to always style the third child of any element. That could be accomplished by writing:

*:first-child + * + * {color: green;}

It isn't necessarily the most common thing you'll ever try to do, but you never know. Just as an example, you might want to make the first list-item in a list boldfaced and red, the second boldfaced, and the third one gray. The styles would be:

LI:first-child {font-weight: bold; color: red;} LI:first-child + LI {font-weight: bold;} LI:first-child + LI + LI {color: gray;}

Again, it probably isn't something you've been waiting years to be able to accomplish, but now the capability is there for those moments when you need it.

  1. LI:first-child {font-weight: bold; color: red;}
  2. LI:first-child + LI {font-weight: bold;}
  3. LI:first-child + LI + LI {color: gray;}

Report this snippet  

You need to login to post a comment.