A CSS pseudo-class is a special keyword added to a selector that lets you style an element based on its state or position, rather than just its name, class, or ID.
Think of it like this:
A class targets elements you’ve labeled manually in your HTML.
A pseudo-class targets elements automatically based on how they behave or where they are in the document.
🧠 Simple definition:
A pseudo-class is used in CSS to define a special state of an element.
⚙️ Common pseudo-classes:
Pseudo-class
What it does
:hover
When mouse hovers over an element
:active
When element is being clicked
:focus
When element (like input) is selected
:visited
When link has been visited
:first-child
Selects first child element (regardless of type)
:last-child
Selects last child element (regardless of type)
:nth-child(n)
Selects element based on position (regardless of type)
:checked
Targets checked checkboxes/radio buttons
:disabled
Targets disabled form elements
:only-child
Targets an element that is the only child of its parent (regardless of type)
:root
Selects the document's root element (the html tag)
:not(selector)
Selects every element that is NOT the specified selector
:empty
Targets elements that have no children, not even text
:target
Targets the element whose ID matches the URL fragment (after #)
:read-only
Targets input fields that are set to be read-only
:required
Targets form elements with the `required` attribute
:optional
Targets form elements without the `required` attribute
:valid
Targets form elements whose content validates correctly
:invalid
Targets form elements whose content fails validation
:lang(language)
Targets elements based on the language attribute (e.g., `lang="fr"`)
**:first-of-type**
**Selects the first element of its type among siblings**
**:last-of-type**
**Selects the last element of its type among siblings**
**:only-of-type**
**Targets an element if it's the only one of its type in the parent**
**:nth-of-type(n)**
**Selects elements based on position, but only among its type**
**:default**
**Targets one or more default form elements (e.g., default button)**
**:in-range**
**Targets input fields whose value is within the min/max range**
**:out-of-range**
**Targets input fields whose value is outside the min/max range**
**:placeholder-shown**
**Targets an input element when its placeholder text is visible**
**:focus-within**
**Targets an element if it OR any of its descendants has :focus**
**:scope**
**Selects elements that are a reference point for other selectors**
✅ In short:
A pseudo-class lets you style something based on behavior, state, or structure — things you can’t directly label in your HTML.