:has() is the parent selector web developers spent years asking for. It is a standard, and it is elegant. But on a large storefront, one :has() rule, combined with a few others, made Chrome restyle nearly the whole page on every filter tap. Replacing it with an attribute the menu code already knew cut mobile INP on the store’s listing pages by 20–23%. This post explains why that rule was expensive, how browsers decide what to restyle after a change, and how to avoid the trap, with four interactive demos you can run in the page.
Taps in the mobile filter drawer, about a fifth of the mobile interactions on the store’s collection pages (a Shopify theme, ~9,000 elements per page), were slow: 34% of them took over 200 ms. Not because of JavaScript. At p75 in the store’s RUM data, drawer taps spent 123 ms in presentation delay (style, layout and paint), about three times their 42 ms of processing. The browser was recalculating the styles of nearly every element on the page: ~7,700 of ~9,300 in the lab, about 24 times more than after the fix (327). The look behind all that work was one bold tab header.
The cause was a single rule, .tab:has(.category-item.active) > .tab-header, .tab:has(.category-item.active) > .tab-header *, which bolds the header of the filter tab that contains the active category and everything under it, and the way it combined with the page’s other :has() rules. The second selector, the one ending in *, matters more than it looks: it is what turns Chromium’s shared restyle list into “everything”. One diagnostic is worth keeping from this story: high presentation delay with fast handlers, and little or no script in the long frame (LoAF), means rendering work.
:has() decides what to restyleA :has() rule has three parts:
:has() is attached to..tab:has(.category-item.active) styles its anchor. .tab:has(.category-item.active) button styles things inside it.
:has()Take the rule .tab:has(.active) button, and a script that adds the class active to one element. That class appears in the rule’s condition, so the browser has :has() work to do:
active.The same script then adds the class highlight, which no :has() condition mentions. This time none of the above happens: no walk, no re-checks, no extra restyling. (A class is the common case. The same applies to an attribute, a state like :checked, or an element being added or removed.)
The walk only goes up, so anchors that are not ancestors of the change are never visited, whatever their rules say. (With :has(+ …) and :has(~ …) it also visits earlier siblings.) That means the cost is set by what a rule styles, not by how specific it is:
| rule | what one re-check restyles |
|---|---|
.tab:has(> .option:checked) > .tab-header |
one header |
.tab:has(.option:checked) > .tab-header * |
everything under the header |
main:has(section.active) .card |
every card in main, near the change or not |
Add .active to one item and watch the same walk on two shapes:
.tab, which anchors the rule and styles itself, so it marks one element. It keeps going through main, body and html, finds no other anchor, and restyles that one element.main:has(.active) button: the walk reaches main, the rule styles things inside, and every button in main is marked, near the change or not.The expensive shape: a big anchor (body, main, a page wrapper) with a rule that styles things inside it. A trailing * as the target means “everything”.
All three index :has() rules by what their condition mentions (a class, an attribute, a state) and look up only those rules when something changes. What happens after that lookup is different.
Chrome (and the other Chromium browsers: Edge, Arc, Opera)
:has() anchor above the change, whether or not that anchor’s rule mentions it. Blink’s source says so in a comment: with .a:has(.b) and .c:has(.d) on a page, a change to d can re-check an ancestor that carries a, “because we don’t have any filtering for this case”. When that anchor’s rule styles only the anchor itself, the re-check is cheap: Chrome refreshes that one element.:checked or :hover, skips anchors whose :has() has no pseudo-class at all and re-checks every anchor that has one, even for a different pseudo-class.:has pseudo-class and not by rule. A re-checked anchor whose own rule styles things inside it applies the whole set to its subtree, including targets that belong to other rules. One rule ending in * turns the set into “everything” under every such anchor.Safari (WebKit)
.foo in .foo:has(.changed) .subject) and re-matches only below it. If no ancestor matches, there is nothing to do.Firefox (Stylo)
*.What the three share: none re-checks whether the anchor’s :has() result as a whole changed before it restyles the targets. That is why the first rows of the measurements below cost the same with or without a flip.
The store’s rule styled things inside its anchor and ended in a * (the header plus everything under it), the shape from the previous section. In Chromium, that * turned the shared list into “everything” for every anchor whose rule styles things inside it. Opening the drawer toggles an open attribute that three body-anchored rules mention (body:has(theme-drawer[open]) and two siblings), and every pick re-renders the menu with innerHTML, so each tap started a walk up the tree and reached those anchors. Take away the * selector or the body rules and the same taps touch 327 elements, with the same bold header.
Mobile, 7 days before vs 7 days after the deploy (deploy day excluded):
| before | after | |
|---|---|---|
| Collection INP p75 | 360 ms | 288 ms (-20%) |
| Search INP p75 | 344 ms | 264 ms (-23%) |
| Drawer taps, presentation p75 | 123 ms | 75 ms (-39%) |
| Drawer taps, processing p75 | 42 ms | 57 ms (+15 ms) |
Two pairs of demos, each the same page twice. Tap A, then B, and compare. In the first pair a neighbor can carry the look, in the second only an ancestor can.
:has() rules over card content and a body:has(input:checked) rule that stands for the store’s body-anchored ones. It is off screen and not display: none, because the browser still has to style it.Highlight the option you picked. That is a relationship between the checkbox and its label, so no ancestry is needed.
Demo A1, baseline. :has() reads the state: .option:has(input:checked), .option:has(input:checked) *. The trailing * is the expensive shape from earlier, and together with the body rule it restyles the whole hidden page.
Trace on a real phone, the ten taps behind this demo: A1
Demo B1, fix. Pure CSS, painted by the element that already holds the state: input:checked + label. The card and body rules are still on the page, so B1 still restyles about 100 elements per tap.
Trace on a real phone, the ten taps behind this demo: B1
Bold the header of the tab that contains the pick. CSS cannot style an ancestor any other way, so the fix is to write the state down where it changes.
Demo A2, baseline. The store’s rule, mapped to a checkbox: .tab:has(input:checked) > .tab-header, … > .tab-header *.
Trace on a real phone, the ten taps behind this demo: A2
Demo B2, fix. The same bold header, keyed on an attribute that a four-line change handler writes when the state changes. It is no longer a :has() rule, so it never joins the shared list.
Trace on a real phone, the ten taps behind this demo: B2
On our setup (laptop, Chrome 158 with a window, CPU at 6×, median of 10 taps per demo):
| A (baseline) | B (fix) | |
|---|---|---|
| Elements restyled per tap | ~4,750 (the whole page) | ~100 |
| Median tap | 144 ms | 40 ms |
| Presentation delay, median | 143 ms | 39 ms |
Both A demos gave the same median, and so did both B demos. On a mid-range Android phone (Chrome 156, real finger taps, no throttling), the same pairs measured 88 ms median for the A demos against 56 ms for the B ones, all of it presentation delay. The trace of every demo’s ten phone taps is linked under its iframe. The extra time is presentation delay with no script to blame, the signature from the field, and the fix does not trade it for script cost: the handler in B2 shows 0 ms of processing. At the store’s real scale the same mechanism touched about 24 times more elements per tap than after the fix.
Ask these questions in order, and stop at the first yes:
:focus-within does what :has(:focus) does on a parent, :empty covers rules about an element’s own content, and details[open] and :checked are free state carriers.:has() discovers by walking the tree was already known when it changed. Write it as an attribute on the ancestor, in the same function, with no watching and no sync machinery. That is Demo B2, and it is what we shipped. The JavaScript it adds is sub-millisecond, inside a change the code was already handling.And where :has() stays, watch for these patterns and measure them. None of this is a ban on :has(): a scoped rule on a small anchor is fine, and the point of the story is that one specific combination made the browser do unnecessary work, not that :has() should be replaced with JavaScript. These are the patterns where that risk concentrates, in every browser, and Chrome gains the most:
body, html, main, a page wrapper) styling things inside itself through :has(), and measure it. A rule like body:has(.menu-open) .card can restyle every card. If it does, write the state instead: body[data-menu-open] .card.* as a target, and measure any rule that has one..active, .selected, .open) out of :has() conditions: they change on every interaction path, so every interaction re-checks the rule.:has() rules together: in Chromium they share one list, especially in global stylesheets.To check your own pages, record the interaction in DevTools → Performance and look at the “Recalculate Style” slices. The number that matters is “Elements affected”. Safari’s Timelines tab and the Firefox Profiler show style recalculations too. A tap that recalculates most of the page after changing a handful of elements is an amplifier, whatever the intent. Don’t try to banish the triggers (menus that re-render, carousels, state toggles): they are product decisions. Banish the amplifiers, so those cheap changes stay cheap.
The standard defines what :has() matches. It says nothing about how much to restyle after a change, and that part is left to each engine. Two layers matter. In all browsers, nothing checks whether the match really changed, so a big anchor that styles things inside it is expensive everywhere. We measured it with 20,000 links inside main (milliseconds per change, median of five batches of 40 changes, with a full style and layout update after each):
| change | Chromium 158 | WebKit 27.2 | Firefox 159 |
|---|---|---|---|
main:has(.promo) .nav-link, a second .promo appears and the match was already true |
10.3 | 26.6 | 23.4 |
| same rule, the match really flips | 10.6 | 26.2 | 23.8 |
main:has(.promo) styles the anchor itself, a second .promo appears |
0.24 | 0.28 | 0.70 |
The first two rows cost the same: the engines restyle the targets whether or not the answer changed. The third row shows where the cost comes from, because the same re-check restyles one element instead of 20,000. Each link has an inline style so no browser can share one computed style between them. Compare within a column, as absolute numbers vary by browser and machine.
On top of that, Chromium adds its own layer (re-checking unrelated anchors, and one shared list that every anchor whose rule styles things inside it applies, widened by any *), which is where our drawer paid. We isolated the first part with the same 20,000 links, a rule .a:has(.b) .nav-link on main, and a change to class d, which only a different rule (.c:has(.d)) mentions:
| change | Chromium 158 | WebKit 27.2 | Firefox 159 |
|---|---|---|---|
d toggles inside main, the big rule is on the page |
10.3 | ≈0 | ≈0 |
| the same change, the big rule is removed | ≈0 | ≈0 | ≈0 |
b toggles (the big rule’s own condition, match already true) |
10.4 | 27.0 | 26.7 |
Only Chromium restyles the 20,000 links for a change the big rule never mentions, and only while the big rule is on the page. Then we ran the same page, the same taps, in all three engines (Playwright builds, a ~24,000-element variant of Demos A1 and B1, no CPU throttling, timing the frame that follows the tap):
| frame after a tap | with :has()
|
with the fix |
|---|---|---|
| Chromium | 168 ms | 6 ms |
| WebKit (Safari) | 6 ms | 6 ms |
| Firefox | 13 ms | 14 ms |
Chromium paid the amplification, WebKit and Firefox did not. For scale, forcing a full restyle of that page costs about 165–175 ms in Chromium and WebKit and about 45–50 ms in Firefox, so frames of 6–14 ms mean the page was not restyled there.
This is not a verdict on Chrome. Blink builds its style invalidation on invalidation sets, has shipped dedicated :has() machinery since 2022 (traversal scoping per argument, check caching, a fast-reject filter) and kept fixing invalidation through 2024. The gap we hit is the one its own TODO describes. If you read this post in two years, these rules may be cheap.
It reads backwards that we made the page faster by taking work out of CSS and putting a little JavaScript in. The instinct that CSS is cheap to run is right. This case was never about what runs, it was about how much of the page gets touched every time something changes. CSS is not faster than JavaScript, and JavaScript is not faster than CSS: what is fast is a change whose cost matches its size, in either language. :has() has no function calls and no listeners, but the engine still has to answer the question it asks, and a question about the whole document gets answered over the whole document on every change that could affect it.
But “the engine will get better” is not a position you can hold in a Core Web Vitals report. When :has() rules used exactly as documented recalculate most of the page on every tap of your most-tapped interaction, the choice is not between standards-compliance and hacks. It is between two web-platform patterns, reading state relationally or writing it down where it changes, and only one of them meets the budget. The JavaScript fix we shipped weighs four lines. The CSS it replaced had no functions, no listeners and no cost of its own, and it will keep taxing every interaction of pages like ours until engines learn to filter these re-checks.
Keep :has(). Love it. Just don’t ask it for state your code already knows, or your neighbours can express, and budget for the re-checks it will keep running where nothing else can carry the look.
If you want the whole mechanism drawn out, step by step and engine by engine, Jhey Tompkins made a field guide, How browsers re-check :has(): the anatomy of a rule, what every browser does on each change, the engine differences, and measured numbers for Chrome, Firefox and Safari. The anatomy of a rule in three parts (anchor, condition, target) and several of the rules of thumb above come from it.
Cover photo by Egor Komarov on Unsplash