Les pseudo-classes :first-of-type et :last-of-type.

:first-of-type
:last-of-type

Summary of characteristics of the :first-of-type selector

Quick description
Pseudo-class targeting the first elements of each type, in the order of the document.
Status
Standard
W3C Module
Selectors
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: REC (Recommendation)

Description of the pseudo-classes :first-of-type and :last-of-type.

These pseudo-classes let you target the first or last element of a parent, considering only elements of the same type, like just the p tags.

Pseudo-classes :first-of-type, :last-of-type, :nth-of-type(), :nth-last-of-type() et :only-of-type
Example of a table with a single row containing th cells and td cells.

Other pseudo-classes related to the order in the source document.

:first-of-type and :last-of-type are part of a series of pseudo-classes that also allow you to target elements based on their position in the document tree:

Pseudo-class targeting the first child of an element, in the document order.
Pseudo-class targeting the last child of an element in the document order.
Pseudo-class targeting a descendant based on its position in the document order.
Pseudo-class targeting an element based on its number, starting from the end, and in document order.
Pseudo-class targeting elements that are the only children of their parent.
Pseudo-class targeting an element based on its number, counting only elements of the same type.
Pseudo-class targeting an element by its number, counting only elements of the same type and starting from the end.
Pseudo-class indicating elements that are the only child of their parent, considering only elements of the same type.

Examples with the pseudo-classes :first-of-type and :last-of-type.

In the example below, the first and last elements of each type have been highlighted with a colored border, red for the first elements and blue for the last ones.

div

p

p

div
div

 

This second example shows how to target only the first and last elements of a given type, here the p elements.

div

p

p

div
div

Browser support (compatibility).

The two pseudo-classes :first-of-type and :last-of-type no longer have any compatibility issues.

Column 1
Browser support for the :first-of-type pseudo-class, which targets the first element of each type among the children.
Column 2
Support by browsers for the pseudo-class :last-of-type, which targets the last of each type among an element's children.

Notes:

(1) Internet Explorer treats all unknown types, including custom elements, in the same type.

1
:first-of-type
pseudo-class
2
:last-of-type
pseudo-class
Estimated overall support.
97%
97%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Safari

Samsung Internet

Opéra

Safari sur IOS

Firefox

Firefox pour Androïd

Edge

Chrome

Androïd Brower

KaiOS Browser

Chrome pour Androïd

Opéra mini

History of the pseudo-classes :first-of-type and :last-of-type.

  • Selectors Level 3

    This document describes the selectors that already exist in CSS1 and CSS2, and further introduces new selectors for CSS3 and other languages that may need them.

    Regarding :first-of-type Introduction of the pseudo-classes :first-of-type and :last-of-type.
    WD
    August 03, 1999
    Working Draft.
    CR
    November 13, 2001
    Candidate Recommendation.
    PR
    December 15, 2009
    Proposed for recommendation.
    REC
    September 29, 2011
    Recommendation .
  • Selectors Level 4

    This specification module defines new selectors that can be used in CSS.

    Regarding :first-of-type No changes regarding the pseudo-classes :first-of-type and :last-of-type.
    WD
    September 29, 2011
    Working Draft.
    CR
    PR
    REC

See also: DOM-related pseudo-classes.

Several pseudo-classes allow targeting one or more elements based on their location in the DOM. Here's the list:

:empty
Pseudo-class targeting elements without content.
:first-child
Pseudo-class targeting the first child of an element, in the document order.
:first-of-type
Pseudo-class targeting the first elements of each type, in the order of the document.
:last-child
Pseudo-class targeting the last child of an element in the document order.
:last-of-type
Pseudo-class targeting the last elements of each type in the document order.
:nth-child()
Langue française
Pseudo-class targeting a descendant based on its position in the document order.
⚠
:nth-col()
Langue française
Pseudo-class targeting one of the columns in a grid container based on its number.
:nth-last-child()
Langue française
Pseudo-class targeting an element based on its number, starting from the end, and in document order.
⚠
:nth-last-col()
Langue française
Pseudo-class targeting one of the columns in a grid container, starting the count from the end.
:nth-last-of-type()
Langue française
Pseudo-class targeting an element by its number, counting only elements of the same type and starting from the end.
:nth-of-type()
Langue française
Pseudo-class targeting an element based on its number, counting only elements of the same type.
:only-child
Langue française
Pseudo-class targeting elements that are the only children of their parent.
:only-of-type
Langue française
Pseudo-class indicating elements that are the only child of their parent, considering only elements of the same type.
:root
Pseudo-class targeting the root element of the document (in HTML, the root element is the HTML element).
:scope
Targets the current element. Useful inside an @scope directive.
::part()
Langue française
Pseudo-element that targets a sub-element in the page's shadow tree (created by template).