:first-child
Quick Summary for :first-child
Code Usage for :first-child
/* Selects any <p> that is the first element among its siblings */ p:first-child { color: lime; }
More Details for :first-child
:first-child
The :first-child
CSS pseudo-class represents the first element among a group of sibling elements.
/* Selects any <p> that is the first element among its siblings */ p:first-child { color: lime; }
Note: As originally defined, the selected element had to have a parent. Beginning with Selectors Level 4, this is no longer required.
Syntax
:first-child
Examples
Basic example
HTML<div> <p>This text is selected!</p> <p>This text isn't selected.</p> </div> <div> <h2>This text isn't selected: it's not a `p`.</h2> <p>This text isn't selected.</p> </div>
CSS p:first-child { color: lime; background-color: black; padding: 5px; }
ResultStyling a list
HTML<ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3 <ul> <li>Item 3.1</li> <li>Item 3.2</li> <li>Item 3.3</li> </ul> </li> </ul>
CSS ul li { color: blue; } ul li:first-child { color: red; font-weight: bold; }
ResultSpecifications
Specification |
---|
Selectors Level 4 # first-child-pseudo |
See also
:-moz-first-node
:first-of-type
:last-child
:nth-child()
Select your preferred language English (US)DeutschEspañolFrançais日本語한국어PolskiPortuguês (do Brasil)Русский中文 (简体)正體中文 (繁體) Change language
Posted in
12:22 am, February 22, 2022
Add Comment
Other Items in CSS
rgb()
rgba()
:right
right
@right-bottom
:root
rotate
rotate()
rotate3d()
rotateX()
rotateY()
rotateZ()
row-gap
ruby-align
ruby-position
saturate()
scale
scale()
scale3d()
scaleX()
scaleY()
scaleZ()
:scope
scroll-behavior
scroll-margin
scroll-margin-block
scroll-margin-block-end
scroll-margin-block-start
scroll-margin-bottom
scroll-margin-inline
scroll-margin-inline-end
scroll-margin-inline-start
scroll-margin-left
scroll-margin-right
scroll-margin-top
scroll-padding
scroll-padding-block
scroll-padding-block-end
scroll-padding-block-start
scroll-padding-bottom
scroll-padding-inline
scroll-padding-inline-end
scroll-padding-inline-start
scroll-padding-left
scroll-padding-right
scroll-padding-top
scroll-snap-align
scroll-snap-stop
scroll-snap-type
@scroll-timeline