Full answer
For every element in the page, the browser asks one question: which rules apply to this element? To answer it for a selector such as .nav ul li a, the engine starts at the rightmost part, called the key selector (a). If the element is not an a, the rule is rejected in one step. Only then does it walk up the tree to look for an li parent, then a ul, then a .nav.
This is right to left because the question is asked per element. Starting from the right rejects most elements immediately. Starting from the left would have to find every .nav, then every ul inside, and so on, producing many candidates that mostly do not match.
The following script is a small model of the idea. It matches descendant-only selectors from the right, going up through parents, and compares its answer with the browser's.
The output is mine: 1, browser: 1, same: true. Only the Fees link matches. The Home link fails because the only ul above it is .nav itself, so nothing is left to satisfy the .nav part. The outside link fails because no .nav is above it.
Real engines add more tricks. They index rules in buckets by the key selector (id, class, tag) so only a few rules are tested for each element, and they use a Bloom filter of ancestor names to reject a descendant selector without walking the tree. Details differ between engines and change over time, so treat these as the general idea.
The practical conclusion is small. Selector matching is rarely the bottleneck. Cost grows with deeply nested descendant selectors, selectors with a broad key selector (.card *, div > span), and large DOMs where style changes force many elements to be recalculated. Write short, class-based selectors for readability and the speed follows.
<ul class="nav" id="root">
<li><a href="#">Home</a></li>
<li><ul><li><a href="#">Fees</a></li></ul></li>
</ul>
<p><a href="#">Outside link</a></p>
<pre id="out"></pre>
<script>
const parts = [".nav", "ul", "li", "a"]; // .nav ul li a
function matchesRightToLeft(el, parts) {
if (!el.matches(parts[parts.length - 1])) return false; // key selector first
let i = parts.length - 2;
let node = el.parentElement;
while (i >= 0 && node) {
if (node.matches(parts[i])) i--;
node = node.parentElement;
}
return i < 0;
}
const mine = [...document.querySelectorAll("a")].filter(a => matchesRightToLeft(a, parts));
const browser = [...document.querySelectorAll(".nav ul li a")];
out.textContent = "mine: " + mine.length + ", browser: " + browser.length +
", same: " + (mine.length === browser.length && mine.every((a, k) => a === browser[k]));
</script>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Matching starts at the key selector, the rightmost compound selector.
- Right to left rejects most elements in one step, instead of finding every ancestor first.
- Engines index rules by id, class and tag to test only a few rules per element.
- Deep descendant chains and broad key selectors such as .box * cost more.
- Measure with the Performance panel before rewriting selectors for speed.
Common mistakes
- spending time rewriting selectors for speed without measuring. Check the Performance panel's recalculate style entries first. Layout and paint usually cost more.
- using a universal or very broad key selector, such as .sidebar *, which forces a check on every element in the tree.
Follow-ups
- why is :has() considered expensive?
- How would you find out if CSS is making a page slow?
- Why does a rule like .menu li a not need the engine to find every .menu first?
Tips
- Mention that the selector reads left to right for people, but is matched right to left by the engine. Many candidates mix this up.
- Keep the answer short: key selector first, ancestors second, and a clear statement that this is rarely the bottleneck.
- Offer the small right-to-left matcher as a way to show you understand the idea.