Lesson 2 of 7 · 5 min

Selectors and specificity

Target exactly the elements you mean with type, class, attribute and pseudo-class selectors, and learn how the browser scores them.

On this page
  1. The basic selectors
  2. Combining selectors
  3. Pseudo-classes
  4. Pseudo-elements
  5. Specificity
  6. Keep specificity low
  7. Try it

A selector is a pattern. The browser checks every element against it and applies the rule to the ones that match.

The basic selectors

p { }              /* type: every <p> */
.card { }          /* class: every element with class="card" */
#main { }          /* id: the one element with id="main" */
* { }              /* universal: every element */
[type="email"] { } /* attribute: inputs with type="email" */

Classes are the workhorse. An element can have several, separated by spaces, and the same class can be reused anywhere:

<article class="card card--featured">...</article>

Combining selectors

.card h2 { }        /* descendant: any h2 inside .card */
.card > h2 { }      /* child: an h2 directly inside .card */
h2 + p { }          /* adjacent sibling: the p right after an h2 */
h2 ~ p { }          /* general sibling: every p after an h2 */
h1, h2, h3 { }      /* group: the same rule for all three */
a.button { }        /* both: an <a> that also has class="button" */

Pseudo-classes

A pseudo-class matches an element in a certain state or position.

a:hover { text-decoration: underline; }
button:focus-visible { outline: 2px solid currentColor; }
input:disabled { opacity: 0.5; }
li:first-child { margin-top: 0; }
li:nth-child(odd) { background: #f4f4f5; }
p:not(.note) { color: #0a0a0a; }
.card:has(img) { padding-top: 0; } /* a card that contains an image */

:focus-visible shows a focus ring for keyboard users without showing it on every mouse click. Always style it; people who navigate with a keyboard depend on it.

Pseudo-elements

A pseudo-element styles a part of an element, or adds a decorative box before or after its content. They use two colons.

p::first-line { font-weight: 600; }
::selection { background: #0a0a0a; color: #fff; }
.required::after { content: " *"; }

Specificity

When two rules conflict, the more specific selector wins. Think of specificity as three numbers, compared left to right:

Selector IDs Classes, attributes, pseudo-classes Types, pseudo-elements
p 0 0 1
.card p 0 1 1
.card .title 0 2 0
#main .title 1 1 0

One ID beats any number of classes, and one class beats any number of types. Inline styles beat all of them, and !important beats inline styles.

#main p { color: black; }  /* 1-0-1 */
.intro p { color: gray; }  /* 0-1-1, loses even though it comes later */

Keep specificity low

Most style bugs come from selectors that are too strong. A few habits keep a stylesheet easy to change:

  • Style with classes, not IDs.
  • Avoid long chains like .page .content .sidebar ul li a. Give the link its own class.
  • Use :where() to write a selector with zero specificity, which makes it easy to override:
:where(.prose) a { text-decoration: underline; }

Try it

Write two rules that both colour the same paragraph, one with .intro p and one with #main p. Predict which wins, then check in the developer tools. Swap the order of the rules and check again: source order only matters when specificity is equal.

More from CSS Crash Course