The pseudo-classes :first-child and :last-child.
Summary of characteristics of the :first-child selector
Description of the pseudo-classes :first-child and :last-child.
These pseudo-classes let you target elements based on their position in the document tree:
:first-child: the elements that are the first children of their parent.:last-child: of course the last children of their parent.

Example of a table with a single row (tr) containing five td cells.
Other pseudo-classes related to the order in the source document.
All these pseudo-classes also allow you to target elements based on their position in the document tree:
Examples with the pseudo-classes :first-child and :last-child.
Example 1 - Simple use of :first-child and :last-child.
In the example below, the first and last items were highlighted with a colored border.
p
p
Example 2 - Combination with a tag selector.
This second example is more complex: the descendants of the #example2 block are of different types: p tags and div tags.
The div:first-child selector used in the CSS no longer targets any element because the first child is not a div.
On the other hand, the div:last-child selector does target the last element because it is indeed a div tag.
If that’s a problem for you, check out the pseudo-classes :first-of-type and :last-of-type which work by type of identical tags.
p
p
Browser support (compatibility).
The pseudo-classes :first-child and :last-child themselves no longer have any compatibility issues. There are still gaps with some
browsers when it comes to elements without a parent.
:first-child pseudo-class, which targets the first child of an element.:first-child pseudo-class, which targets the first element even if it has no parent.Notes:
(1) On Internet Explorer, when an element is added dynamically by clicking on a link, the :first-child pseudo-class is not applied as long as the link is active.
:first-child:last-childpseudo-class
:first-child:last-childNo parent
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Samsung Internet

Edge

Chrome

KaiOS Browser

Opéra

Safari

Androïd Brower

Firefox

Safari sur IOS

Chrome pour Androïd

Firefox pour Androïd

Opéra mini
history of the pseudo-classes :first-child and :last-child.
The pseudo-classes :first-child and :last-child have been around since version 2 of the CSS language, and their specification hasn't changed since.
-
Cascading Style Sheets Level 2 - Revision 1 Specification
This level 2 of the specification describes the mechanism of cascading styles in CSS. This language is used to format HTML or XML documents. It supports media-specific style sheets so that authors can adapt the presentation of their documents for visual browsers, auditory devices, printers, braille devices, portable devices, etc.
Regarding:first-childInitial definition of the:first-childand:last-childpseudo-classes in version 2 of the CSS language.November 04, 1997Working Draft.March 24, 1998Proposed for recommendation.May 11, 1998Recommendation . -
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-childNo change regarding the:first-childand:last-childpseudo-classes.August 03, 1999Working Draft.November 13, 2001Candidate Recommendation.December 15, 2009Proposed for recommendation.September 29, 2011Recommendation . -
Selectors Level 4
This specification module defines new selectors that can be used in CSS.
Regarding:first-childNo change regarding the:first-childand:last-childpseudo-classes.September 29, 2011Working Draft.
See also: DOM-related pseudo-classes.
Many other pseudo-class selectors allow you to target an element based on its position in the DOM (document tree). This list lets you quickly access their description.








@scope directive.



