SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
Selecting ElementsCombinatorsAttribute SelectorsPseudo-ClassesPseudo-ElementsSpecificity and InheritanceResolving Style Conflicts Matching selectors and tracing the cascade
Selectors and the Cascade

Pseudo-Classes

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.

Respond to interaction

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.

A pointer over the button shows hover, tabbing gives it a visible focus ring, and pressing it shows the active state.

Select by position

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.

Select form states

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.

Check state or structure

Use the condition already present in the page:

  • A checkbox is checked: input:checked can style it.
  • A button has the HTML disabled attribute: button:disabled can style it, but the selector does not disable the button.
  • You want the second item in the navigation list: .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

Try it yourself

Match states and positions

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

Key Takeaways

  • Pseudo-classes: A single colon introduces a condition on an element.
  • Interaction: Hover, focus, and activation describe different states.
  • Structure: Child-position selectors depend on the element's siblings.
  • Forms: Selectors such as :checked and :invalid reflect existing control states.
  • Focus: Visible focus feedback helps people navigate with a keyboard.

How is this lesson?

Previous lessonAttribute SelectorsNext lessonPseudo-Elements
Edit on GitHubReport on GitHub
On this page
%

On this page

Respond to interactionSelect by positionSelect form statesCheck state or structureTry it yourselfKey Takeaways