Theming
Using CSS variables and theme tokens.
We use and recommend CSS variables for theming.
This gives you semantic theme tokens like background, foreground, and primary that
components use by default. Override those tokens in your CSS to change the look of your app without rewriting component classes.
<div class="bg-background text-foreground"></div>init puts them in Styles/theme.css, which is yours to edit. Tailwind maps these tokens into utilities like bg-background, text-foreground, border-border, and ring-ring.
Dark mode works by overriding the same tokens inside a .dark selector. See the dark mode docs for toggling the .dark class.
Token Convention
We use semantic background and foreground pairs. The base token controls the surface color and the -foreground token controls the text and icon color that sits on that surface.
primary pairs with primary-foreground.Given the following CSS variables:
--primary: oklch(0.205 0 0);
--primary-foreground: oklch(0.985 0 0);The background color of the following component will be var(--primary) and the foreground color will be var(--primary-foreground).
<div class="bg-primary text-primary-foreground">Hello</div>Theme Tokens
These tokens live in Styles/theme.css under :root and .dark.
Radius Scale
--radius is the base radius token for your theme.
We derive a small radius scale from it so components can use consistent corner sizes while still sharing a single source of truth.
@theme inline {
--radius-sm: calc(var(--radius) * 0.6);
--radius-md: calc(var(--radius) * 0.8);
--radius-lg: var(--radius);
--radius-xl: calc(var(--radius) * 1.4);
--radius-2xl: calc(var(--radius) * 1.8);
--radius-3xl: calc(var(--radius) * 2.2);
--radius-4xl: calc(var(--radius) * 2.6);
}This means:
radius-lgis the base value.- Smaller radii scale down from
--radius. - Larger radii scale up from
--radius. - Changing
--radiusupdates the entire radius scale.
Adding New Tokens
To add a new token, define it under :root and .dark, then expose it to Tailwind with @theme inline.
:root {
--warning: oklch(0.84 0.16 84);
--warning-foreground: oklch(0.28 0.07 46);
}
.dark {
--warning: oklch(0.41 0.11 46);
--warning-foreground: oklch(0.99 0.02 95);
}
@theme inline {
--color-warning: var(--warning);
--color-warning-foreground: var(--warning-foreground);
}You can now use bg-warning and text-warning-foreground in your components.
<div class="bg-warning text-warning-foreground"></div>Contrast
The tokens in Styles/theme.css meet the contrast WCAG 2.2 AA asks for, in
both modes. When you change them, keep these pairs at these ratios:
--muted-foregroundand--destructive, as text: 4.5:1 against--background,--card,--popoverand--muted, and--destructiveagainst its own tints too.--input, the border of inputs, checkboxes and radios and the track of switches, sliders and progress bars: 3:1 against what it sits on.--ring, which draws focus as a halo at 50% opacity: 3:1 against the background once blended.
Other Themes
Styles/theme.css starts with the neutral theme. The variables of any theme that uses the same token
names paste straight into :root and .dark. Check a pasted theme against the ratios above: many
use lighter muted text, input borders and rings.