Design Tokens to a Tailwind Theme: Light and Dark Mode with @theme inline
Name a colour once and one switch changes the whole page: tokens.css with :root and .dark values, @theme inline in index.css, a React page with only token classes, a ThemeToggle that flips one class on html, and what one hard-coded bg-white breaks.
What you will learn
- Store light and dark values under the same token names in tokens.css
- Register the tokens with Tailwind CSS 4 using @theme inline
- Build a React page that uses only token classes like bg-page and text-ink
- Switch the whole theme by toggling one dark class on the html element
Before you start
- Tailwind CSS 4 in React
- Design Tokens: Primitive and Semantic Tokens
Think about a paint tin with a label. The label says page. It describes the wall behind everything. In light mode the paint inside is almost white, and in dark mode it is a deep navy. The label never changes. Only what is in the tin does.
Those labels are design tokens: names for values. In this article you take the colours from a design, turn them into a Tailwind CSS 4 theme, and flip a whole React page between light and dark with one switch. The page is small: a card with a Done badge, a title, an owner and a button.
The labels: page, card, ink, brand
| Token | Job | Light | Dark |
|---|---|---|---|
--page | The wall behind everything | #f8fafc | #0b1120 |
--card | A panel on the wall | #ffffff | #1e293b |
--ink | Main text | #0f172a | #f1f5f9 |
--brand | Buttons and links | #4f46e5 | #818cf8 |
--ok-bg | The Done badge | #dcfce7 | #14532d |
The page asks for page, never for a colour. Change what is in the tin and every wall follows.
From Figma to a class, one name all the way
The designer creates a variable in Figma called color/page. You copy the name and its values into a CSS file as --page. You register it in the Tailwind theme as --color-page: var(--page), which links your variable to Tailwind. Tailwind then gives you the class bg-page. If the idea of tokens is new, the background is in primitive and semantic design tokens. Here we wire them into a React app that switches themes.
tokens.css: names and values
:root {
--page: #f8fafc;
--card: #ffffff;
--ink: #0f172a;
--ink-soft: #475569;
--line: #e2e8f0;
--brand: #4f46e5;
--on-brand: #ffffff;
--ok-bg: #dcfce7;
--ok-ink: #166534;
}
.dark {
--page: #0b1120;
--card: #1e293b;
--ink: #f1f5f9;
--ink-soft: #94a3b8;
--line: #334155;
--brand: #818cf8;
--on-brand: #0b1120;
--ok-bg: #14532d;
--ok-ink: #bbf7d0;
}
:root on line 1 holds the light values. .dark on line 13 repeats the same names with dark values. Look at --brand on line 19: same name, but a lighter indigo, because a light indigo reads better on a dark page. Only the values change, never the names.
index.css: @theme inline
@import "tailwindcss";
@import "./tokens.css";
@theme inline {
--color-page: var(--page);
--color-card: var(--card);
--color-ink: var(--ink);
--color-ink-soft: var(--ink-soft);
--color-line: var(--line);
--color-brand: var(--brand);
--color-on-brand: var(--on-brand);
--color-ok-bg: var(--ok-bg);
--color-ok-ink: var(--ok-ink);
--radius-card: 1rem;
}
First Tailwind is imported, then the tokens file. Then @theme inline tells Tailwind about the names. --color-page: var(--page) gives you bg-page, text-page and so on.
Why inline? Because the colour must be read from your variable at the moment the page is drawn. That is what lets it change between light and dark. And it is not only colours: --radius-card: 1rem gives you the class rounded-card.
App.tsx: only token classes
import { ThemeToggle } from "./ThemeToggle";
export default function App() {
return (
<main className="min-h-screen space-y-6 bg-page p-6 text-ink">
<header className="flex justify-between">
<h1 className="text-2xl font-bold">Team tasks</h1>
<ThemeToggle />
</header>
<section className="rounded-card bg-card p-6 shadow">
<span className="rounded-full bg-ok-bg px-3 text-ok-ink">
Done
</span>
<h2 className="mt-3 text-xl font-semibold">Write report</h2>
<p className="mb-4 text-ink-soft">Owner: Emma</p>
<button className="rounded bg-brand px-4 py-2 text-on-brand">
Open task
</button>
</section>
</main>
);
}
main uses bg-page and text-ink. The card uses bg-card and rounded-card. The button uses bg-brand with text-on-brand. There are no colour codes anywhere. The component says what it is, and the theme decides how it looks.
The switch: one class on html
import { useState } from "react";
export function ThemeToggle() {
const [dark, setDark] = useState(() =>
document.documentElement.classList.contains("dark")
);
function flip() {
const next = !dark;
document.documentElement.classList.toggle("dark", next);
localStorage.setItem("theme", next ? "dark" : "light");
setDark(next);
}
return (
<button
onClick={flip}
className="rounded-lg border border-line px-3 py-1"
>
{dark ? "Light mode" : "Dark mode"}
</button>
);
}
On line 10, a click toggles one class, dark, on the html element. The moment it is there, the .dark values win and the whole page changes. Line 11 saves the choice in localStorage so the browser remembers it. The button label follows the state: "Light mode" when dark is on, "Dark mode" otherwise.
One more line, in index.html, puts the class back before the page paints, so there is no flash of light on reload:
<script>
document.documentElement.classList.toggle("dark", localStorage.getItem("theme") === "dark");
</script>
The same set, light then dark

The badge, the card and the button all adapted. None of the classes changed. Only the variable values did. Do not worry about memorising the colours. With AI you can generate a whole palette in seconds. You need the foundation so you know where it goes.
Break it: one hard-coded colour ignores the switch
Change the card on line 10 of App.tsx:
// before
<section className="rounded-card bg-card p-6 shadow">
// after
<section className="rounded-card bg-white p-6 shadow">
Switch to dark mode:

Everything else turned dark, but the card stays white, and the light title is almost invisible on it. bg-white never reads a token. It does not know the theme exists. Go back to bg-card and the card follows the theme again.
Why this pays off in a big app
| Task | Colour codes in classes | Tokens |
|---|---|---|
| Change the brand colour | Edit every file | Edit one line |
| Add dark mode | Repeat it on every class | Write one .dark block |
| Match the designer | Copy values by eye | Same names as Figma |
| Keep a big app tidy | Colours slowly drift apart | One source of truth |
Common mistakes
- Forgetting the script in
index.html. Without it, a dark-mode user sees a flash of light on every reload. - Mixing
dark:prefixes with tokens. With tokens you do not needdark:bg-slate-800on each tag. The.darkblock does it once. - One stray
bg-whiteor hex colour. It is the one thing that will not switch.
Recap
Tokens are names for values. The theme connects those names to Tailwind. The switch is one class on html. One name, both themes. We covered the foundations, and AI can generate the rest. Try it yourself: add a --warn-bg and --warn-ink pair to both blocks, register them in @theme inline, and add a "Due soon" badge that works in both modes.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
One Request, Two Answers: the Same Post as an HTML Page and as JSON
Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.
A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.
How a Web Page Works: HTML Tags, Elements and Attributes
A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.
Semantic HTML: header, nav, main, aside and footer as Page Landmarks
The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.
