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:
function syncEmptyState() {
const visibleCount = grid.querySelectorAll('.preview-card:not([hidden])').length;
emptyBanner.hidden = visibleCount > 0;
}
With :has(), the grid container already knows whether any unhidden card exists:
.empty-state-banner {
display: none;
}
.previews-grid:not(:has(.preview-card:not([hidden]))) + .empty-state-banner {
display: flex;
flex-direction: column;
align-items: center;
padding: 3rem 1rem;
}
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:
.hero-header {
max-height: 220px;
opacity: 1;
overflow: hidden;
transition:
max-height 0.28s cubic-bezier(0.16, 1, 0.3, 1),
opacity 0.2s ease,
margin 0.28s ease;
}
body:has(#user-input:focus, #user-input:not(:placeholder-shown)) .hero-header {
max-height: 0;
opacity: 0;
margin-bottom: 0.5rem;
}
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:
.preview-card {
container-type: inline-size;
container-name: card;
display: flex;
flex-direction: column;
padding: 1rem;
border-radius: 12px;
}
.preview-output {
font-size: clamp(0.95rem, 6.5cqi, 1.3rem);
line-height: 1.4;
overflow-wrap: anywhere;
}
@container card (max-width: 260px) {
.card-actions {
flex-direction: column;
gap: 0.35rem;
}
}
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 @container inline-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!