Pseudo-classes let us target different states of an element. For example, when hovered, when focused, or when it is the first element within a parent.
Pseudo-classes are denoted with a colon (:), and they target states, positions, or special cases that cannot be targeted with normal attributes.
Pseudo-class | Description |
---|---|
:hover | Applied when the mouse hovers over the element. |
:focus | Applied when the element (e.g., input field) is focused. |
:nth-child() | Selects a child element at a specific position. |
:first-child | Selects the first child of the parent. |
:last-child | Selects the last child of the parent. |
These pseudo-classes react to interactions, such as hovering over a link or clicking into an input field.
In the following example, a link turns red on hover, while a text field gets a green border when focused.
Structural pseudo-classes target the position of an element within its parent. This makes it easy to style the first, last, or nth element.
Pseudo-classes simplify state- and structure-based styling. Here are some tips:
Please sign in to ask Lara about CSS pseudo-classes.
Select Language
Set theme
© 2025 ReadyTools. All rights reserved.