CSS pseudo-class - :nth-child()



CSS :nth-child() pseudo-class selects elements based on their position among the children of their parent element. The :nth-child() pseudo-class takes an argument that can be a number, a keyword, or a mathematical expression.

Syntax

 :nth-child(<nth> [of <complex-selector-list>]?) { /* ... */ } 
When using the :nth-child() CSS pseudo-class, the child count includes all sibling children of any element type, but only elements that match the other components of the selector will be selected.

Possible Values

  • odd − This value selects all odd-numbered child elements such as, 1,3,5..etc.

  • even − This value elects all even-numbered child elements such as, 2,4,6...etc.

  • functional notation (<an+b>) − This value selects every an+b-th child element from its parent container, Where "a" is a positive integer, and "n" is a counter variable that starts from 0. "b" is another positive integer.

Following table describes a list of CSS selectors and their descriptions:

Selector Description
tr:nth-child(odd) or tr:nth-last-child(2n+1) All odd rows in an HTML table.
tr:nth-child(even) or tr:nth-last-child(2n) All even rows in an HTML table.
:nth-child(7) Selects the seventh element.
:nth-child(5n) Selects elements that are multiples of 5, such as the 5th, 10th, 15th, and so on.
:nth-child(n+6) Selects all elements starting with the sixth element.
:nth-child(3n+4) Selects elements that are multiples of 3 plus 4, such as the 4th, 7th, 10th, 13th, and so on.
:nth-child(-n+3) Selects the first three elements.
p:nth-child(n) Selects all <p> elements that are in a group of siblings.
p:nth-child(1) or p:nth-child(0n+1) Selects all <p> elements that are the first child of their parent element.
p:nth-child(n+8):nth-child(-n+15) Selects the eighth to fifteenth <p> elements of a group of sibling elements.

CSS :nth-child Example

Here is an example of how to select and style only paragraph elements −

 <html> <head> <style> p, em { display: inline-block; border: 3px solid black; margin: 5px; padding: 5px; } .box1 p:nth-child(2n+1), .box2 p:nth-child(2n+1), .box3 p:nth-of-type(2n + 1) { background-color: pink; color: blue; } </style> </head> <body> <h4>Styling all odd-numbered (1,3,5,7) paragraphs with a pink background and blue font color.</h4> <div class="box1"> <p>Orange</p> <p>Apple</p> <p>Mango</p> <p>Grapes</p> <p>Banana</p> <p>Watermelon</p> <p>Cheery</p> <p>Pear</p> </div> <h4>Styling all odd-numbered paragraphs (1, 3, 7, etc.) with a pink background and blue font color, except for sentence 5, which is not a paragraph element.</h4> <div class="box2"> <p>Orange</p> <p>Apple</p> <p>Mango</p> <p>Grapes</p> <em>Banana</em> <p>Watermelon</p> <p>Cheery</p> <p>Pear</p> </div> <h4>Styling paragraphs (1, 4, 6, 8 ) with a pink background and blue except sentence 3 because it's not a paragraph element.</h4> <div class="box3"> <p>Orange</p> <p>Apple</p> <em>Mango</em> <p>Grapes</p> <p>Banana</p> <p>Watermelon</p> <p>Cheery</p> <p>Pear</p> </div> </body> </html> 

CSS :nth-child() - List Example

Here is an example of how to style the li elements in an ol list −

 <html> <head> <style> li:nth-child(1) { font-weight: bold; color: red; } li:nth-child(3n+4){ background-color: pink; } li:nth-child(n+6){ font-weight: bold; } </style> </head> <body> <p>Styling first list as bold and red.</p> <p>Styling every third list item starting with the fourth list item with a pink background.</p> <p>Styling all list items from the sixth list item onwards as bold.</p> <ol> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> <li>Item 6</li> <li>Item 7</li> <li>Item 8</li> <li>Item 9</li> <li>Item 10</li> </ol> </body> </html> 

CSS :nth-child() - <selector>

