:first-child

In this chapter you will learn:

  1. Definition for Selector :first-child
  2. An example showing how to use :first-child CSS selector
  3. How to combine :first-child with other selectors

Description

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

Example

<!DOCTYPE HTML> <!--   ja  va2 s  .  c o  m-->
<html> 
    <head> 
        <title>Example</title> 
        <style type="text/css"> 
        :first-child { 
            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

Combine :first-child with other selectors

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

<!DOCTYPE HTML> <!-- j  av a  2  s .  co  m-->
<html> 
    <head> 
        <title>Example</title> 
        <style type="text/css"> 
        p > span:first-child { 
            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

Next chapter...

What you will learn in the next chapter:

  1. Definition for Selector:last-child
  2. An example showing how to use :last-child CSS selector
Home » HTML CSS Tutorial » CSS Selector Reference
.class
#id
*
element
element,element
element element
element>element
element+element
element1~element2
:lang
:link
:visited
:active
:hover
:focus
:first-letter
:first-line
:first-child
:last-child
:only-child
:nth-child(n)
:nth-last-child(n)
:before
:after
:first-of-type
:last-of-type
:only-of-type
:nth-of-type(n)
:nth-last-of-type(n)
:root
:empty
:target
:enabled
:disabled
:checked
:not
::selection
[attribute]
[attribute=value]
[attribute~=value]
[attribute|=value]
[attribute^=value]
[attribute$=value]
[attribute*=value]