Modern CSS in Practice: The scroll-state() Sidebar Loop, Beyond 12-Column Grids, @layer, Diagrams, and random()Thematic roundup · digest for November 26
Isometric architectural model of CSS cascade layers and a cyclic dependency in a sidebar
Thematic roundup5 sections≈ 7 min

Modern CSS in Practice: The scroll-state() Sidebar Loop, Beyond 12-Column Grids, @layer, Diagrams, and random()

Shrink your laptop browser window vertically, and the sidebar menu starts flickering wildly. Modern CSS already gives us if() conditions, sibling-count(), vector shape() contours, @layer cascade layers, and random(). Along with that power come subtle traps. Here are five practical techniques that simplify layout without extra JavaScript—and keep you out of hidden loops.

Why scroll-state() Traps a Collapsing Sidebar in a Loop

In a vertical sidebar with icons and labels, short screens trigger a scrollbar. You want to hide the text labels with clip-path: inset(50%) (keeping them accessible to screen readers) and show only icons.

If you tie that to scroll-state() (a scroll-state container query), the layout loses its mind. The sidebar overflows → @container sidebar scroll-state(scrollable: y) hides the labels → total height shrinks → scrolling disappears → labels come back. It's an endless loop, like an automatic door whose sensor watches its own door leaf. A plain @container (height < 500px) check is brittle too: 500px breaks as soon as you add a menu item, and var() doesn't work inside size queries.

In a breakdown of the compacting sidebar, the loop is broken using Style Queries (comparing CSS custom properties on a container): register <length> properties with @property, compute --limit via sibling-count() (child count in the parent), and compare it against 100cqh.

/* WRONG: infinite layout loop */
.sidebar-inside { container: sidebar / scroll-state; }
@container sidebar scroll-state(scrollable: y) {
  .nav-label { position: absolute; clip-path: inset(50%); }
}

/* WORKING SOLUTION: Style Queries + @property + sibling-count() */
@property --sidebar-h {
  syntax: "<length>"; inherits: true; initial-value: 0px;
}
@property --limit {
  syntax: "<length>"; inherits: true; initial-value: 0px;
}
.sidebar-inside { --sidebar-h: 100cqh; }
.nav-item {
  container-name: nav-item;
  --limit: calc(
    (var(--spacing) * 2 + var(--icon-size) + var(--text-gap) + 14px)
    * sibling-count() + var(--spacing) * 2
  );
}
@container nav-item style(--sidebar-h < --limit) {
  .nav-label {
    position: absolute; width: 1px; height: 1px;
    overflow: hidden; clip-path: inset(50%); white-space: nowrap;
  }
}

There's no loop because --limit depends on constants and item count, not label height. You only assume 14px for line height, and note that sibling-count() counts all children, so an extra <hr> would skew the math. It works in Chrome 142+ and Firefox 154+; in Safari, labels simply stay visible.

Beyond 12 Columns: Expressive Layouts in CSS Grid

The habit of slicing every screen into 12 equal strips dates back to 2008's 960 Grid System and Bootstrap. CSS Grid removed that constraint years ago, yet most sites still look stamped from the same mold.

A guide to expressive grids using the Wheelman brand shows two content-driven approaches. Modular grids add rows—say, 4×4 (16 modules)—where a headline and hero photo each span 4 modules while alternative cards take 1 or 2. Compound grids overlay two grids on top of each other: 2 columns over 3 produce 4 columns of two widths (2fr 1fr 1fr 2fr), while combining 4 and 5 builds an asymmetric system across sections.

/* Compound grid: 2 + 3 columns -> 4 unequal tracks */
.compound-grid {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 2fr;
  gap: 1.5rem;
}

One grid-template-columns line breaks cookie-cutter symmetry while keeping a tight visual rhythm. It's far easier to size tracks to your actual photos and copy than to force them into a 12-column template.

Cascade Layers (@layer) in Real Projects

As a codebase grows, selector wars follow: extra classes, #id hooks, and !important. Cascade layers (@layer) act like shelves with strict priority: a higher layer always beats a lower one, so a plain p in an upper layer overrides #foo .bar.baz below.

In a case study on migrating a site to @layer, the layer order mirrors Sass 7-1 folders from vendors to helpers. That lets you strip out specificity hacks and nearly every !important.

/* Declare layer order at the top of base/base.css */
@layer vendors, base, layout, components, pages, helpers;
@import url("/css/vendors/charts.css") layer(vendors);

@layer components {
  .Fleuron { display: inline-block; transform: scale(0.75); }
}

<link rel="stylesheet"> still lacks a layer attribute in all browsers, so third-party CSS loads via <style>@import ... layer(vendors);</style>, which can slow preloading. Unlayered styles always beat layered ones, revert-layer lets you roll back, and checking @layer support (~97%) via @supports at-rule(@layer) is pointless because at-rule() itself has worse support.

Diagrams and Graphs in Pure Modern CSS

Connecting nodes with lines without SVG or Canvas is now possible using CSS Anchor Positioning (anchoring to another box's coordinates), border-shape with shape() (CSS vector curves), if(), and hypot().

In an experiment with CSS diagrams, dynamic connectors check the center-to-center distance via hypot(100cqw, 100cqh) < 200px and switch between nearest sides and a straight line to the center. A static variant divides nodes into four anchor quadrants and nests if() directly inside shape().

/* if() condition right inside a vector shape() curve */
.link *:before {
  border-shape: shape(
    from 0 0,
    curve to 50% 50%
      with if(style(100cqh < 100px): 0; else: 150px;) 0,
    smooth to 100% 100%
  ) padding-box;
}

When the container height drops below 100px, the curve flattens to zero and the line straightens. It's not ready for production—full support is limited to Chrome and Edge, so keep SVG or Canvas for real graphs—but it's a great playground for new primitives.

CSS random() in Practice: Margin Notes Across All Engines

With Firefox 161 on November 24, 2026, random() now works across all three major engines alongside Safari 26.2 and Chrome 156. Slight variations in angles and spacing no longer need scripts or cause hydration flicker.

In a deep dive on CSS random(), the function tilts .sidenote margin notes slightly. It takes a minimum, a maximum, and an optional step (random(0px, 10px, 5px) yields 0, 5, or 10). Across @media and @keyframes, the angle stays stable: by default (auto), the browser caches the value per element, property, and call index.

/* auto: unique angle per note, stable across @media */
.sidenote { transform: scale(0.925) rotate(random(-2.5deg, 2.5deg)); }
@media (min-width: 55rem) {
  .sidenote { transform: scale(1) rotate(random(-2.5deg, 2.5deg)); }
}

/* Explicit key + scope (space-separated!) and debug fixed 0.5 */
.box {
  margin-block: random(
    --gap element-scoped property-scoped, 1rem, 5rem
  );
  rotate: random(fixed 0.5, -2.5deg, 2.5deg);
}

You can control caching explicitly: element-scoped shares a value across properties on one element, property-scoped shares across elements for one property, a standalone --id gives one shared value page-wide, and fixed 0.5 locks output for debugging. Watch two gotchas: separate --id and scope with a space rather than a comma, and don't try saving a value in --random-margin: random(0px, 10px) on :root—each var() substitution rolls the dice again.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 7