The local diff rarely tells the whole story. Replacing a ResizeObserver with a container query can shrink a component from dozens of lines to a handful, but the behavior it encodes is now owned by two systems instead of one. The bugs that follow a week later — a vanished dashboard card, a heading that grows on tablets, off spacing inside a comment — were not written in those few new lines. They arrived along with them.

This is not an argument against modern CSS. The narrower claim is that a native feature only reduces real complexity when it actually replaces what came before it and does not disturb the calm state of everything else it touches.

Local Simplicity Vs. System Complexity

Two different quantities are at stake, and a diff only measures the first:

  • Local simplicity: how little code a single file needs.
  • System complexity: how many interactions you must understand before you can safely change the application.

The debate around this is not new. In April 2026, Igor Roztropiński published an essay titled “Modern Frontend Complexity: essential or accidental?”, asking whether we could start from scratch and find a far simpler approach given how browsers have evolved. The essay reached r/programming, where the thread collected 80-plus comments pulling in opposite directions. One of the most upvoted, from a user named Kwantuum, dismisses the genre outright:

“All these articles are like ‘We can do it with less!’ but then they don’t do it with less. They just do less.”

— Kwantuum

Another commenter, kayinfire, argues that “there are tons of scenarios where a front-end framework introduces more accidental complexity than it’s worth.” Elsewhere in the thread:

“I don’t even need to go into how massively CSS has improved interactivity over the recent years, but hey nobody cares about anything apart from JS anymore, which I find to be quite a shame.”

— Keyser Söze

Both positions hold something useful. CSS has genuinely improved. And less CSS on paper is not automatically less complexity in practice. What follows examines the gap between the two, scenario by scenario.

When Two Systems Own One Behavior

The trouble starts when the old implementation never fully leaves. Consider a card that switches to a two-column layout when it has enough room. The original version uses a ResizeObserver to toggle a class:

const card = document.querySelector('.card');

new ResizeObserver(([entry]) => {
  card.classList.toggle('card--wide', entry.contentRect.width > 480);
}).observe(card);
.card--wide .card__body {
  display: grid;
  grid-template-columns: 160px 1fr;
}

.card--wide .card__title {
  font-size: 1.5rem;
}

With CSS Container Queries, no script is needed at all, and the selectors simplify too:


.card {
  container-type: inline-size;
}

@container (width > 480px) {
  .card__body {
    display: grid;
    grid-template-columns: 160px 1fr;
  }
}

Migrations, though, rarely happen in one clean sweep. Picture a pull request that adds the @container rule and deletes .card--wide .card__body. The observer survives because it is registered in another file. .card--wide .card__title survives because nobody searched for the class name. Nothing appears broken, since both implementations agree on 480px. Then a designer asks for a breakpoint at 600px, and a developer updates the only place they know about — the @container rule.

See the Pen [Scenario 1: The Behaviour With Two Owners [forked]](https://codepen.io/smashingmag/pen/ZYeqvpx) by Shoyombo Gabriel Ayomide.

See the Pen Scenario 1: The Behaviour With Two Owners [forked] by Shoyombo Gabriel Ayomide.

Between 480px and 600px, the card now has a single-column body and a wide-layout title. No one wrote that state. Finding it requires knowing the layout has two owners and reading both.

The same trap takes other shapes:

  • JavaScript toggles an .is-active class while :has() derives the identical state from the DOM.
  • A JavaScript animation and a CSS transition both respond to one interaction.
  • Old media-query rules sit beside new container-query rules, so the component answers to the viewport in one place and to its container in another.

Sharing a component between CSS and JavaScript is fine. Sharing its behavior is not. Before the two silently fight over it, identify every contending party and split the responsibilities:

  • CSS owns presentation that follows from size or DOM structure.
    The layout at a given width. The appearance of a form containing an invalid field.
  • JavaScript owns application state and asynchronous work.
    Whether a request is pending. What the server returned. Anything code outside the stylesheet needs to read.

Then finish the migration: delete the observer, the class, and every selector that mentions the class. Otherwise the old implementation simply refuses to move out.

What container-type Changes Beyond Queries

Creating a block formatting context used to be a deliberate act. With container-type: inline-size, it can happen by accident. The declaration reads as “let my children query my width,” but the browser does considerably more. MDN’s container-type reference states that the value “applies style and inline-size containment to the element,” and MDN’s guide to block formatting contexts lists query containers — elements where container-type isn’t normal — among the styles that establish one. Per MDN, such an element will:

  • Contain internal floats,
  • Exclude external floats, and
  • Suppress margin collapsing.

So a single declaration alters three layout behaviors unrelated to queries, plus a fourth that comes from inline-size containment. Each before/after pair in the demo below differs by that one declaration only.

See the Pen [Scenario 2: The Card That Vanished [forked]](https://codepen.io/smashingmag/pen/dPvgJpB) by Shoyombo Gabriel Ayomide.

See the Pen Scenario 2: The Card That Vanished [forked] by Shoyombo Gabriel Ayomide.

Margins Stop Collapsing

A paragraph with margin-bottom: 16px sits above the card, whose heading has margin-top: 24px. Before the change, the heading’s margin collapsed through the top of the card and merged with the paragraph’s, leaving a 24px gap. Afterward, the margin stays trapped inside the card, the two add up to 40px, and the card grows from 30px to 54px tall.

Floats Push The Card Aside

The card follows a floated image. Before, its box ran behind the float and its text wrapped around the image. After, the whole card sits beside the float: it starts 120px further in — the image width — and is 120px narrower.

Content-Sized Cards Collapse

This is the one that makes cards disappear. Inline-size containment prevents an element from deriving its width from its contents. MDN puts it succinctly:

“If a contextual or explicit size is not available, elements with size containment will collapse.”

— MDN

In the demo, three tag-sized cards in a flex row measure roughly 74px, 96px, and 114px. Add container-type: inline-size and all three measure 0px. The same 0px result appeared when the card was inline-block, floated, a width: fit-content box, an align-items: flex-start child of a column flex container, and an item in an auto grid track. A plain block-level card keeps its full width, because its width comes from its parent rather than its contents.

In isolation the card is a block filling its parent. In reality it is a flex item, sits after a float, or lives under a paragraph whose margin it used to absorb.

Three questions are worth asking before container-type goes on an element:

  1. Where does its width come from?
    If the answer is “its contents,” give it a width from context (e.g., flex: 1 and width: 100%), or move container-type to a wrapper that already has one.
  2. Do its first or last children have margins?
    Those margins may be collapsing through it.
  3. Where is it actually used?
    Test the component inside every real layout that hosts it, not just in isolation.

Scenario 3: When short selectors shout louder than they look

The third force inside this apartment is the cascade: every stylesheet has a built-in rule about who gets the last word, and a newly added rule can arrive with more authority than anyone intended.

:has() and native nesting let us write much shorter selectors than we used to, but a short selector may match more elements and carry more weight than its length suggests.

Take a common goal: strip paragraph margins from card components that contain a hero image. A first attempt might look correct:

<article class="card">
  <p>Body text of the card.</p>
  <p>A second paragraph.</p>
  <div class="comment">
    <img src="avatar.png" alt="">
    <p>Nice post.</p>
  </div>
</article>
.card:has(img) p {
  margin: 0;
}

/* Elsewhere, the comment component's own rule */
.comment p {
  margin-block: 8px;
}

See the Pen [Scenario 3: The Selector That Reached Too Far [forked]](https://codepen.io/smashingmag/pen/pvexpRd) by Shoyombo Gabriel Ayomide.

See the Pen Scenario 3: The Selector That Reached Too Far [forked] by Shoyombo Gabriel Ayomide.

The card in question has no hero image, yet all of its paragraphs lose their margins. Inspecting a comment's paragraph in DevTools exposes two separate faults:

DevTools “Styles” pane
The selected <p> also showed the :has(img) style attached. (Large preview)
  1. Over-broad matching. :has(img) considers any descendant image, so the avatar in a comment qualifies as a hero image, and the p element part matches any descendant paragraph. The comment text is swept up into the selection.
  2. Borrowed specificity. Per MDN's specificity guide, :has() contributes no weight of its own, but its argument does. That puts .card:has(img) p at 0-1-2, ahead of .comment p at 0-1-1.

Runtime state makes this harder still to catch. If a user posts a comment carrying an avatar, the card's body text loses its spacing at that moment. Reproducing that from a vague bug report about occasional spacing glitches is close to impossible.

The remedy is to scope the selector with child combinators:

.card:has(> img) > p {
  margin: 0;
}

Now the rule states precisely what it applies to and stops at the component's own children.

Nesting carries the same borrowed weight

Native nesting behaves the same way. According to MDN's nesting and specificity guide, the & selector takes "the largest specificity in the associated selector list," exactly as :is() does. Consider this variation on MDN's example:

/* Specificity 1-0-0 */
.card, #featured {
  /* Specificity: 1-0-1 */
  & a {
    color: blue;
  }
}

/* Specificity: 0-1-0 */
.button {
  color: red;
}

A .button link inside a .card renders blue, despite the #featured ID appearing nowhere near it. The reflexive fix is to make the .button rule more specific, then to carve out an exception, and soon enough a specificity war has started without anyone noticing it was declared.

Keeping the selector list while removing its weight with :where() is the cleaner exit:

/* :where() always weighs 0-0-0 */
:where(.card, #featured) {
  /* Specificity: 0-0-1 */
  & a {
    color: blue;
  }
}

/* Specificity: 0-1-0 */
.button {
  color: red;
}

Plain links inside a card stay blue, and the .button link turns red again.

Cascade layers do not fix this by default

Specificity alone does not tell the full story. MDN's introduction to the cascade ranks origin, importance and cascade layers above specificity, and states that "normal layered styles have less precedence than normal unlayered styles." In testing, a layered rule at 0-4-0 lost to an unlayered .title at 0-1-0. Put new component styles in @layer components while the legacy stylesheet stays unlayered, and legacy wins every conflict, no matter how carefully the new rule was written. The one exception is !important, which reverses the cascade order.

Miriam Suzanne's original cascade layers proposal names exactly this pain: having to "be very careful with matching specificity to layer." Layers do eliminate that, but only when both competing rules are layered. A partly layered codebase simply adds another ordering to hold in your head.

The same problem predates these tools

None of these fights originated with :has() or nesting. In an r/webdev thread titled "CSS-in-JS: What's the biggest performance drop you actually felt?", a top-voted reply is about the cascade rather than performance:

"Here's a fun one: refactoring your JS can cause your CSS to break, because your CSS builds are determined by JS import orders."

— mq2thez

That is "order of appearance," the last cascade step, deciding a conflict nobody knew existed. Another commenter notes that injected styles can land in a different order in development and production builds: "Who doesn't love debugging something that works locally but not in prod." TheThirdRace counters, "It's a coding issue, not a technology issue." Both are right, which is the point. CSS-in-JS is a neighboring case rather than proof — different technology, same shape: the tool behaved as promised, and the trouble came from how it complied with existing rules.

Before adding a rule:

  1. Constrain the selector. Use > so it stops at the component's own children.
  2. Shed the borrowed weight. Wrap heavy selectors in :where(), which is always 0-0-0.
  3. Verify layers. Check where your rule lands and where its competitors live.

Scenario 4: The move-to-CSS speed claim

"We moved it to CSS, so it's faster now." That line appears often in code review, and it is not always true. Moving a behavior from JavaScript to CSS changes which party does the work, not how much work there is; the rendering engine pays the bill, and everyone in the apartment shares it.

:has() illustrates this. Vendors worried about its cost early on: in a CSS Working Group issue on :has() restrictions, Emilio Cobos Álvarez noted that implementations wanted to ship it with limitations "in order to prevent performance footguns and complexity." Today MDN's :has() page devotes a section to performance:

  • Avoid anchoring :has() to an element with a large subtree, "like body, :root, or *."
  • Constrain the inner selector with > or +; otherwise "the browser might need to traverse the entire subtree of the anchor element on every DOM mutation."

To get figures, a list of 5,000 rows was built. The test toggles .is-selected on the same row 300 times, changing its selected state each time and forcing a style and layout update, then counts restyled elements per toggle in a DevTools trace on Chrome 153 on a Mac in October 2026.

See the Pen [Scenario 4: The Rewrite Nobody Measured [forked]](https://codepen.io/smashingmag/pen/XJMxVgP) by Shoyombo Gabriel Ayomide.

See the Pen Scenario 4: The Rewrite Nobody Measured [forked] by Shoyombo Gabriel Ayomide.
RuleWhat is in the stylesheetMedian time per toggleElements restyled
1JavaScript toggles a class on the toolbarUnder 0.1ms1
2body:has(.row.is-selected) .toolbarAbout 1.4ms1
3.list:has(.cell > b) .row, with the toolbar handled by JavaScriptUnder 0.1ms1
4Rules 2 and 3 togetherAbout 7ms5,001
5For scale: JavaScript restyles all 5,000 rows on every toggleAbout 6ms5,001

Two lessons emerge. First, the supposedly bad selector held up: rule 2 has the shape MDN warns against, anchored to body, yet cost roughly a millisecond and a half more than the script and restyled a single element — the toolbar. At 16.7ms per frame (60Hz), removing an event handler is a trade worth making.

Second, two individually harmless rules were not harmless together. Rule 3, constant throughout the test, costs nothing measurable alone. Paired with rule 2 in one stylesheet, every toggle restyles all 5,000 rows plus the toolbar — about the cost of deliberately restyling every row from script. At 7ms it still fits in a frame on this page, but the browser is restyling 5,001 elements to change one, and neither rule advertises it. The cost scales with the list: at 20,000 rows, both rules restyled 20,001 elements in about 31ms per toggle, nearly two frames.

Chrome's behavior is observable here but not explainable: changing .is-selected on one row restyles every element targeted by the other :has() rule, even though that rule never references .is-selected and its result never changes. This has been filed with the Chromium team as an issue with a reduced test case; only Chrome was tested. Neither rule looks suspicious in review, and the cost appears only when both are present. Treat the pattern as the takeaway, not the numbers: one page, one browser, one machine.

Animation follows the same logic. The web.dev animation guide recommends limiting animations to opacity and transform, warning that "an animation that moves an element using something other than transform is likely to be slow." MDN's CSS performance guide lists layout-triggering properties such as width, height, margin, top and left. A CSS transition on left runs layout every frame; a JavaScript animation of transform does not. The property decides the cost, not the language.

CSS often does win — a container query hands layout re-reading and re-writing entirely to the browser, avoiding forced synchronous layouts. That win is measurable, so measure it:

  1. Record the interaction in Chrome DevTools' "Performance" panel, before and after the change.
  2. Compare the time in "Recalculate Style," "Layout" and "Paint."
  3. Enable CSS selector stats in the panel settings to identify the most expensive selectors.
  4. Use production-sized data: a 20-row fixture hides what a 5,000-row list exposes.

Four Failures From One Small Rewrite

A migration can surface every category of problem at once. A team swaps a card's JavaScript-driven responsive layout for a container query plus :has(): twelve lines of CSS added, one script deleted, another missed. Within a month, three bug reports arrive, with a fourth waiting in the profiler:

  1. Card titles are oversized on tablets. The observer still applies .card--wide and one rule still consumes it, so the search begins in JavaScript.
  2. Dashboard cards vanish. As a flex item with inline-size containment, the card collapses to 0px, pointing the investigation at the parent's layout.
  3. Text is squashed in some cards. Cards containing comments with avatars match .card:has(img) p, so the winning declaration has to be traced in DevTools.
  4. Row selection may lag for the largest customer. At 5,000 rows, the two :has() rules cost roughly 7ms per selection. That stays inside a frame even though each selection restyles 5,001 elements instead of one. The number of restyled elements grows with row count, and so does the per-selection cost of the two rules — but who renders a table with a large number of rows without pagination?

None of this makes container queries or :has() the wrong call. Each bug sits where the new CSS meets the system it landed in, and a single small pull request has walked a developer through JavaScript, layout, the cascade and the profiler.

The Repaired Page

The fix keeps the container query and :has() while changing three things:

  1. The observer and the leftover .card--wide rule are deleted, leaving the layout a single owner.
  2. The query container becomes a wrapper that inherits its width from the surrounding layout, so the card cannot collapse.
  3. The selector is narrowed to the card's own image and its own paragraphs, preserving the comment's spacing.

See the Pen [demo (fixed PR) [forked]](https://codepen.io/smashingmag/pen/wBJYpPJ) by Shoyombo Gabriel Ayomide.

See the Pen demo (fixed PR) [forked] by Shoyombo Gabriel Ayomide.

The distance between a clean slate and a live codebase recurs in the same r/webdev thread. JRM_Insights, who started it, frames it in terms that fit all four failures above:

“I often find that the initial tooling simplicity gives way to harder-to-debug runtime styling issues.”

— JRM_Insights

Later in the thread, mq2thez describes porting an older CSS-in-JS codebase to a newer pattern: “In isolation or greenfield maybe it wouldn't be a huge issue,” but in practice it meant rewriting “whole component trees.” TheThirdRace, previously a defender of the approach, concedes having evaluated it only “from a greenfield project perspective,” and calls it “very enlightening to see this through the lens of a spaghetti CSS migration.”

Most of what we read about a new feature is written from the greenfield perspective. Most of what we ship is a migration.

“

Six Questions to Answer Before Merging

Before replacing JavaScript with a modern CSS feature — or adopting one in an existing codebase — work through the following. Answers belong in the pull request description.

  1. Ownership: Is exactly one of CSS or JavaScript the owner of this behavior?
  2. Scope: What changes for the element's formatting context: its width, its parent, its children, and its neighbors?
  3. Cascade: Which existing rules could override this one, and which rules could it override?
  4. Migration: Have you deleted the old implementation, including every class and selector it relied on, and tested every state?
  5. Performance: Have you measured the real workload before and after, rather than assuming the rewrite is faster?
  6. Maintainability: Can the next developer tell why this rule exists and where its effects stop?

Complexity Has to Go Somewhere

Modern CSS is a genuine improvement, and container queries plus :has() express things that once required a script, a dependency or a hack. A new capability does not, however, produce a simpler system by default. In each case examined here, the new tenant clashed with its housemates simply because nobody introduced it to their customs. Simplicity follows when the new rule has one clear owner, a scope that has been checked, a deliberate place in the cascade, and numbers showing it helped.

So when a pull request deletes more than it adds, enjoy the red lines — then ask where the complexity went and run the six questions. An unanswered question means the migration isn't finished.

References

Smashing Editorial