# Tailwind CSS v4 Design Tokens with OKLCH: Building Dark and Light Themes That Don't Drift

> How Haze UI structures semantic OKLCH tokens, opaque borders, and instant View Transition dark/light switching in src/styles/haze.css.

- Category: Design Systems · Published: 2026-10-15 · Updated: 2026-11-23 · Reading time: 9 min read
- Canonical URL: https://hazeui.com/guides/tailwind-v4-oklch-design-tokens-dark-light-mode
- Tags: tailwind-v4, oklch, design-tokens, dark-mode, theming

## Executive Summary

Defining Tailwind CSS v4 design tokens in OKLCH guarantees predictable perceived brightness across dark and light themes without hue drift. Haze UI maps DTCG tokens into semantic shadcn CSS variables inside @theme inline, replaces translucent borders with opaque grey hairlines so overlapping edges never brighten, and switches modes before first paint via a tiny inline guard.

Color drift is what gives away cobbled-together dark themes. Card borders glow bright at intersections because someone stacked `border-white/10` twice. Yellow status badges read fine on dark cards and turn unreadable on light surfaces because HSL `50%` lightness shifts wildly depending on hue.

Moving design tokens to Tailwind CSS v4's `@theme inline` backed by OKLCH perceptual lightness solves both problems cleanly—especially when paired with explicit border stepping and a zero-flash theme switcher.

Here is the token architecture powering Haze UI's [Theming system](/docs/theming) and [Theme Switch](/ui/theme-switch).

## Why OKLCH outperforms HSL and Hex for UI systems

In HSL, blue at `50%` lightness looks dramatically darker to the human eye than yellow at `50%` lightness. In OKLCH (`oklch(L C H)`), `L` models perceived lightness directly, `C` controls chroma saturation, and `H` rotates hue from `0` to `360` degrees.

That perceptual consistency lets you step surface hierarchy cleanly in dark and light modes:

| Semantic token | Dark mode (`:root`) | Light mode (`[data-theme="light"]`) | Visual role |
|---|---|---|---|
| `--background` | `oklch(0.19 0 0)` | `oklch(1 0 0)` | Page canvas |
| `--card` | `oklch(0.215 0 0)` | `oklch(1 0 0)` | Cards, panels, tables |
| `--popover` | `oklch(0.24 0 0)` | `oklch(1 0 0)` | Floating menus, dialogs, drawers |
| `--muted` | `oklch(0.26 0 0)` | `oklch(0.958 0 0)` | Sunken tracks and subtle wells |
| `--accent` | `oklch(0.285 0 0)` | `oklch(0.935 0 0)` | Hover and active row fill |
| `--border` | `oklch(0.29 0 0)` | `oklch(0.935 0 0)` | Opaque 1 px panel hairlines |
| `--border-strong` | `oklch(0.4 0 0)` | `oklch(0.82 0 0)` | Hovered card borders & emphasized dividers |
| `--foreground` | `oklch(0.96 0 0)` | `oklch(0.15 0 0)` | Primary text and active icons |
| `--muted-foreground` | `oklch(0.72 0 0)` | `oklch(0.42 0 0)` | Captions, metadata, helper copy |
| `--brand` | `oklch(0.74 0.13 255)` | `oklch(0.47 0.14 255)` | Focus highlights, chart accent, info status |

Notice two intentional choices in that table:

1. **Zero chroma on base surfaces.** Setting `C = 0` on `--background` through `--accent` eliminates blue-tinted mud and lets product content, status badges, and syntax highlighting hold visual focus.
2. **One hue reserved for state.** Hue appears on semantic status tokens (`--brand`, `--success`, `--warning`, `--destructive`) and data charts—never as decorative glow or colored card stripes.

## Opaque hairlines vs. translucent white borders

Look closely at any UI using `border-white/10` on dark panels. When a `Button Group` places three buttons side by side or a `Card` embeds an inner panel sharing an edge, two `10%` white borders overlap and composite to `19%`. You see bright tick marks at joints and corners.

Switching `--border` and `--border-strong` to **opaque grey OKLCH values** (`oklch(0.29 0 0)` dark, `oklch(0.935 0 0)` light) guarantees that overlapping borders stay completely invisible. Every divider, table row, and joined segment reads at exact uniform weight.

## Clean default vs. framed mode via `--haze-frame`

Design preferences shift between ultra-clean flat surfaces and crisp framed controls. Rather than maintaining two stylesheets or forcing teams to edit 240 components, `src/styles/haze.css` exposes a CSS toggle variable (`--haze-frame`, defaulting to `0`) evaluated inside `@theme inline` using `color-mix()`:

