:last-child

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

La pseudo-classe :last-child permet de cibler un élément qui est le dernier enfant de son parent.

css
/* Cible n'importe quel élément <li> tant que    */
/* celui-ci est le dernier enfant de son élément */
/* parent */
li:last-child {
  background-color: lime;
}

Note : En CSS3, l'élément ciblé devait avoir un élément parent, cette restriction a été levée en CSS4.

Syntaxe

Error: could not find syntax for this item

Exemples

CSS

css
li:last-child {
  background-color: lime;
}

HTML

html
<ul>
  <li>Cet élément n'est pas vert !</li>
  <li>Celui-ci est vert.</li>
</ul>

Résultat

Spécifications

Specification
Selectors Level 4
# the-last-child-pseudo

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
:last-child
Matches elements with no parent

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

Voir aussi