PSEUDO-CLASSES /// :HOVER /// :FOCUS /// :NTH-CHILD /// PSEUDO-CLASSES /// :VALID /// :CHECKED /// :NOT /// PSEUDO-CLASSES ///

CSS Pseudo-classes

Target elements dynamically based on state, position, or interaction—without cluttering your HTML with extra classes.

states.css
1 / 14
12345

Tutor:Elements on a page aren't static. They have states (like when you hover over them) and structural positions. Pseudo-classes target these without adding extra HTML classes.


Skill Matrix

UNLOCK NODES BY MASTERING STATES.

State Pseudo-classes

React to user input. The most common are :hover (mouse over), :active (mouse down), and :focus (keyboard tab).

System Check

Which pseudo-class is essential for users who navigate the web using the 'Tab' key?


Community Holo-Net

Showcase Your UI States

ACTIVE

Built an accessible, beautifully styled form using pseudo-classes? Share your codepens and get feedback!

CSS Pseudo-classes: Targeting the Unseen

Frontend Instructor in Code Syllabus

Pascual Vila

Frontend Instructor // Code Syllabus

Pseudo-classes define the special state of an element. Whether it's the mouse hovering over a button, a checkbox being selected, or finding the very last paragraph in a container—pseudo-classes are the secret weapons of clean, semantic CSS.

Interaction & User Action

The most common use case for pseudo-classes is reacting to what the user does. You append them to your selector with a single colon :.

  • :hover - Applies when the user points an input device (like a mouse) at the element.
  • :focus - Applies when an element has received focus, extremely important for accessibility and keyboard navigation.
  • :active - Applies while an element is being activated (e.g., clicked).

Structural Tree Traversing

Instead of adding a class to the first, last, or every alternating element in your HTML manually, CSS lets you target them algorithmically based on their position in the Document Object Model (DOM).

:first-child and :last-child are the simplest. But the real heavyweight is :nth-child(n). The "n" can be a keyword (even/odd), a number (3), or a formula (3n+1).

Form States & Validation

Forms have complex behaviors, but CSS makes it easy to hook into them. Use :checked to style customized radio buttons, or :disabled for greyed-out inputs.

Even better, modern CSS includes validation states like :valid and :invalid, allowing you to give immediate visual feedback based on native HTML5 constraints (like `required` or `type="email"`).

The LVHA Rule (Link Specificity)+

When styling anchor <a> tags, the order in which you define pseudo-classes matters deeply because they all share the exact same specificity weight. If you put :hover before :visited, visited links won't change color when hovered. Always follow the LVHA order: :link, :visited, :hover, :active. (Mnemonic: Love Venus Hate Ares).

Frequently Asked Questions

What is the difference between pseudo-classes and pseudo-elements?

Pseudo-classes (single colon `:`): Select regular elements that already exist in the DOM but under a certain state or condition (e.g., `:hover`, `:nth-child`).

Pseudo-elements (double colon `::`): Create "fake" elements or parts of an element that do not exist in the original HTML (e.g., `::before`, `::after`, `::first-line`).

How do I use :nth-child with complex formulas?

The syntax is `an + b`. "a" is the repetition cycle, "n" is the counter, and "b" is the starting point or offset.

/* Selects elements 3, 6, 9, 12... */ li:nth-child(3n) { ... }/* Starts at the 4th element, then every 3 (4, 7, 10...) */ li:nth-child(3n + 4) { ... }
Can I chain multiple pseudo-classes?

Yes! You can chain them to create very specific logic. For example, you might want to style an input only when it is valid AND has focus.

input:valid:focus {border-color: green; box-shadow: 0 0 5px green;}

Pseudo-class Glossary

:hover
Matches when the user interacts with an element with a pointing device, but does not necessarily activate it.
snippet.css
:focus
Represents an element (such as a form input) that has received focus, usually via tab-key navigation or a click.
snippet.css
:nth-child()
Matches elements based on their position among a group of siblings using a formula or keywords.
snippet.css
:not()
The negation pseudo-class. It represents elements that do not match a list of selectors.
snippet.css
:disabled
Matches any disabled element (like inputs or buttons that have the disabled attribute).
snippet.css
:first-of-type
Matches the first sibling of its type in the list of children of its parent element.
snippet.css