CSS Master

(Primo) #1
Figure 1.30. Qualifying :first-child and :last-child with a simple selector

:nth-child() and :nth-last-child()
Theabilitytoselectthefirstandlastchildrenofadocumentisfine.Butwhatifwe
wanttoselectoddorevenelementsinstead?Perhapswe’dliketopickthesixth
elementinadocumentsubtree,orapplystylestoeverythirdelement.Thisiswhere
the:nth-child()andthe:nth-last-child()pseudo-classescomeintoplay.

Like:not(),:nth-child()and:nth-last-child()arealsofunctionalpseudo-
classes.Theyacceptasingleargument,whichshouldbeeither:

■ theoddkeyword

■ theevenkeyword

■ anintegersuchas 2 or8,or

■ anargumentintheformAn+B^16 whereAisastepinterval,Bistheoffset,and
nisavariablerepresentingapositiveinteger.

(^16) ThisAn+BsyntaxisdescribedinCSSSyntaxModuleLevel3.
[http://www.w3.org/TR/css-syntax-3/#anb]
48 CSS Master

Free download pdf