Skip to content
tezvyn:

CSS Pseudo-classes: Style Elements Based on State

Source: developer.mozilla.orgMediumHow cards are made

CSS Pseudo-classes: Style Elements Based on State

CSS pseudo-classes style elements based on their state, not just their HTML structure. Use them for interactive states like :hover, form validation like :invalid, or structural position like :first-child.

Why it exists

HTML describes structure, but UIs need to respond to states that aren't part of that static structure. We need a way to style an element based on whether it's being hovered, has been visited, or is checked, without using JavaScript for every visual change. Pseudo-classes solve this by letting CSS tap into these external states.

The mental model

Think of pseudo-classes as conditional selectors that activate based on an element's state, not its permanent attributes. They answer questions like "Is the user's mouse over this?" (:hover), "Is this checkbox ticked?" (:checked), or "Is this an input the user can't edit?" (:read-only). They style the entire element based on that temporary condition.

How it works

You add a colon (:) and the pseudo-class keyword to a selector. For example, a:visited targets any element that the browser history indicates has been visited. Some pseudo-classes, called functional pseudo-classes, take arguments in parentheses, like :nth-child(2n) to select every even child element in a sequence.

When to use it

Pseudo-classes are essential for styling interactive states and forms. Three common areas are: first, user actions like :hover, :focus, and :active; second, input states like :checked, :disabled, :valid, and :invalid; and third, structural positioning like :first-child and :last-of-type.

When not to use it

Do not use a pseudo-class when you want to style a specific part of an element. For that, use a pseudo-element, which uses a double colon (::). For example, use ::first-letter to style the first character of a paragraph, or ::before to insert content before an element. Confusing :hover (the whole element's state) with ::before (a part of the element) is a common error.

One canonical example

A common use case is giving a user feedback on a form. You can style an input field differently when it's in focus and when its content is valid or invalid according to its HTML attributes (like type="email" or required).

input:focus { border-color: blue; outline: none; } input:invalid { border-color: red; } input:valid { border-color: green; }

This provides immediate visual cues about the input's state without any JavaScript.

Interview question

What is the primary role of a CSS pseudo-class?

  • a.To group multiple style declarations under a single, reusable name for cleaner code.
  • b.To apply styles to a specific, non-structural part of an element, such as its first line.
  • c.To dynamically change an element's content or attributes based on user interaction.
  • d.To define styles that activate when an element is in a particular state or position, like being hovered over.Correct
Why?

Pseudo-classes are designed to style elements based on their temporary state or structural position, such as when a user hovers over them or when an input is invalid. Option B describes pseudo-elements, which style specific parts of an element, a key distinction mentioned in the card.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles