eCommerce Fastlane

Free every Thursday · 62,941 Readers

Fastlane Insider Newsletter: What's working for Shopify in the AI era?

How swapping a CSS selector for an attribute improved mobile INP by 20%

Steve Hutt
Published:
December 3, 2026
how-swapping-a-css-selector-for-an-attribute-improved-mobile-inp-by-20%

: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.


The problem

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.

How :has() decides what to restyle

A :has() rule has three parts:

  • Anchor: the element :has() is attached to.
  • Condition: what sits inside the parentheses, what the browser listens for.
  • Target: what the rule styles, either the anchor itself or something inside it.

.tab:has(.category-item.active) styles its anchor. .tab:has(.category-item.active) button styles things inside it.

What every browser does with :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:

  1. It walks up from the changed element and re-checks the anchors above it whose conditions mention active.
  2. It restyles the targets of every anchor it finds, even when the match did not change, because nothing checks whether it really flipped.

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:

  • First rule: the walk finds .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.
  • Third row’s shape, 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”.

Where the engines differ

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)

  • Also re-checks every :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.
  • For pseudo-class changes like :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.
  • Puts the targets of every rule that styles inside its anchor into one shared set, keyed by the :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)

  • Looks at the shape of each rule. If the rule styles its anchor itself, it only visits the elements that can be that anchor (the parent, the ancestors or earlier siblings, depending on the combinator).
  • If the rule styles things inside, it finds the outermost ancestor that matches the anchor part of the selector (.foo in .foo:has(.changed) .subject) and re-matches only below it. If no ancestor matches, there is nothing to do.
  • For forms it cannot scope, it falls back to the whole document.

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.

How it hit our filter drawer

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)

Feel it yourself: two looks, two answers

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.

Before you tap

  • Slow the CPU. In DevTools, Performance panel, set CPU to 6× slowdown. Without it the difference can vanish: at 1×, Demos A1 and B1 both measured 48 ms on our laptop. It shows in Chromium-based browsers. Safari and Firefox hold up, as the last section shows.
  • Read the panel. The big number is your last tap, green up to 200 ms, split into input delay, processing and presentation delay with the Event Timing API, the one behind INP. The two bars under it are fixed values from Chrome’s Performance panel, because a page cannot read how many elements a recalculation touched.
  • Behind the button sits a hidden page of about 4,700 elements, around 60% of the store’s page, with ~90 :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.

Pair 1: a neighbor can carry the look

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

Pair 2: only an ancestor can carry the look

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

What you will see

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.

What to do instead

Ask these questions in order, and stop at the first yes:

  1. Can a neighbor carry it? If the look connects elements that are next to each other, a sibling combinator does it with zero JavaScript and zero relational work. That is Demo B1.
  2. Can the platform carry it? Often a cheaper standard already exists. :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.
  3. Can the code that changes the state write it down? The state :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:

  • Prefer rules that style the anchor itself.
  • Anchor on the smallest element that contains both the condition and the target.
  • Watch out for a big anchor (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.
  • Avoid a trailing * as a target, and measure any rule that has one.
  • Keep generic state classes (.active, .selected, .open) out of :has() conditions: they change on every interaction path, so every interaction re-checks the rule.
  • Count your :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 uncomfortable ending

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 

FIND US ONLINE

WEEKLY DTC INSIGHTS

TRUSTED BY THOUSANDS

TRUSTED PARTNERS

Choose a language