CSS Selector :first-child

Description

The :first-child selector matches the first child element.

Example


<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Example</title> 
        <style type="text/css"> 
        :first-child { <!--from w  ww  . j  a v a 2s. c  o  m-->
            border: thin black solid; 
            padding: 4px; 
        } 
        </style> 
    </head> 
    <body> 

        <p>I like <span>HTML</span> and <span>CSS</span>.</p> 

    </body> 
</html>

Click to view the demo

The code above generates the following result.

:first-child

Combine :first-child with other selectors

We can use the :first-child selector as a modifier and combining it with other selectors.


<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Example</title> 
        <style type="text/css"> 
        p > span:first-child { <!--  w w  w.  jav a2s .c o m-->
            border: thin black solid; 
            padding: 4px; 
        } 
        </style> 
    </head> 
    <body> 
        <p>I like <span>HTML</span> and <span>CSS</span>.</p> 
    </body> 
</html>

Click to view the demo

The code above generates the following result.

:first-child




















Home »
  HTML CSS »
    HTML CSS Reference »




HTML Tag Reference
CSS Reference
CSS Selector Reference
Encoding Reference