← Back to Overview Chapter 09: Modern CSS

1. CSS Custom Properties & Live Theme Switcher

CSS variables (--var-name) allow dynamic theming that cascades down the DOM tree without reloading or CSS preprocessing.

Live CSS Theme Component
Magic Wand

Dynamic Palette Application

Notice how the background, border, typography color, and button style adapt synchronously through CSS Custom Properties!

:root {
    --bg: #f8fafc;
    --card: #ffffff;
    --primary: #008080;
    --text: #1e293b;
}
.theme-dark {
    --bg: #0f172a;
    --card: #1e293b;
    --primary: #38bdf8;
    --text: #f8fafc;
}

2. Sibling Hover Dimming using :has()

Hover over any card below. Notice how all other cards dim smoothly, focusing attention on the active element — achieved purely in CSS without JS mouse event handlers!

Rocket
Fast
Gem
Precious
Fire
Dynamic
Hero
Powerful
/* When container has a hovered child, dim all other children */
.container:has(.item:hover) .item:not(:hover) {
    opacity: 0.35;
    transform: scale(0.95);
}
.item:hover {
    transform: scale(1.05);
}

3. Native aspect-ratio (No Padding Hacks!)

Replace older padding-top: 56.25% hacks with modern native aspect-ratio for responsive video wrappers and square avatars.

16 / 9 Video widescreen ratio
1 / 1 Square avatar / icon
4 / 3 Standard photo ratio
.video-box  { aspect-ratio: 16 / 9; }
.avatar-box { aspect-ratio: 1 / 1; }
.photo-box  { aspect-ratio: 4 / 3; }

4. Pure CSS Scroll Snap Carousel

Smooth touch and mouse scrolling carousel that snaps to cards automatically. Scroll horizontally below!

.carousel {
    display: flex;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    scroll-behavior: smooth;
}
.card {
    scroll-snap-align: center;
}

5. Modern Architecture: Nesting, @layer & @scope (#9, #10, #12)

Modern CSS supports native nesting without preprocessors, cascade layers (@layer) for controlling specificity wars, and scoped styles (@scope).

Native CSS Nesting

No Sass/Less required! & syntax works natively in evergreen browsers.

@layer & @scope Architecture

@layer components { .btn { ... } } guarantees priority order regardless of selector weight. @scope (.card) to (.content) creates donut scopes that don't leak styles!

/* Native CSS Nesting */
.card {
    background: #f8fafc;
    & .btn {
        background: teal;
        &:hover { background: darkcyan; }
    }
}

/* Cascade Layers & Scoped Styles */
@layer reset, base, components, utilities;
@scope (.card) to (.card-body) { p { color: #475569; } }

6. Color Mixing, Typography & Performance (#31, #33, #34, #35, #41)

color-mix(in srgb, teal 75%, white):
🎨 Blended purely in CSS with color-mix()
font-variant-numeric: tabular-nums:
00:00:00
Monospaced numbers prevent jitter during counter updates!
text-wrap: balance vs pretty:
Balanced Headline That Automatically Avoids Ugly Single-Word Orphans
-webkit-line-clamp: 3 (Multiline Truncate):
This paragraph contains an extensive amount of explanatory documentation regarding modern CSS layout and typographic features. Instead of running on indefinitely or requiring JavaScript string substring operations, CSS automatically truncates overflowing content after exactly three lines with an ellipsis.
/* Native Color Mixing */
.mixed { background: color-mix(in srgb, var(--primary) 75%, white); }

/* Typography Balancers & Monospace Numerals */
h1 { text-wrap: balance; }
p  { text-wrap: pretty; }
.timer { font-variant-numeric: tabular-nums; }

/* Multiline Truncation */
.clamp-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
Fixed 🏠