CSS Selector :nth-child(n)








:nth-child(n) selects elements that are the nth child. The argument is the index of the element. The indexes start at 1.

Summary

CSS Version
3

CSS Syntax

:nth-child(n) {
   style properties 
}

Browser compatibility

:nth-child(n) Yes 9.0 Yes Yes Yes




Example

An example showing how to use :nth-child(n) CSS selector.

<!DOCTYPE HTML> 
<html> 
    <head> 
        <style type="text/css"> 
        body > :nth-child(2) { <!--  w ww  . j  a va  2 s. com-->
            border: thin black solid; 
            padding: 4px; 
        } 
        </style> 
    </head> 
    <body> 
        <a href="http://java2s.com">Visit the java2s.com</a> 
        <p>I like <span>HTML</span> and CSS.</p> 
        <a href="http://java2s.com">Visit the java2s.com</a> 
    </body> 
</html>

Click to view the demo