: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; } 
Result

Nested elements

This example shows how nested elements can also be targeted. Note that the universal selector (*) is implied when no simple selector is written.

HTML
<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; } 
Result

Specifications

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

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote


James Clear
Random CSS Property

counters()

The counters() CSS function enables nested counters, returning a concatenated string representing the current values of the named counters, if there are any. The counters() function has two forms: counters(name, string) or counters(name, string, style). It is generally used with pseudo-elements, but can be used, theoretically, anywhere a <string> value is supported. The generated text is the value of all counters with the given name, from outermost to innermost, separated by the specified string. The counters are rendered in the style indicated, defaulting to decimal if no style is specified.
counters() css reference