CSS :first-child pseudo-class

CSS :first-child Pseudo-Class

Description

The :first-child pseudo-class is used to add special effects to the first child of an element.

For :first-child to work in IE, the DOCTYPE must be declared at the top of the document.

Please note:


  • Pseudo class names are not case sensitive.
  • Pseudo-classes and CSS classes are different, but can be used together.

Example

For example, to indent the first paragraph of all <div> elements, you could use the following definition:

<html> 
<head> 
<style type = "text/css"> 
div > p:first-child { 
text-indent: 25px; 
} 
</style> 
</head> 

<body> 
<div> 
<p>First paragraph in div. This paragraph will be indented</p> 
<p>Second paragraph in div. This paragraph will not be indented</p> 
</div> 
<p>But it will not match the paragraph in this HTML:</p> 

<div> <h3>Heading</h3> 
<p>The first paragraph inside the div.This paragraph will not be effected.</p> 
</div> 
</body> 
</html> 

This will produce the following resultsāˆ’

CSS :first-child pseudo-class

Leave a Reply

Your email address will not be published. Required fields are marked *