Your office logo is orange, #f97316. If you write that colour code on every button, the code is spread over dozens of places. When the brand changes colour, you have to find each one by hand. It's easier when that colour has a name of its own.
Tailwind comes with a built-in theme: a set of ready-made values such as a colour palette, fonts, text sizes and breakpoints. You can add your own, such as a brand colour, and get classes like bg-brand right away. In Tailwind v4, this goes in your CSS file, inside an @theme block. It's like asking a paint shop to mix a special colour and give it a name, such as "office orange". From then on, you just say its name every time you buy. The @theme block is the paint shop: you register the colour #f97316 once under the name brand, then you only write the name in a class, such as bg-brand.
@theme: register once, get the classes
This is the CSS file:
@import "tailwindcss";
@theme {
--color-brand: #f97316;
--font-heading: Georgia, serif;
}
Then the HTML that uses both tokens:
<div class="p-6 rounded-xl bg-brand">
<h2 class="font-heading text-xl font-bold text-white">Custom Card</h2>
</div>
@import "tailwindcss";loads Tailwind. This line always goes at the top of your CSS file.@theme { ... }holds tokens: design values (colours, fonts, sizes) you give a name, written like CSS variables. The Tailwind docs call them theme variables.- A token's name = a token prefix (the namespace) + a name you choose.
--color-branduses the--color-prefix, so Tailwind makesbg-brand,text-brandandborder-brand. The CSS ofbg-brand:background-color: var(--color-brand). --font-headinguses the--font-prefix, so Tailwind makes afont-headingclass.
| Token prefix | Example token | Classes you get |
|---|---|---|
--color- |
--color-brand: #f97316 |
bg-brand, text-brand, border-brand |
--font- |
--font-heading: Georgia, serif |
font-heading |
--radius- |
--radius-card: 20px |
rounded-card |
--breakpoint- |
--breakpoint-3xl: 90rem |
a 3xl: prefix (from 1440px) |
A token can also be used as a plain CSS variable: in your own CSS you can write color: var(--color-brand). In the Playground, which loads Tailwind from a CDN, write @theme inside <style type="text/tailwindcss">.
Adding or replacing
@theme adds to the built-in theme. After --color-brand is registered, every built-in colour is still there. So bg-blue-500 still works.
Want to remove the whole built-in palette and use only your colours? Write --color-*: initial; first:
@theme {
--color-*: initial;
--color-brand: #f97316;
}
--color-* means every token whose name starts with --color-, and initial removes them all. Now the only colour left is brand. bg-blue-500, bg-slate-800, even text-white no longer generate any CSS. Bracket classes such as bg-[#1e293b] still work, because they don't use a token.
Older projects: tailwind.config.js
In Tailwind v3, and in many existing projects, these settings are written in a JavaScript file called tailwind.config.js (or tailwind.config.ts):
// tailwind.config.js (Tailwind v3)
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {
colors: {
brand: "#3B82F6",
},
},
},
}
contentis the list of files Tailwind reads to find classes. In v4, those files are found automatically.theme.extend(extendinsidetheme) adds to the built-in theme: Tailwind makesbg-brandandtext-brand, andbg-blue-500stays.- Without
extend, such astheme: { colors: { brand: "#3B82F6" } }, you replace the whole palette.bg-blue-500is gone. - The value can be hex,
rgb(...)orhsl(...). It's the same idea as in v4:extendadds, while writing values straight intothemereplaces.
v4's automatic file search skips the node_modules folder (where the libraries you install are kept), files listed in .gitignore, images, and CSS files. If your classes are in a UI library in node_modules, register its folder with @source. Without that line, the library's classes get no CSS:
@import "tailwindcss";
@source "../node_modules/@acmecorp/ui-lib";
One-off values: square brackets
This is the exercise's card, written with square brackets:
<div id="card" class="p-6 rounded-xl shadow-lg bg-[#1e293b]">
<h2 id="title" class="text-xl font-bold text-white">Custom Card</h2>
<p id="desc" class="mt-2 text-[#94a3b8]">With custom colours</p>
</div>
| Class | CSS it generates | Meaning |
|---|---|---|
p-6 |
padding: 1.5rem; |
24px of padding |
rounded-xl |
border-radius: 0.75rem; |
12px corners |
shadow-lg |
box-shadow: 0 10px 15px ...; |
a large shadow |
bg-[#1e293b] |
background-color: #1e293b; |
your own dark blue |
text-xl |
font-size: 1.25rem; |
20px text |
font-bold |
font-weight: 700; |
bold |
mt-2 |
margin-top: 0.5rem; |
8px top margin |
text-[#94a3b8] |
color: #94a3b8; |
a bluish grey |
Brackets suit a value you use once. If #1e293b is used on many cards, make it a token. That's what you do in the exercise: --color-card: #1e293b; and --color-muted: #94a3b8; in @theme, then bg-card and text-muted. The exercise editor loads Tailwind v4 from a CDN, like the Playground, so its @theme goes inside <style type="text/tailwindcss">.
⚠️ A brand colour that won't show, or a palette that's gone
- Forgetting the namespace prefix.
--brand: #f97316in@themedoesn't makebg-brand. Tailwind only makes classes from tokens with a prefix such as--color-. There's no error: the class just quietly generates no CSS. - Forgetting
extendin a v3 config.theme: { colors: {...} }removes the built-in palette. Put your colours intheme.extend.colors. @themein a plain<style>. Tailwind loaded from a CDN, in the exercise editor or the Playground, only reads@themeinside<style type="text/tailwindcss">. In a plain<style>,@themeis ignored andbg-cardgenerates no CSS at all.- The wrong numbers in the exercise. The checker looks for: the padding of
#cardis24px(p-6), theborder-radiusof#cardis12px(rounded-xl), thefont-weightof#titleis700(font-bold, notfont-semibold), and the colours come from tokens: the background of#cardis#1e293bthroughbg-card, the text of#descis#94a3b8throughtext-muted.
TL;DR: A theme token is a design value you name yourself, such as a brand colour. In Tailwind v4, tokens are written in the CSS file: @import "tailwindcss"; then @theme { --color-brand: #f97316; }, and Tailwind makes the classes bg-brand, text-brand and border-brand right away. The token prefix decides the class: --color- for colours, --font- for fonts, --breakpoint- for screen prefixes such as md:. @theme adds to the built-in theme (Tailwind's ready-made values), while --color-*: initial replaces the whole palette. In a v3 project, the place is the file tailwind.config.js: theme.extend adds, theme without extend replaces. A one-off value only needs square brackets, such as bg-[#1e293b]. In the exercise: the tokens --color-card and --color-muted in @theme, used as bg-card and text-muted, plus p-6 (24px), rounded-xl (12px) and font-bold (700).
⚡ Give your colour a name
Write:
@import "tailwindcss";
@theme {
--color-brand: #f97316;
--font-heading: Georgia, serif;
}
Meaning: The @theme block holds tokens: design values you give a name, written like CSS variables. --color-brand uses the --color- prefix, so Tailwind makes the classes bg-brand, text-brand and border-brand. --font-heading becomes the class font-heading.
⚡ The prefix is required
Guess first: you write --brand: #f97316 in @theme, with no --color-. Does the class bg-brand work?
No. Tailwind only makes classes from tokens with a prefix like --color-. There's no error: the class just quietly makes no CSS.
Meaning: @theme adds to the built-in theme (Tailwind's ready-made values), so bg-blue-500 is still there. Only --color-*: initial removes the whole built-in colour palette.
⚡ One-off or repeated
Meaning: A colour used once only needs square brackets, like bg-[#1e293b]. If many cards use that colour, make it a token in @theme.
In the exercise, register --color-card: #1e293b; and --color-muted: #94a3b8; in @theme, inside <style type="text/tailwindcss">, then use bg-card and text-muted.