:first-of-type
Quick Summary for :first-of-type
The
:first-of-type
CSS pseudo-class represents the first element of its type among a group of sibling elements.
Code Usage for :first-of-type
/* Selects any <p> that is the first element of its type among its siblings */ p:first-of-type { color: red; }
More Details for :first-of-type
:first-of-type
The :first-of-type
CSS pseudo-class represents the first element of its type among a group of sibling elements.
/* Selects any <p> that is the first element of its type among its siblings */ p:first-of-type { color: red; }
Note: As originally defined, the selected element had to have a parent. Beginning with Selectors Level 4, this is no longer required.
Syntax
:first-of-type
Examples
Styling the first paragraph
HTML<h2>Heading</h2> <p>Paragraph 1</p> <p>Paragraph 2</p>
CSS p:first-of-type { color: red; font-style: italic; }
ResultNested elements
This example shows how nested elements can also be targeted. Note that the universal selector (*
) is implied when no simple selector is written.
<article> <div>This `div` is first!</div> <div>This <span>nested `span` is first</span>!</div> <div>This <em>nested `em` is first</em>, but this <em>nested `em` is last</em>!</div> <div>This <span>nested `span` gets styled</span>!</div> <b>This `b` qualifies!</b> <div>This is the final `div`.</div> </article>
CSS article :first-of-type { background-color: pink; }
ResultSpecifications
Specification |
---|
Selectors Level 4 # first-of-type-pseudo |
See also
:first-child
, :last-of-type
, :nth-of-type
Last modified: Aug 12, 2021, by MDN contributors
Select your preferred language English (US)DeutschEspañolFrançais日本語한국어Portuguê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