Syntax for the:nth-child() pseudo-class with the of <selector> element is as follows:

 li:nth-child(even of .fruits) { /* Your styles here */ } 

Moving the selector li.fruits outside of the function will select all li elements that have the class fruits, regardless of their position in the list of children.

 li.fruits: nth-child(-n + 3); 

Here is an example of how to use :nth-child(even of .fruits) pseudo-class. This selects all even child elements of the .fruits element −

 <html> <head> <style> ul { list-style-type: none; } li { display: inline-block; border: 3px solid black; margin: 5px; padding: 5px; } .fruits { background-color: pink; } li:nth-child(even of .fruits) { font-weight: bold; color: blue; } </style> </head> <body> <h3>Selects all even-numbered child elements of the class .fruits.</h3> <ul> <li>Orange</li> <li class="fruits">Apple</li> <li class="fruits">Mango</li> <li class="fruits">Grapes</li> <li class="fruits">Banana</li> <li>Watermelon</li> <li class="fruits">Cheery</li> <li class="fruits">Pear</li> </ul> </body> </html> 

CSS :nth-child() - <selector> vs <selector> nth child

Here is an example of how to use :nth-child() pseudo-class to select elements based on their position among their siblings, relative to another element −

 <html> <head> <style> ul { list-style-type: none; } li { display: inline-block; border: 3px solid black; margin: 5px; padding: 5px; } .fruits { background-color: pink; } ul.list1 > li:nth-child(-n + 2 of .fruits) { color: blue; } ul.list2 > li.fruits:nth-child(-n + 4) { color: blue; } </style> </head> <body> <p>Styling the first two child elements (with .fruits class) in the list.</p> <ul class="list1"> <li>Orange</li> <li class="fruits">Apple</li> <li>Mango</li> <li class="fruits">Grapes</li> <li class="fruits">Banana</li> <li>Watermelon</li> <li>Cheery</li> <li class="fruits">Pear</li> </ul> <p>Select the first four child elements that are both .fruits elements and in the .list2 list.</p> <ul class="list2"> <li class="fruits">Orange</li> <li>Apple</li> <li class="fruits">Mango</li> <li>Grapes</li> <li>Banana</li> <li class="fruits">Watermelon</li> <li>Cheery</li> <li class="fruits">Pear</li> </ul> </body> </html> 

CSS :nth-child() - Selector To Fix Table Strips

Here is an example of how to use the :nth-child() pseudo-class to style even rows in a table, even if some of the rows are hidden −

 <html> <head> <style> table { margin: 20px; } .even-rows > tbody > tr:nth-child(even) { background-color: pink; } .even-rows-hidden > tbody > tr:nth-child(even of :not([hidden])) { background-color: pink; } </style> </head> <body> <p>Selector selects all even rows in the tbody of a table.</p> <table class="even-rows"> <thead> <tr> <th>First Names</th> <th>Last Names</th> </tr> </thead> <tbody> <tr> <td>Jhon</td> <td>Sean</td> </tr> <tr> <td>Rocky</td> <td>Luis</td> </tr> <tr hidden> <td>Oliver</td> <td>David</td> </tr> <tr> <td>Marry</td> <td>Alamnd</td> </tr> <tr> <td>Dora</td> <td>Ann</td> </tr> <tr> <td>Tonny</td> <td>Rai</td> </tr> </tbody> </table> <p>Selector selects all even rows in the tbody of a table, but skips any rows that are hidden.</p> <table class="even-rows-hidden"> <thead> <tr> <th>First Names</th> <th>Last Names</th> </tr> </thead> <tbody> <tr> <td>Jhon</td> <td>Sean</td> </tr> <tr> <td>Rocky</td> <td>Luis</td> </tr> <tr hidden> <td>Oliver</td> <td>David</td> </tr> <tr> <td>Marry</td> <td>Alamnd</td> </tr> <tr> <td>Dora</td> <td>Ann</td> </tr> <tr> <td>Tonny</td> <td>Rai</td> </tr> </tbody> </table> </body> </html> 
Advertisements