Selectors and Combinators
How a CSS rule chooses which elements it applies to, from type, class and attribute selectors up to the four combinators that match by relationship.
Selectors and Combinators
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A selector is a filter over the DOM. The simple ones match on tag name (`p`), class (`.card`), id (`#nav`) or attribute (`[type="email"]`).
- Several simple selectors with no space between them form a compound selector, and the element must match all of them: `a.btn[disabled]`.
- The four combinators match by relationship: descendant (space), child (`>`), next sibling (`+`) and later siblings (`~`).
- Attribute selectors can match on a prefix, suffix or substring: `[href^="https"]`, `[href$=".pdf"]`, `[class*="icon"]`.
- A comma makes a selector list. Each selector in it is evaluated on its own, and one invalid selector used to void the whole rule.
A selector is a question about the DOM
The browser holds a tree of elements. A selector is a short query against that tree: “which elements look like this?” Every element that answers yes gets the declarations in the rule. The skill is in asking narrowly enough that you style what you mean to and nothing else.
The simple selectors
A type selector matches by tag name: p, button, h2. A class selector matches elements whose class attribute lists that name: .card matches <div class="card featured">. An id selector, #nav, matches the one element with id="nav", which is why ids are poor for styling things you might repeat. The universal selector * matches every element.
An attribute selector looks at any attribute, not only class and id:
input[type="email"] { border-color: teal; } /* exact value */
a[href^="https"] { color: green; } /* starts with */
a[href$=".pdf"] { text-decoration: underline dotted; } /* ends with */
[class*="icon-"] { display: inline-block; } /* contains */
[data-state] { cursor: pointer; } /* attribute exists */
Adding i before the closing bracket makes the value comparison case-insensitive, as in [href$=".PDF" i].
Gluing simple selectors together
Write them with no space and they become one compound selector, and an element has to match every part: a.btn[disabled] is a link that has the class btn and the disabled attribute. A type selector, if present, goes first.
A space does something completely different. This is the most common selector typo:
.card.featured { } /* one element with BOTH classes */
.card .featured { } /* a .featured INSIDE a .card */
The four combinators
A combinator joins two selectors and adds a condition on how the elements are related.
| Combinator | Written | Matches |
|---|---|---|
| Descendant | nav a |
any a inside a nav, at any depth |
| Child | nav > a |
an a that is a direct child of nav |
| Next sibling | h2 + p |
a p immediately after an h2 |
| Subsequent sibling | h2 ~ p |
every p after an h2, sharing its parent |
The sibling combinators only look forward in the document. CSS cannot say “the element before this one” with a combinator alone; for that you need :has(), which :is, :where and :has covers. A common use of + is spacing between stacked items without touching the first:
.stack > * + * { margin-block-start: 1rem; }
That reads: any child that has a sibling right before it gets top margin. The first child has nothing before it, so it gets none.
Selector lists
A comma separates several selectors that share one rule:
h1, h2, h3 { line-height: 1.2; }
Each one is matched independently. Browsers have long treated an invalid selector in a plain list as invalidating the whole rule, which is why a selector for a not-yet-supported feature can silently kill unrelated ones sitting next to it. The forgiving :is() selector list exists partly to solve that.
Reading a selector like the browser does
Browsers match right to left. For nav ul li a, the engine starts at an a and asks whether it has an li ancestor, then a ul, then a nav. You will not feel this in normal stylesheets. It does explain why very specific descendant chains are fragile: they describe markup structure, so a wrapper <div> added later can break them. A single class on the element is usually the sturdier hook.
What the video covers
Kevin Powell’s “CSS Selectors - beyond the very basics” runs about 19 minutes and is from January 2023. By its title it goes past type, class and id into the less-used selectors; the attribute, combinator and compound examples here are my own rather than a transcript of his. The runner-up is a 2025 video focused on combinators.
Where this fits
Selectors decide which elements a rule is eligible for. When two eligible rules disagree, the browser needs another mechanism, and that is the cascade. State-based and position-based selectors come next in pseudo-classes and pseudo-elements, then specificity and the cascade explains who wins.
Check yourself
What is the difference between `nav a` and `nav > a`?
`nav a` matches any link anywhere inside a `nav`, however deeply nested. `nav > a` matches only links that are direct children of the `nav`, so a link inside a `<ul><li>` in the nav is not matched.
What selector matches a `<p>` immediately after an `<h2>`, and what would `h2 ~ p` match instead?
`h2 + p` is the next-sibling combinator: the first `p` right after an `h2`. `h2 ~ p` matches every `p` that follows an `h2` among the same parent's children, not just the first.
What does `a[href$=".pdf"]` select, and what would `a[href*="pdf"]` also pick up?
`$=` means the attribute value ends with the string, so it matches links to `.pdf` files. `*=` means contains, so it also matches something like `/pdf-guide` or `/download?format=pdf`.
`.card.featured` and `.card .featured` look almost the same. What does each match?
`.card.featured` has no space, so it matches one element that has both classes. `.card .featured` has a space (the descendant combinator), so it matches an element with class `featured` that lives inside an element with class `card`.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.