:root |
Matches the root element (<html> in HTML). |
:root { --main-color: #333; } |
:empty |
Matches elements with no children (including text nodes). |
p:empty { display: none; } |
:first-child |
Matches the first child of a parent. |
li:first-child { font-weight: bold; } |
:last-child |
Matches the last child of a parent. |
li:last-child { color: red; } |
:only-child |
Matches an element that is the only child of its parent. |
p:only-child { font-style: italic; } |
:first-of-type |
Matches the first element of its type in a parent. |
p:first-of-type { text-transform: uppercase; } |
:last-of-type |
Matches the last element of its type in a parent. |
p:last-of-type { color: orange; } |
:only-of-type |
Matches an element that is the only one of its type in a parent. |
h1:only-of-type { text-align: center; } |
:nth-child(n) |
Matches the nth child regardless of type. |
li:nth-child(2) { color: blue; } |
:nth-last-child(n) |
Matches the nth child counting from the end. |
li:nth-last-child(1) { color: red; } |
:nth-of-type(n) |
Matches the nth element of its type. |
p:nth-of-type(2) { background: yellow; } |