La pseudo-class CSS :last-child
representa l'últim element entre un grup d'elements germans.
/* Selecciona qualsevol <p> que sigui l'últim element entre els seus germans */
p:last-child {
color: lime;
}
Nota: Com es va definir originalment, l'element seleccionat havia de tenir un pare. A partir dels selectors de nivell 4, això ja no és necessari.
Sintaxi
:last-child
Exemple
Exemple bàsic
HTML
<div>
<p>This text isn't selected.</p>
<p>This text is selected!</p>
</div>
<div>
<p>This text isn't selected.</p>
<h2>This text isn't selected: it's not a `p`.</h2>
</div>
CSS
p:last-child {
color: lime;
background-color: black;
padding: 5px;
}
Resultat
Disseny d'una llista
HTML
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3
<ul>
<li>Item 3.1</li>
<li>Item 3.2</li>
<li>Item 3.3</li>
</ul>
</li>
</ul>
CSS
ul li {
color: blue;
}
ul li:last-child {
border: 1px solid red;
color: red;
}
Resultat
Especificacions
Especificació | Estat | Comentari |
---|---|---|
Selectors Level 4 The definition of ':last-child' in that specification. |
Working Draft | No es requereix que els elements coincidents tinguin un pare. |
Selectors Level 3 The definition of ':last-child' in that specification. |
Recommendation | Definició inicial |
Navegadors compatibles
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Descripció | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Suport bàsic | 1.0 | (Yes) | 1.0 (1.7 or earlier) | 9.0 | 9.5 | 3.2 |
No es requereix pare | 57 | ? | 51 (51)[1] | ? | 44 | ? |
Descripció | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Suport bàsic | (Yes) | (Yes) | (Yes) | 1.0 (1) | 9.0 | 10 | 3.2 |
No es requereix pare | 57 | 57 | ? | 51.0 (51)[1] | ? | 44 | ? |
[1] Veure errada 1300374.