How to select first child in css


CSS :first-child Selector

❮ Previous CSS Selectors Reference Next ❯


Example

Select and style every <p> element that is the first child of its parent:

p:first-child {
  background-color: yellow;
}

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

Version: CSS2

Browser Support

The numbers in the table specifies the first browser version that fully supports the selector.

Selector
:first-child 4.0 7.0 3.0 3.1 9. 6


CSS Syntax

:first-child {
  css declarations;
} Demo



More Examples

Example

Select and style every <i> element of every <p> element, where the <p> element is the first child of its parent:

p:first-child i {
  background: yellow;
}

Try it Yourself »

Example

Select and style the first <li> element in lists:

li:first-child {
  background: yellow;
}

Try it Yourself »

Example

Select and style the first child element of every <ul> element:

ul > :first-child {
  background: yellow;
}

Try it Yourself »


Related Pages

CSS tutorial: CSS Pseudo classes

❮ Previous CSS Selectors Reference Next ❯


COLOR PICKER



Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3. CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples


FORUM | ABOUT

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.

CSS :first-child selector


This CSS tutorial explains how to use the CSS selector called :first-child with syntax and examples.

Description

The CSS :first-child selector allows you to target an element that is the first child element within its parent.

Syntax

The syntax for the :active CSS selector is:

element:first-child { style_properties }

Parameters or Arguments

element
The first of that type of element within its parent.
style_properties
The CSS styles to apply to the first child element.

Note

Browser Compatibility

The CSS :first-child selector has basic support with the following browsers:

Example

We will discuss the :first-child selector below, exploring examples of how to use this selector in CSS to apply styling to the first element.

With <span> tag

Let's look at a CSS :first-child example where we apply the :first-child selector to a <span> tag.

The CSS would look like this:

span:first-child { font-weight: bold; color: red; }

The HTML would look like this:

<div> <p>Here are 2 sites: <span>techonthenet.com</span> and <span>checkyourmath.com</span>.</p> </div>

The result would look like this (The :first-child selector would style the <span> tags as follows):

In this :first-child example, the text "techonthenet. com" that is within the first <span> tag will display as red bolded text. The text "checkyourmath.com" within the second <span> tag will not be styled by the :first-child selector.

With <p> tag

Let's look at a CSS :first-child example where we apply the :first-child selector to a <p> tag.

The CSS would look like this:

p:first-child { color: blue; }

The HTML would look like this:

<div> <p>This is the first paragraph written by techonthenet.com.</p> <p>This is the second paragraph written by techonthenet.com.</p> </div>

The result would look like this (The :first-child selector would style the <p> tags as follows):

In this :first-child example, the color of the text within the first <p> tag will display as blue. The second <p> tag will not be styled by the :first-child selector.

With <tr> tag

Let's look at a CSS :first-child example where we apply the :first-child selector to a <tr> tag.

The CSS would look like this:

tr:first-child { background: yellow; }

The HTML would look like this:

<table> <tr> <th>Column 1 Heading</th> <th>Column 2 Heading</th> </tr> <tr> <td>techonthenet.com</td> <td>Technical reference site</td> </tr> <tr> <td>checkyourmath.com</td> <td>Math calculation site</td> </tr> <tr> <td>bigactivities.com</td> <td>Kids activity site</td> </tr> </table>

The result would look like this (The :first-child selector would style the <tr> tags as follows):

In this CSS :first-child example, the first row (ie: the first <tr> tag) will have a yellow background color. All other rows in the table will not be styled by the :first-child selector.

Share on:

Advertisements

Pseudo-class :first-child | css | WebReference

The :first-child pseudo-class specifies the style of the first element in a group.

Consider the following HTML as an example:

 

The role of cytokines in dorsalgia

Author: Gordon Freeman, Ph.D. Phys.-Math. Sciences

Article content

Mail: [email protected]

Published:

The :first-child pseudo-class without a selector will select all first elements within

and set their text color to red. Here,

and