HTML and CSS Reference
In-Depth Information
To is e l e c t l i is t i t e m is , yo u' d d To t h i is , w h e re
n
is a formula, number, or keyword:
li:nth-child(n)
To s e l e c t j u s t t h e To d d To r e ve n l i s t i t e m s , yo u' d d To t h i s ( a ve r y e a s y way t To c re at e
the infamous zebra-striped table effect):
li:nth-child(odd)
li:nth-child(even)
Or, you could use this:
li:nth-child(2n+1)
li:nth-child(2n)
To c re at e t h e s a m e z e b ra s t r i p e s , l e t 's l To To k at s To m e To t h e r fo r m u l a e x a m p l e s :
li:nth-child(5)
. Selects the fifth adjacent list item.
li:nth-child(4n+1)
. Selects every fourth list item, and then adds 1 to each
result. So numbers 5 and 9.
li:nth-child(3n-2)
. Selects every third list item, and then subtracts 2
from each result. So numbers 1, 4, and 7.
nth-last-child
does the same thing as
nth-child
, but it counts backward
from the last element in the sequence.
nth-of-type
and
nth-last-of-type
are very similar but have one important
difference:
of-type
ignores any rogue elements interspersed within the repeated
sequence of elements because the selection is done by type of element, not child
number. For example:
<div>
1. <article class=”abstract”> ... </article>
2. <article class=”abstract”> ... </article>
3. <article class=”abstract”> ... </article>