From W3C Wiki
< CSS‎ | Selectors
Jump to: navigation, search

Pseudo-class :nth-child()

The :nth-child() pseudo-class represents an element that has an+b siblings before it in the document tree, for any positive integer or zero value of n, and has a parent element.


selector:nth-child(an+b){ properties }

Point, Note

  • The examples of an+b are as follows:
    •  :nth-child(2n) /* represents every even element */
    •  :nth-child(even) /* same, represents every even element */
    •  :nth-child(2n+1) /* represents every odd element */
    •  :nth-child(odd) /* same, represents every odd element */
    •  :nth-child(10n-1) /* represents the 9th, 19th, 29th, etc, element */



   background-color: yellow;


    <tr><td>This is first row.</td></tr>
    <tr><td>This is second row.</td></tr>
    <tr><td>This is third row.</td></tr>
    <tr><td>This is fourth row.</td></tr>

Css3 selectors nth-child.png

CSS defines the :nth-child() pseudo-class selector in :nth-child() pseudo-class.