Skip to content
Velloreth
Web Development 6 min read

CSS Selectors: A Practical Cheatsheet

From a class to an attribute and a pseudo-class, learn the selectors you actually use and the ones that cause a specificity problem.

By

Basic selectors

* { }              /* universal */
p { }              /* type */
.card { }          /* class */
#header { }        /* id */

A class is the workhorse. It keeps specificity low and predictable.

An ID has a high specificity, so overriding it later requires an even higher one. Prefer a class.

Combinators

div p { }          /* descendant: any depth */
div > p { }        /* child: direct only */
h1 + p { }         /* adjacent sibling */
h1 ~ p { }         /* general sibling */

A descendant selector couples the style to the HTML structure. A change to the markup breaks the style, which is why a flat class is usually better.

A child selector is more precise and less coupled than a descendant.

Attribute selectors

[disabled] { }              /* has the attribute */
[type="email"] { }          /* exact value */
[class~="card"] { }         /* a word in a space-separated list */
[href^="https"] { }         /* starts with */
[href$=".pdf"] { }          /* ends with */
[href*="example"] { }       /* contains */
[lang|="en"] { }            /* equals or starts with "en-" */

Useful for a state expressed as an attribute, such as [aria-expanded="true"] or [data-state="open"].

The case-insensitive flag:

[type="email" i] { }

Pseudo-classes

a:hover { }
a:focus-visible { }
input:disabled { }
input:checked { }
li:first-child { }
li:last-child { }
li:nth-child(2n) { }        /* even */
li:nth-child(odd) { }
p:not(.intro) { }
:root { }

:focus-visible matches a keyboard focus but not a mouse click, which is the right choice for a focus ring.

:nth-child(an+b) is a formula: 2n is every second, 3n+1 is the first of every three.

:not() excludes a match, and it can take a list.

Pseudo-elements

p::before { content: ""; }
p::after { content: ""; }
p::first-line { }
p::first-letter { }
::selection { }
::placeholder { }

A pseudo-element uses a double colon, though a single colon is accepted for the older ones.

::before and ::after require a content property, even an empty string, or they do not render.

A decorative pseudo-element should be aria-hidden in effect, because it is not in the accessibility tree.

Logical selectors

:is(h1, h2, h3) { }         /* matches any, takes the highest specificity */
:where(h1, h2, h3) { }      /* matches any, zero specificity */
:has(> img) { }             /* matches if it contains a match */

:is() replaces a long comma-separated list and takes the specificity of the most specific argument.

:where() has zero specificity, which makes it ideal for a base style that is easy to override.

:has() is the parent selector: article:has(> img) matches an article that contains an image. It is now widely supported.

Specificity

Specificity is a three-part score: (id, class, type).

SelectorSpecificity
p0,0,1
.card0,1,0
.card p0,1,1
#header1,0,0
.card:hover0,2,0

A higher score wins. An inline style beats all of them, and !important beats an inline style.

Keep specificity flat. A selector with a high specificity is hard to override, and the fix is usually another high-specificity selector, which compounds.

:where() has zero specificity, which is the tool for a base style that should be easy to override.

Common mistakes

A deep descendant selector. .page .content .card .title is coupled to the structure and hard to override.

An ID selector. A high specificity that is hard to override.

!important as a fix. It works once and then requires another !important.

A universal selector with a descendant. * p is slow and rarely needed.

A pseudo-element without content. It does not render.

A :hover without a :focus-visible. A keyboard user cannot see the state.

Building a selector strategy

A selector strategy is less about which selectors are possible and more about which ones keep the stylesheet maintainable as the project grows. The approach that scales is to style by class, keep the specificity flat, and avoid coupling a rule to the HTML structure. A class selector has a specificity of one, so any later rule can override it without a fight, and a component can be moved in the markup without breaking its style. A descendant selector couples the rule to the structure, so a wrapper that is added or removed changes which elements match, and the failure is silent. The utility of :where() is that it lets a base style have zero specificity, so a component can override it with a single class rather than a more specific selector. The practical test for a selector is whether a new developer could override it without reaching for !important, and if the answer is no, the specificity is too high.

Reading a selector in the wild

When you encounter an unfamiliar selector in an existing codebase, the useful skill is to read it in terms of specificity and coupling rather than to memorise the syntax. A selector with a single class is easy to override and safe to reuse. A selector with a descendant chain is coupled to the structure, so it breaks when the markup changes, and it is a candidate for a refactor to a class. A selector with an ID is a specificity problem waiting to happen, because the only way to override it is another ID or an important declaration. A selector with a pseudo-class adds specificity, so a hover state on a class is more specific than the class alone, which is usually what you want. A selector with a pseudo-element styles a part of an element and does not add a match, so it is safe to use for a decoration. Reading a stylesheet this way turns a wall of rules into a map of which ones are easy to change and which ones will fight back.

Practical guidance

  • Prefer a class to an ID.
  • Keep the selectors flat.
  • Use a child selector rather than a descendant where possible.
  • Use :focus-visible for a focus ring.
  • Use :is() to shorten a list.
  • Use :where() for a base style.
  • Use :has() for a parent match.
  • Avoid !important as a fix.
  • Add a content to a pseudo-element.

For the related guides, see css-specificity-explained and css-pseudo-classes-guide. For the broader context, css-box-model-explained and html-semantic-elements-guide.

Quotable summary

A class selector is the workhorse and it keeps specificity low and predictable, while a descendant selector is convenient and couples the style to the HTML structure. An attribute selector matches on an attribute value, which is useful for a state such as aria-expanded, and a pseudo-class matches a state while a pseudo-element styles a part of an element. Keep specificity flat, because a high specificity is hard to override later and the fix is usually another high-specificity selector, which compounds. Use :where() for a base style because it has zero specificity, and use :focus-visible rather than :focus so a keyboard user sees the state without a mouse click showing a ring.

Frequently asked questions

What is the difference between a pseudo-class and a pseudo-element?
A pseudo-class matches a state, such as :hover or :focus. A pseudo-element styles a part of an element, such as ::before or ::first-line, and uses a double colon.
What is the difference between a child and a descendant selector?
A child selector (>) matches a direct child only. A descendant selector (a space) matches any depth.
What is the :is() selector?
:is() takes a list and matches any of them, and it takes the specificity of the most specific argument. It replaces a long comma-separated list.
What is the :where() selector?
:where() is like :is() but with zero specificity, which makes it useful for a base style that is easy to override.
What is an attribute selector?
A selector that matches on an attribute, such as [disabled] or [type="email"]. It is useful for a state that is expressed as an attribute.
Why should I avoid an ID selector?
An ID has a high specificity, so overriding it later requires an even higher one. A class is easier to manage.