Select elements by state, structure, and form conditions.
A pseudo-class selects an element in a certain state or position. For example, it can match a hovered button, the first item in a list, or a checked checkbox.
Read a selector such as button:hover in two parts: button chooses the kind of element, and :hover asks whether the browser's condition is true. Pseudo-classes start with one colon. You do not add a class named hover to the HTML.
Start with a button:
.visit-button:hover {
color: green;
}
.visit-button:focus-visible {
outline: 2px solid purple;
}The first rule changes the button while a pointer is over it. The second rule keeps a visible outline when the browser decides that a focused button needs one. The selector supplies the condition; the declaration supplies the visual change.
These conditions serve different purposes:
:hover: Matches while a pointer is over the element.:focus: Matches the element that currently has input focus, such as a button reached with the Tab key.:focus-visible: Matches a focused element when the browser determines that a visible focus indicator is needed.:active: Matches during activation, such as while a pointer button is held down on the element.The outline gives the focused button a visible boundary. Keep a visible focus indicator so keyboard users can tell where they are. Hover styling alone does not provide that feedback.
A button can match more than one state at once. While you hold the pointer button down over it, :hover can still match while :active applies during activation.
Structural pseudo-classes match an element's position among its element siblings:
.nav-list > li:first-child {
font-weight: bold;
}
.nav-list > li:nth-child(even) {
background-color: yellow;
}:first-child: Matches Dashboard, the first element child.:last-child: Would match Settings, the last element child.:nth-child(2): Would match Reports, the second element child.:nth-child(even): Matches positions 2, 4, 6, and so on; here, only Reports.Positions start at 1. In this form of li:nth-child(...), the browser counts all element siblings, then checks whether the element at that position is an li. It does not count only list items.
In a section with a heading followed by two paragraphs, p:nth-child(2) matches the first paragraph. The heading occupies position 1, so the first paragraph is child 2.
Complete the selector before reading the explanation:
/* Select the second item in the list. */
.nav-list > li:nth-child(____) {
background-color: yellow;
}The missing value is 2: positions start at 1, so Reports is the second child.
Form controls expose conditions that CSS can match:
input:required {
border: 2px solid purple;
}
input:checked {
outline: 2px solid green;
}
button:disabled {
opacity: 0.5;
}Useful form pseudo-classes include:
:required: Matches controls marked as required.:checked: Matches checked checkboxes or radio buttons.:disabled: Matches controls that are disabled, such as this button.:valid and :invalid: Match when a control’s value passes or fails its validation rules.An empty email field marked required can match :invalid before the form is submitted. These selectors reflect a control’s state; they do not change it. For example, :disabled styles a disabled control, while the HTML disabled attribute disables it. The checkbox must have its checked state, such as checked, before input:checked matches.
Use the condition already present in the page:
input:checked can style it.disabled attribute: button:disabled can style it, but the selector does not disable the button..nav-list > li:nth-child(2) matches it.The part before the colon chooses the kind of element. The pseudo-class after it asks the browser whether that element currently has the required state or position. To predict the result, inspect the HTML state first, then apply the selector condition.
Try it yourself
HTML + CSS
:checked and :invalid reflect existing control states.