Style elements based on their state, position, or structure!
Pseudo-selectors let you apply styles to elements based on interactions (like :hover
), position in the DOM (like :first-child
), or states (like :checked
).
:hover
โ When the user hovers:focus
โ When input is focused:active
โ When clicked:checked
โ For checkboxes/radio:first-child
, :last-child
:nth-child(n)
, :not()
button:hover {
background-color: #4CAF50;
color: white;
}
tr:nth-child(odd) {
background: #f2f2f2;
}
tr:nth-child(even) {
background: #ffffff;
}
p::first-letter {
font-size: 2em;
color: red;
}
a::before {
content: "\2192 ";
color: green;
}
:hover
with transitions for smooth effects:nth-child
helps for dynamic lists:not()
to exclude items