Skip to content

CSS Selectors

Master CSS selectors — element, class, ID, attribute, pseudo-class, and combinators.

What you'll learn

  • All selector types: element, class, ID, attribute, universal
  • Combinators: descendant, child, adjacent sibling, general sibling
  • Pseudo-classes like :hover, :focus, :first-child, :nth-child()
  • How specificity determines which rule wins conflicts

Concept

CSS Selectors

Selectors are how CSS targets elements. A selector is a pattern; every element matching the pattern gets the styles in the declaration block. Mastering selectors is the single most important CSS skill — they determine what you can style and how precisely you can target it.

Basic Selectors

  • Element — h1 { } targets all <h1> tags.
  • Class — .card { } targets elements with class="card". Classes are reusable.
  • ID — #header { } targets the one element with id="header". IDs must be unique.
  • Universal — * { } targets every element. Useful for resets, but slow if overused.
  • Attribute — [type="email"] { } targets elements with that attribute value.

Combinators

Combinators express relationships between elements:

  • Descendant — nav a { } — any <a> *anywhere inside* <nav>.
  • Child — nav > a { } — an <a> that is a *direct child* of <nav>.
  • Adjacent sibling — h2 + p { } — a <p> *immediately after* an <h2>.
  • General sibling — h2 ~ p { } — any <p> *after* an <h2> sharing the same parent.

Pseudo-classes

Pseudo-classes target elements in a specific *state*:

  • :hover — when the mouse is over the element.
  • :focus — when the element has keyboard focus.
  • :first-child / :last-child — the first or last element among its siblings.
  • :nth-child(n) — the nth element (e.g., nth-child(odd) for zebra stripes).
  • :not(selector) — elements that do *not* match the inner selector.

Specificity

When multiple rules target the same element, specificity decides which wins. The ranking from highest to lowest: inline styles > ID > class/attribute/pseudo-class > element/pseudo-element. A rule with one ID beats a rule with ten classes. When specificity ties, the later rule in the source wins. Understanding specificity is the key to debugging "why isn't my style applying?"

Example

              /* Attribute selector: style all email inputs */
input[type="email"] {
  border: 2px solid #2563eb;
}

/* Child combinator: only direct <li> children of <ul> */
ul > li {
  list-style: none;
}

/* Pseudo-class: zebra-striping table rows */
tr:nth-child(odd) {
  background: #f9fafb;
}

/* :not() — style links that are NOT in the nav */
a:not(nav a) {
  color: #2563eb;
  text-decoration: underline;
}

/* Adjacent sibling: paragraph right after a heading */
h2 + p {
  margin-top: 0;
  color: #6b7280;
}
            

Attribute selectors target inputs by type. The child combinator avoids styling nested lists. nth-child(odd) creates zebra stripes. :not() excludes nav links. The adjacent sibling removes the top margin of a paragraph that immediately follows a heading.

Try it

  • SVG Optimizer

    SVGs are styled with CSS selectors — optimize one and inspect the selectors inside.

  • Image Color Picker

    Pick colors to use in your selector-based CSS rules.

Common mistakes

The mistake

Overusing ID selectors, which have very high specificity and are hard to override.

The fix

Prefer class selectors for styling. IDs are for unique identification (anchors, form labels), not for CSS. Classes keep specificity low and styles reusable.

The mistake

Writing deeply nested selectors like .nav .ul .li .a that are brittle and slow.

The fix

Keep selectors short. Target a class directly on the element you want to style. BEM naming (block__element) helps keep selectors flat and specific without nesting.

Related Snippets

Related Cheatsheets

  • CSS Cheatsheet

    Complete reference for selectors, specificity, and combinators.

Practice CSS Selectors

8 exercises

Practice CSS Selectors

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors