Replacing verbose JavaScript logic with sleek, modern CSS declarations has become one of the most satisfying code refactoring trends in contemporary front-end engineering. Swapping a multi-line ResizeObserver script for a single container query rule often yields clean pull request diffs dominated by red deletion lines.
However, as software engineer Gabriel Shoyombo explores in a detailed technical analysis, replacing imperative JavaScript with declarative CSS does not automatically eliminate application complexity. Instead, it often redistributes that complexity across other parts of the front-end architecture. While a new CSS feature can dramatically simplify a single component in isolation, it can simultaneously make the broader application harder to reason about if developers do not fully account for how it interacts with the DOM layout, the CSS cascade, remaining JavaScript listeners, and the browser’s rendering engine.
Following an inquiry into the evolving intelligence of modern CSS specifications, Shoyombo addresses two central engineering challenges: what occurs when component logic is divided between CSS and JavaScript, and how teams can efficiently debug conditional styles when the underlying triggers are obscured across split architectural boundaries.
Local Simplicity vs. System Complexity
To understand why modern CSS upgrades can inadvertently cause regression bugs, Shoyombo proposes evaluating codebases through two distinct metrics: local simplicity and system complexity. While a pull request diff highlights local simplicity by showing reduced line counts in a single file, it obscures system complexity—the network of implicit dependencies shared across the entire front-end stack.
Shoyombo likens adopting a modern CSS feature to introducing a new tenant into a shared apartment. Even if the new tenant is quiet and efficient, they still share common spaces with existing housemates: legacy JavaScript observers, surrounding layout flows, the cascade, and the browser’s layout engine. Systemic stability depends on how seamlessly these interconnected systems co-exist.
This tension between minimalist syntax and broader system overhead has sparked widespread industry discussion. In an essay titled "Modern Frontend Complexity: essential or accidental?", software developer Igor Roztropiński questioned whether web application architecture could be simplified by leveraging modern browser capabilities from scratch.
Responding to the essay in online developer forums, engineers expressed divided views. Pushing back against the notion that modern CSS features inherent simplicity, one commenter noted that many architectural redesigns claiming to do more with less often end up simply doing less. Conversely, advocates pointed out that front-end JavaScript frameworks frequently introduce significantly more accidental complexity than modern CSS features, arguing that recent CSS advancements in handling interactivity remain underutilized due to an over-reliance on JavaScript.
Shoyombo places his analysis directly in the middle of this debate. While CSS capabilities have advanced significantly, reducing code length on paper does not inherently translate to reduced operational complexity. A native feature reduces application complexity only when it cleanly replaces legacy logic without destabilizing adjacent architectural behaviors.
Scenario 1: Behaviors Driven by Competing Owners
The first major architectural vulnerability occurs when behavioral logic becomes split between JavaScript and CSS, leaving a component with dual ownership.
Consider a standard media card component designed to switch to a multi-column layout when sufficient inline space is available. Historically, this state change was managed by instantiating a JavaScript ResizeObserver that dynamically toggled a modifier class, such as .card--wide. The presence of .card--wide in turn updated grid layout templates and font sizes in the stylesheet.
With modern CSS Container Queries, developers can remove the JavaScript observer entirely, applying layout adjustments directly within an @container block:
.card
container-type: inline-size;
@container (width > 480px)
.card__body
display: grid;
grid-template-columns: 160px 1fr;
In practice, full-scale migrations rarely occur in a single, perfectly isolated commit. During partial refactoring, a developer might introduce the @container rule and remove the class-based grid styles, but overlook a legacy JavaScript file that continues toggling .card--wide. Simultaneously, secondary selectors like .card--wide .card__title may survive undetected in separate stylesheets.
As long as both implementations share identical breakpoint thresholds—such as 480 pixels—the component appears to function normally. However, if a design update adjusts the container query threshold to 600 pixels while leaving the legacy JavaScript observer intact at 480 pixels, the component breaks. Between 480 and 600 pixels, the card enters an unhandled hybrid state: displaying a single-column body driven by the CSS rule alongside a wide-layout title driven by the orphaned JavaScript class.

Debugging such issues requires developers to recognize that component state is split across competing architectural layers. Similar conflicts frequently arise when native CSS popovers coexist with legacy event handlers, or when CSS scroll-driven animations run alongside JavaScript scroll listeners.
To avoid these dual-ownership traps, engineering teams must ensure that migrating a behavior to native CSS involves completely removing legacy script listeners, modifier classes, and associated cascade rules.
Scenario 2: Layout Side Effects of Block Formatting Contexts
The second challenge involves unexpected layout side effects caused by declaring modern CSS properties. When developers declare container-type: inline-size to enable container queries, the browser interprets the declaration as a instruction to modify fundamental layout rules.
According to specifications documented by MDN, assigning a container-type value other than normal applies style and inline-size containment to the element while establishing a new Block Formatting Context (BFC). A BFC alters how an element interacts with its surroundings, changing layout behaviors that extend far beyond query calculations:
- Margin Collapsing Halts: Under standard block layout rules, adjacent vertical margins collapse into a single shared gap. Establishing a BFC prevents margin collapsing across the container’s boundary. As a result, internal child margins no longer collapse with external sibling margins, expanding the total calculated height of surrounding elements.
- Float Clearance Shifts: In traditional layouts, block elements sit behind floated elements while their inline text content wraps around them. Once an element establishes a new BFC, its entire bounding box is pushed adjacent to the floated element, altering its inline alignment and shrinking its available width.
- Content-Sized Containers Collapse to Zero: Inline-size containment explicitly prevents an element from calculating its width based on its child contents. If an element with
container-type: inline-sizerelies on intrinsic sizing—such as being rendered inside a flexbox row, awidth: fit-contentblock, aninline-blockcontainer, or an automatic grid track—its inline dimension collapses to zero pixels, causing components to disappear entirely.
While a plain block element typically retains its width by inheriting dimensions from its parent, components positioned within dynamic flex rows or float layouts can suffer severe visual regressions simply by introducing container queries.
Before applying container-type, developers must evaluate whether the target container relies on intrinsic sizing, sits adjacent to floated elements, or depends on vertical margin collapsing.
Scenario 3: Specificity and Cascade Pitfalls in Modern Selectors
Modern CSS features like the :has() relational selector and native nesting permit highly expressive syntax. However, these tools can unintentionally increase selector scope and specificity weight, creating conflicts within the cascade.
For instance, a team attempting to strip bottom margins from card body text when a card contains an image might write:
.card:has(img) p
margin: 0;
.comment p
margin-block: 8px;
If a nested component—such as an avatar inside an embedded user comment—contains an img element, the unconstrained :has(img) selector matches any descendent paragraph inside the card parent. Because .card:has(img) p carries a higher specificity (0-2-0) than standard component class selectors like .comment p (0-1-0), it overrides the intended spacing of nested elements.
If a user posts a comment containing an image, the surrounding card text loses its margin dynamically at runtime—creating intermittent bugs that are notoriously difficult to reproduce. Constraining the rule using child combinators, such as .card:has(> img) > p, confines style modifications strictly to direct children.
A similar specificity trap exists within native CSS nesting. Per the W3C specification, the & nesting selector inherits the highest specificity from its parent selector list, matching the behavior of :is().
Consider the following nested structure:
.card, #featured
& a
color: blue;
.button
color: red;
Even if #featured is not present in the DOM, the nested link selector & a inherits the high specificity of #featured (1-0-1), causing standard .button links within a .card to evaluate as blue rather than red. Developers can neutralize this specificity inflation by wrapping multi-selector lists in :where(), which explicitly resets selector specificity weight to zero:

:where(.card, #featured)
& a
color: blue;
Cascade management becomes further complicated when mixing legacy styles with modern Cascade Layers (@layer). Normal layered styles inherently yield precedence to unlayered styles, regardless of individual selector specificity. Consequently, migrating new components into @layer components while leaving legacy stylesheets unlayered causes legacy rules to consistently override new styles, unless developers apply !important flags or completely migrate the legacy code into distinct cascade layers.
Scenario 4: The Rendering Engine Performance Bill
A common rationale for replacing JavaScript logic with native CSS is that offloading tasks to the browser’s native engine improves performance. However, Shoyombo emphasizes that shifting work to CSS merely alters who executes the work, not the total volume of computation required.
The browser’s rendering engine must calculate style recalculations whenever DOM mutations occur. To measure the real-world performance impact of complex relational selectors, Shoyombo benchmarked style evaluation metrics across a 5,000-row dataset in Chrome 153. The test toggled an .is-selected class across rows while monitoring layout recalc times and restyle element counts in DevTools.
The benchmark highlighted critical performance distinctions:
- Direct Class Toggle via JavaScript: Toggling a class directly on a target toolbar element executed in under 0.1 milliseconds, forcing the engine to restyle only 1 target element.
- Global
:has()Rule Attached to Body: Using a broad relational rule—body:has(.row.is-selected) .toolbar—increased execution time to approximately 1.4 milliseconds per toggle. Although slightly slower, it successfully restricted restyling to a single element. - Scoped Component Rule: A localized rule matching specific list cells—
.list:has(.cell > b) .row—ran in under 0.1 milliseconds when tested in isolation, affecting only 1 element. - Combined Relational Rules: When the global body-anchored
:has()selector and the list-level:has()selector were present in the same stylesheet simultaneously, performance dropped sharply. Toggling a single row’s selected state forced Chrome to recalculate styles for all 5,001 elements, elevating execution time to roughly 7.0 milliseconds per event.
For comparison, having a JavaScript loop explicitly iterate over and restyle all 5,000 rows manually produced a similar cost of 6.0 milliseconds across 5,001 restyled elements.
Expanding the test dataset to 20,000 rows exacerbated the problem: the combined CSS relational rules forced 20,001 element restyles, consuming 31 milliseconds per toggle—equivalent to dropping nearly two full rendering frames at 60Hz. Shoyombo has formally documented this behavior in a bug report filed with the Chromium engineering team.
This unexpected overhead underscores a key performance reality: while individual CSS rules may pass code reviews, combining complex relational selectors can trigger expensive global style recalculations.
Similarly, animating layout-impacting properties like width, top, or margin via CSS transitions forces the rendering engine to recalculate page layout on every frame. Conversely, driving hardware-accelerated properties like transform or opacity via JavaScript avoids layout recalculations altogether. The performance cost is dictated by the underlying property being altered, rather than the language driving the change.
Greenfield Visions Versus Real-World Codebase Migrations
The root cause of these architectural friction points lies in the gap between greenfield development assumptions and production migration realities. Modern web APIs are typically demonstrated in clean, isolated environments free of legacy technical debt. In production codebases, however, a single pull request refactoring a component to use container queries and relational selectors can simultaneously trigger layout regressions, specificity overrides, and performance bottlenecks.
In community discussions regarding modern styling paradigms, developers repeatedly note that tooling simplicity during initial setup often gives way to complex runtime debugging later on. Refactoring existing applications to adopt newer browser features often requires updating entire component trees to maintain architectural consistency.
When refactoring components, engineering teams should evaluate modern CSS features through a structured migration checklist:
- State Ownership: Have all legacy JavaScript observers, event listeners, and modifier classes driving the target behavior been completely removed?
- Layout Boundaries: Does applying
container-typeintroduce size containment or Block Formatting Contexts that disrupt adjacent float clearances, margin collapse, or intrinsic element sizing? - Selector Scope: Are modern relational selectors (
:has()) and nested rules appropriately constrained with direct child combinators to prevent unintended style inheritance in deeper DOM subtrees? - Cascade Integration: How do newly introduced styles interact with existing Cascade Layers (
@layer) and unlayered legacy stylesheets? - Combined Selector Overhead: Do active relational selectors interact with unrelated state toggles in ways that force unexpected tree-wide style recalculations?
- Performance Profiling: Have style recalculation metrics and layout frame rates been profiled under realistic DOM data scales using browser DevTools?
By systematically addressing these questions during code reviews, engineering teams can successfully leverage the power of modern CSS while preserving overall front-end reliability and performance.