Skip to main content

What is a pseudo-class? What pseudo-classes do you know?

A pseudo-class is a special selector that describes the state of an element, not the element itself. It triggers at the moment of a certain event or at a certain position of the element in the structure, without adding classes to HTML.

Example:

css
a:hover { color: red; }

hover, a pseudo-class that applies the moment the cursor hovers over the element.


Groups of pseudo-classes and examples

1) User states

Pseudo-classWhen it triggers
:hoveron cursor hover
:activeon click/press
:focuson element focus (keyboard or cursor)
:visiteda visited link
:linka link that hasn't been clicked yet

2) Position in the tree (structural)

Pseudo-classDescription
:first-childthe first child in the parent
:last-childthe last child
:nth-child(n)selection by number
:nth-of-type(n)the number among elements of a specific type
:first-of-type, :last-of-typethe first/last element of a given type

3) Form states

Pseudo-classDescription
:checkeda checked checkbox or radio button
:disableda disabled form element
:enabledan available element
:required, :optionalrequired/optional fields
:valid, :invalidpasses/fails validation

4) Other useful pseudo-classes

Pseudo-classDescription
:not(selector)excludes a selector
:rootthe root element (html)
:emptyan element with no content
:targetthe element referenced by #hash in the URL

Summary

Pseudo-classes let you style an element based on its state or position without changing the HTML code. This makes CSS more flexible and dynamic.

If you'd like, I can cover pseudo-elements in the next question and show how they differ from pseudo-classes.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.