r/Frontend • u/Both-Tart7460 • 11h ago
How CSS :has() and @container let me cut ~200 lines of UI state JS in a <10KB zero-build tool
Hey everyone! A few weeks ago I shared a post here about how UTF-16 surrogate pairs and historical Unicode holes break naive math-offset text converters. A bunch of you gave super helpful feedback on Safari clipboard edge cases, which pushed me to clean up the CSS architecture of my little side project (100emoji.shop β single static HTML file, no bundler/minifier so view-source has the full readable code).
When building a live preview grid with 20+ text style cards and category filters, my initial instinct was the usual JS-heavy route: track filter/input state in JS, toggle .is-empty classes, listen to input/focus to collapse the header on mobile, and add viewport breakpoints so wide glyphs don't overflow.
While refactoring to keep the whole app under 10KB gzipped, I realized modern CSS can handle almost all of that UI state declaratively on the compositor/layout thread with zero JS listeners. Here are the 3 patterns that replaced ~200 lines of glue code for me:
1. Zero-JS Empty States with :has()
Previously, every time a filter or search ran, JS had to count visible cards and toggle an empty-state element:
// Old imperative glue
With :has(), the grid container already knows whether any unhidden card exists:
}
/* Show empty state automatically whenever no visible card remains */
}
Now filtering logic only sets card.hidden = !matches and walks away. No state sync bugs, no extra DOM queries.
2. Auto-Collapsing the Mobile Hero Header on Input
On mobile screens, once someone taps the text input or starts typing, a tall hero banner wastes half the viewport above the virtual keyboard. Instead of binding focus/input/scroll handlers to toggle classes on <body>, :has() handles both focus and non-empty input natively:
}
}
Quick tip: make sure your <input id="user-input" placeholder=" "> has at least a non-empty placeholder string so :not(:placeholder-shown) reliably detects when text is present.
3. Per-Card Fluid Sizing with @container + cqi Instead of Viewport Media Queries
Unicode mathematical alphabets (like Double-Struck πππππ ππ or Fraktur ππ’π©π π¬πͺπ’) have much wider advance widths than normal Latin characters, so fixed rem sizes overflow narrow grid columns fast.
Instead of juggling @media queries for 1-col, 2-col, and 3-col viewports, turning each card into an inline-size query container let every card scale its own typography from its actual column width:
}
/* Font size scales smoothly with the card's own inline width */
}
@) }
}
Takeaways & Caveats
- Performance: Keeping
:has()scoped to direct containers (.previews-grid:not(:has(...))) and specific IDs (#user-input) kept style recalculation under ~0.3ms in Chrome DevTools performance traces, even when typing fast on throttled CPU. - Browser support:
:has()and@containerinline-size queries have been baseline across Chrome, Edge, Firefox, and Safari for a good while now, so for modern utility tools we didn't need a JS fallback.
Curious if anyone else here has been ripping out small UI state listeners in favor of :has() / container queries lately, or if you've hit selector performance gotchas on much larger DOM trees? Happy to share more details if anyone is curious!