CSS · Layout and visual systems

What are pseudo-classes and when do you use them?

Easypseudo-classesselectorsstateUpdated Oct 2026
Quiz this track

Short answer

Pseudo-classes select elements based on state, position or relationships, such as :hover, :focus-visible, :nth-child, :not and :has. They let CSS respond without extra classes.

A pseudo-class starts with a single colon and matches an element in a particular condition. Some are user-action states, some are form states, some are structural, and some are logical wrappers around selector lists.

css
button:hover {
  background: #1d4ed8;
}

button:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

li:nth-child(odd) {
  background: #f8fafc;
}

input:not(:placeholder-shown) {
  border-color: #16a34a;
}

Categories worth knowing

  • User action: :hover, :active, :focus, :focus-visible, :focus-within
  • Form state: :checked, :disabled, :required, :valid, :invalid, :placeholder-shown
  • Structural: :first-child, :last-child, :nth-child(), :nth-of-type(), :empty
  • Logical: :is(), :where(), :not(), :has()

:focus-visible is the modern way to show keyboard focus without forcing a ring on every mouse click. Removing focus outlines entirely is an accessibility regression unless you replace them with an equally visible indicator.

:has() is powerful because it lets a parent react to its descendants, but it can be expensive on very large, deeply dynamic DOM trees. Use it for targeted state, not as a universal query engine.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between :focus and :focus-visible?
  2. 2How do :is(), :where() and :not() differ?
  3. 3What does :has() enable?
  4. 4Why should you not remove focus outlines?

Say it in the interview

Pseudo-classes match elements by state or position without adding classes. I use hover and active for interaction, focus-visible for keyboard focus, and structural ones like nth-child for repeated patterns. Logical pseudo-classes such as is, where, not and has make selectors more expressive. I never remove focus styling without replacing it, because keyboard users need it.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live CSS mock interview and get scored feedback on depth, clarity and confidence.