Skip to content

Engineering

Modern CSS in Velocity: Letting the Browser Do the Work

OKLCH tokens, color-mix(), fluid clamp() scales and cascade layers: the modern CSS already in Velocity's code, and the newer features we're watching next.

Engineering No. 03

A lot of front-end JavaScript exists to do things CSS can now do on its own: pick a color between two others, scale type between breakpoints, sort out which rule wins, position a tooltip, start an animation on scroll. Every one of those that moves into the stylesheet is code you no longer ship, load or maintain.

Last week on the Southwell Media blog we looked at the newest of these features in CSS in 2026: 7 Features That Let the Browser Do the Work. This post is the practical companion. It shows the modern CSS that’s already in Velocity, with the files it lives in, and where the newer features would fit next.

Everything below comes from the Velocity v0.1.0 beta source.

OKLCH for every color token

All of Velocity’s color tokens are written in OKLCH. From src/styles/tokens/colors.css:

--brand-500: oklch(62.5% 0.22 38);
--brand-600: oklch(53.2% 0.19 38);
--brand-700: oklch(45.5% 0.16 38);

OKLCH describes a color by lightness, chroma and hue. The useful part is that lightness is perceptual: two colors at the same lightness look about equally light, whatever their hue. That makes a scale predictable. In the brand scale above, the hue barely moves, and stepping down the lightness gives you a darker orange that still reads as the same orange.

The status colors use the same idea. Each one has a base, a light background and a foreground, and the file notes that the foreground is chosen to meet WCAG AA on its light background:

--success-light: oklch(94% 0.05 145);
--success-foreground: oklch(35% 0.15 145); /* WCAG AA compliant on success-light */

Because the hue stays fixed and only lightness and chroma change, the background and text stay visibly related while the contrast between them is easy to reason about.

oklch() is supported in all current major browsers. See MDN for the syntax.

Tokens as custom properties

The tokens are plain CSS custom properties, and that’s what makes dark mode cheap. colors.css defines semantic tokens like --background and --foreground on :root, then redefines them under .dark:

.dark {
  --background: var(--gray-950);
  --foreground: var(--gray-50);
  --border: var(--gray-800);
}

Components never ask which theme is active. They use var(--background), and the browser resolves it. Switching themes is one class change on the root element, with no re-rendering and no theme object passed through JavaScript.

src/styles/global.css wires the same tokens into Tailwind v4 through @theme, so bg-background and text-foreground resolve to the same variables:

@theme {
  --color-background: var(--background);
  --color-foreground: var(--foreground);
}

color-mix() instead of another token

Sometimes you need a tint, not a new color. The component showcase page, src/pages/components.astro, makes a translucent header background from an existing token:

background-color: color-mix(in oklch, var(--background-secondary) 50%, transparent);

There’s no extra token for “secondary background at half opacity”, and it follows the theme automatically because --background-secondary changes under .dark. Mixing in oklch keeps the result in the same color space as the tokens. color-mix() is supported in all current major browsers (MDN).

Fluid type and spacing with clamp()

Velocity’s type scale doesn’t jump at breakpoints. Each size grows smoothly between a minimum and a maximum. From src/styles/tokens/typography.css:

--text-base: clamp(1rem, 0.925rem + 0.375vw, 1.0625rem);
--text-5xl: clamp(3rem, 2.25rem + 3.75vw, 4.5rem);

Section spacing works the same way in src/styles/tokens/spacing.css, built from the fixed spacing tokens:

--space-section: clamp(var(--space-16), 10vw, var(--space-24));
--space-container-x: clamp(var(--space-4), 5vw, var(--space-8));

Without clamp(), you’d write a media query per size per breakpoint, or measure the viewport in a script. Here the browser computes it, and the minimum and maximum keep text readable on small screens and in check on large ones. clamp() has been supported in all major browsers for years (MDN).

Cascade layers

global.css puts its own rules into @layer base, @layer components and @layer utilities, the same layers Tailwind uses. Layers decide which rule wins by layer order first, before specificity. A utility class beats a component style because utilities come later, not because someone added a more specific selector.

That’s the specificity problem the SWM post describes for @scope, solved one level up. It’s why you can put a Tailwind class on a Velocity component and expect it to apply. Cascade layers are supported in all current major browsers (MDN).

Small things that add up

  • :focus-visible in global.css draws the focus ring only when the browser decides it’s needed, like keyboard navigation, so mouse clicks don’t leave outlines behind.
  • :where() in the dark variant, @custom-variant dark (&:where(.dark, .dark *));, adds zero specificity, so dark styles don’t escalate the cascade.
  • prefers-reduced-motion in src/components/landing/LighthouseScores.astro turns off the score animation and shows the final state for people who ask for less motion.
  • scroll-mt-32 on sections in the component showcase gives anchor targets a scroll margin, so headings don’t land under the sticky header and section nav.

Where the newer features would fit

None of the features in the SWM post are in Velocity’s code yet. They’re new enough that we’re watching browser support first. But the codebase already has places where they’d remove JavaScript.

Anchor positioning

The Tooltip in src/components/ui/Tooltip.astro positions its bubble with absolute offsets per side in global.css:

[data-tooltip-position="top"] .tooltip-bubble {
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%) scale(0.96);
}

It works, but it can’t flip when there’s no room above. Anchor positioning, with position-anchor and position-try-fallbacks, lets the browser handle that.

Scroll-driven animations

LighthouseScores.astro pauses its animations, then uses an IntersectionObserver to start them once the section is 30% visible. animation-timeline: view() expresses the same idea in CSS, with no observer script.

Also on the list

@scope, sibling-index() and sibling-count(), CSS carousels, masonry layout and the Interest Invoker API are covered in the SWM post, with the current state of support for each. None of them are used in Velocity today.

The pattern

The features Velocity already relies on share one trait: they move a decision to the browser. Which shade, which size, which rule wins, which theme. Each one is a line of CSS instead of a function, and each one is supported widely enough to ship without a fallback.

The newer features continue the same direction. When they’re ready, the Tooltip and the scroll animation are the first places we’ll look.

Velocity is open source on GitHub. Start a project with create-velocity-astro.

All posts
Share:

Related posts