```css
[data-haze-frame] { --haze-frame: 1; }
[data-haze-frame="off"] { --haze-frame: 0; }

@theme inline {
  --color-frame: color-mix(in oklab, var(--border) calc(var(--haze-frame, 0) * 100%), transparent);
  --color-field: color-mix(in oklab, var(--card) calc(var(--haze-frame, 0) * 100%), var(--control));
  --shadow-key: 0 1px 2px 0 color-mix(in oklab, oklch(0 0 0 / 18%) calc(var(--haze-frame, 0) * 100%), transparent);
}
```

In the clean default (`--haze-frame: 0`), cards (`Card`) retain crisp `border-border` edges while form fields (`bg-field`), button tracks, and resting keys sit borderless on a quiet 4% contrast fill. Drop `data-haze-frame` onto `<html>` or any container, and raised key shadows (`shadow-key`), top highlights (`inset-shadow-highlight`), and crisp input outlines appear across every nested Haze component instantly.

## One `--radius` driving nested corner math

Rounded corners look clumsy when an outer container and an inner menu item share the same radius—nested corners appear pinched when spacing isn't accounted for.

In `haze.css`, every radius step derives mathematically from `--radius: 0.4rem` (`6.4px`):

- `rounded-xs`: `calc(var(--radius) - 5px)` (`1.4px` — checkbox marks)
- `rounded-sm`: `calc(var(--radius) - 4px)` (`2.4px` — tags, kbd keys, menu items)
- `rounded-md`: `calc(var(--radius) - 2px)` (`4.4px` — small controls)
- `rounded-lg`: `var(--radius)` (`6.4px` — buttons, inputs, menu surfaces)
- `rounded-xl`: `calc(var(--radius) + 6px)` (`12.4px` — cards, tables, alerts)
- `rounded-2xl`: `calc(var(--radius) + 10px)` (`16.4px` — dialogs, hero stages)

Following the rule **inner radius = outer radius − padding**, a `rounded-lg` menu surface (`6.4px`) with `p-1` (`4px` padding) holds `rounded-sm` (`2.4px`) items with concentric geometric alignment.

## Zero-FOUC dark/light initialization and GPU View Transitions

Theme toggles only feel polished when two rules hold simultaneously:

1. **Zero flash of wrong theme on hard refresh.** Run a minimal synchronous inline script inside `<head>` that reads `localStorage` (`"light"`, `"dark"`, or `"system"`), resolves OS media queries, and sets `data-theme`, `data-theme-pref`, and `document.documentElement.style.colorScheme` before paint.
2. **Smooth non-blocking toggles.** Defer the interactive View Transition reveal (`document.startViewTransition` animating `clip-path: circle(...)` from the clicked button coordinates) into an external cached ES module (`theme-controller.ts`), and temporarily disable CSS element transitions under `html[data-theme-motion]` so the browser composites one clean GPU snapshot instead of animating hundreds of DOM borders simultaneously.

Check [Theming](/docs/theming) for the full variable list, inspect [Theme Switch](/ui/theme-switch) and [Card](/ui/card) in live dark and light stages, or audit your contrast ratios next with [WCAG 2.2 AA in Dark Mode](/guides/wcag-22-aa-contrast-dark-mode-ui-checklist).

## Mentioned Haze UI Entries

- [Theme switch](https://hazeui.com/ui/theme-switch.md): A light, dark and system radio group that applies appearance to a chosen target and optionally remembers it.
- [Card](https://hazeui.com/ui/card.md): Bordered surface with an optional header, a body slot and a footer band; it can lift on hover when the whole card is a link.
- [Button](https://hazeui.com/ui/button.md): Button or link styled as a button, in seven variants and three text sizes, plus a square icon size and press and loading states.
- [Badge](https://hazeui.com/ui/badge.md): Compact round pill with neutral, brand, and status variants across three sizes, optionally rendered as a link.
- [Alert](https://hazeui.com/ui/alert.md): Inline message panel with a status icon, title, optional description and action slot, in five variants.
- [Button group](https://hazeui.com/ui/button-group.md): Related actions share joined borders, or sit in a sunken track where a raised key slides to the pressed button.

## Frequently Asked Questions

### What is the difference between clean default and data-haze-frame in Haze UI?

By default, Haze UI keeps cards bordered (border-border) while making controls, fields, tracks, and resting shadows clean by setting frame tokens (--color-frame, --shadow-key, --shadow-lift) transparent. Setting data-haze-frame on <html> or a section wrapper turns full framed borders and resting shadows back on with zero markup edits.

### Can I re-skin Haze UI with my own brand hue and corner radius?

Yes. Because components reference semantic tokens like --brand and --radius instead of hardcoded colors, changing --brand or --radius: 0.4rem inside src/styles/haze.css updates every installed block and component globally.

### How does Haze UI prevent a white flash on dark mode load?

A tiny synchronous inline script in <head> resolves localStorage preference and sets data-theme, data-theme-pref, and color-scheme on <html> before the first pixel renders, while View Transitions animate user-initiated theme toggles.
