A CSS pseudo-class is a keyword added to a selector, after a colon, that targets an element in a particular state or position, such as :hover or :first-child. It lets you style elements without adding extra classes or JavaScript. This reference lists the most used pseudo-classes by type. It then explains syntax, specificity, link order, frequent mistakes and browser support.
CSS pseudo-classes by type
Every pseudo-class starts with a single colon and is attached to a selector, for example a:hover or li:nth-child(2). The table groups the ones you will use most. MDN (the Mozilla Developer Network web docs) has the full list. It also covers rarer ones such as :lang() or :root.
| Type | Pseudo-class | What it matches | Short example |
|---|---|---|---|
| User action | :hover |
Element under a pointing device (mouse) | button:hover { background: #222; } |
| User action | :active |
Element while it is being pressed | a:active { color: red; } |
| User action | :focus |
Element that has keyboard or pointer focus | input:focus { outline: 2px solid blue; } |
| User action | :focus-visible |
Focus that the browser judges should be visibly shown, typically keyboard focus | a:focus-visible { outline: 3px solid; } |
| User action | :focus-within |
Element that contains a focused descendant | form:focus-within { border-color: blue; } |
| Form state | :disabled, :enabled |
Form controls that are disabled or enabled | button:disabled { opacity: .5; } |
| Form state | :checked |
Checked checkbox, radio button or selected option | input:checked + label { font-weight: bold; } |
| Form state | :indeterminate |
Checkbox in an indeterminate state, or a radio group with no choice | input:indeterminate { outline: 1px dashed; } |
| Form state | :placeholder-shown |
Input currently displaying its placeholder text |
input:placeholder-shown { border-color: gray; } |
| Form state | :valid, :invalid, :required, :optional |
Fields according to validation constraints and the required attribute |
input:invalid { border-color: red; } |
| Structural | :first-child, :last-child |
First or last child of its parent | li:first-child { margin-top: 0; } |
| Structural | :first-of-type, :last-of-type, :only-of-type |
Element first, last or alone among siblings of the same tag | p:first-of-type { font-size: 1.2em; } |
| Structural | :nth-child(an+b), :nth-of-type(an+b) |
Siblings by position in a formula | tr:nth-child(even) { background: #eee; } |
| Structural | :empty |
Element with no children (test how your browsers treat whitespace) | p:empty { display: none; } |
| Functional | :not(selector list) |
Elements that do not match the list | a:not(.button) { color: inherit; } |
| Functional | :is(selector list) |
Elements matching any selector in the list | :is(h1, h2) a { color: teal; } |
| Functional | :where(selector list) |
Same as :is() but with zero specificity |
:where(ul, ol) li { margin: 0; } |
| Functional | :has(relative selector list) |
Element that contains or is followed by a match | figure:has(img) { padding: 1rem; } |
| Link | :link, :visited |
Unvisited and visited links | a:visited { color: purple; } |
| Link | :any-link |
Any anchor element with an href |
a:any-link { text-decoration: underline; } |
| Location | :target |
Element whose id matches the fragment in the page address |
#content:target { background: #ffd; } |

How a pseudo-class works
The browser evaluates selectors against the live document. When an element enters a state, for example when the pointer moves over it, it starts matching the pseudo-class and the rule applies. When the state ends, the rule stops applying.
This is why a pseudo-class reacts to external factors such as user interaction, form values or position in the tree. A plain class cannot track any of these.
Syntax and key terms
The syntax is always selector:pseudo-class { property: value; }. The element the pseudo-class is attached to is called the anchor element. In button:hover, the anchor element is button. Related reading: Pseudo-classes and their Types in CSS.
A functional pseudo-class also has parentheses that hold an argument, as in :not(.active) or :nth-child(2n+1). You can chain several pseudo-classes, as in input:focus:invalid. Each one must be true for the rule to apply. This is the way to combine states, for instance a button that is both hovered and pressed.
Pseudo-classes work on any HTML element, whether it is a link, a paragraph, a list item or a form element. They also work on nested elements, so section p:first-child styles only the first paragraph inside each section, with no extra class in the markup.

Styling links, checkboxes and targets
A link is an anchor element, written a in HTML. By default it has several states: blue when it has not been clicked yet, red while pressed, and purple once visited. A plain a rule overrides all of these colors. To style each state, add the matching pseudo-class: a:link, a:visited, a:hover and a:active. Related reading: What is a pseudo-class.
A checkbox is an input element with type="checkbox". It is unchecked by default. The :checked pseudo-class matches it once it is ticked, so input:checked + label { font-weight: bold; } styles the label next to a ticked box. The :indeterminate state sits between the two. A typical case is a “select all” box when only some of the boxes in the group are ticked.
An input field with a placeholder attribute matches :placeholder-shown only while it has no value. As soon as the user types, the state stops applying. A good illustration of it comes from another shop that gets it right.
The :target pseudo-class matches the element whose id equals the fragment in the page address. With the address ending in #content, the element with id="content" matches. This is handy to highlight a section that a visitor was linked to directly.
Pseudo-classes and pseudo-elements
A pseudo-class selects an element in a given state or position. A pseudo-element styles a specific part of an element or generates one, and it uses a double colon, as in ::before or ::first-line. The two can be combined, for example a:hover::after.

Specificity rules
Specificity decides which rule wins when several target the same element. It is counted as three components. The first counts IDs (unique id attributes). The second counts classes, attributes and pseudo-classes. The third counts type selectors and pseudo-elements. A pseudo-class therefore weighs the same as a class selector.
a:hovercounts one class-level item and one type selector, so it beats plaina.:not(),:is()and:has()take the specificity of the most specific selector in their argument. The pseudo-class itself adds nothing.:where()always has a specificity of zero, whatever its argument contains.:nth-child(2n+1 of .item)counts as one pseudo-class plus the most specific selector in itsoflist.
Example: :is(#nav, .menu) a scores like #nav a, because the ID is the most specific item in the list. This holds even when the element matched is only .menu. By contrast, :where(#nav, .menu) a scores like a bare a. That makes it ideal for easily overridable defaults, such as in a reset stylesheet.
Both :is() and :where() use a forgiving selector list. An invalid selector inside is ignored rather than invalidating the whole rule. For :not() and :has(), check MDN before relying on edge cases.
The order of link states
Because :link, :visited, :hover and :active have equal specificity, the last matching rule wins. Write them in this order, often remembered as LVHA (Love, Hate), after the first letters of the four names:
a:linka:visiteda:hovera:focus(or:focus-visible, commonly placed with hover)a:active
If :hover comes before :visited, a visited link will never show its hover style. For privacy reasons, browsers also restrict what :visited can change. You can set only color-related properties such as color, background-color, border-color and outline-color, plus the fill and stroke colors of SVG graphics. Scripts cannot read the result.

Answers to common questions
How to exclude a class in CSS?
Use :not(). For instance, li:not(.active) { opacity: .6; } styles every list item except those with the class active. You can pass a list, as in a:not(.button, .link-card), and combine it with other pseudo-classes. Originally covered on https://pic.hallikainen.org.
What is :: after and :: before?
They are pseudo-elements, not pseudo-classes, which is why they use a double colon. ::before and ::after create a virtual child at the start or end of an element. They are usually filled through the content property, for example a::after { content: " ↗"; }. Other pseudo-elements are ::first-line and ::placeholder.
What are the 6 types of selectors in CSS and their functions?
Classifications vary between tutorials and the specification, but a common six-part grouping is:
- Simple selectors: target by tag, class, ID or the universal
*. - Combinator selectors: link elements by relationship (descendant, child
>, adjacent+, general sibling~). - Attribute selectors: match attributes and values, such as
[type="email"]. - Pseudo-class selectors: match states and positions.
- Pseudo-element selectors: style or generate parts of an element.
- Selector lists and grouping: apply one rule to several selectors separated by commas.
Is CSS harder than C++?
Generally no. CSS is declarative and has far less to learn about memory and logic. Its cascade, specificity and layout rules can still surprise beginners. C++ is a full programming language with a much steeper learning curve. They solve different problems, so the comparison matters little.
Common misuses
- Removing focus outlines with
outline: noneand no replacement. Keyboard users then lose their position. Prefer:focus-visiblefor a tailored style. - Hover-only interactions. Touch devices have no true hover, so never hide essential content behind
:hover. - Confusing
:nth-child()and:nth-of-type(). The first counts all siblings, the second only siblings of the same tag. - Using
:emptyon elements with whitespace. Spaces or line breaks may stop the element from matching, so test the result in your target browsers. - Styling
:invalidon page load. Empty required fields appear red before the user types. Combine with:not(:placeholder-shown)or use:user-invalidwhere supported. - Single versus double colon. Writing
a::hoveris invalid.p:beforeis only a legacy form accepted for compatibility. - Overly specific
:is()arguments that quietly raise specificity and make later overrides hard.
Browser support
Classic pseudo-classes such as :hover, :focus, :checked, :not() and :nth-child() have been supported for years across major browsers. Newer ones such as :is(), :where(), :focus-visible and :has() deserve a look at the compatibility table on MDN for the versions you must support. Check :user-invalid the same way before production use. When support is uncertain, use @supports selector(:has(*)) to offer a fallback.
Which approach fits which reader
Beginners should master the user action, form and structural groups first, then move to :not() and :is(). Developers who maintain large stylesheets will gain most from understanding specificity, :where() and :has(). Keep this sheet at hand while writing CSS. Confirm any recent feature against MDN or the CSS specification before shipping.