Lesson 10
18 min
Add colors, fonts and spacing with the @theme directive.
Definition
Tailwind's theme is the set of design values available in your project: colors, fonts, spacing, border radii, breakpoints... Customizing it means adding or changing these values with the@theme directive in your CSS file: eachtheme variable you declare automatically generates the matching utility classes.
Tailwind ships with a very complete default theme, but every project has its own brand colors, its own fonts or its own spacing. Since version 4, all customization is done directly in CSS, thanks to the @themedirective.
Before version 4, you had to edit a JavaScript file (tailwind.config.js). Now everything is set in the same place as your CSS, which is simpler to read and maintain.
In your main CSS file, you can define theme variables that automatically generate new utility classes:
@import "tailwindcss";
@theme {
--color-brand: #7c3aed;
--color-brand-light: #a78bfa;
--font-display: "Poppins", sans-serif;
--spacing-gutter: 1.5rem;
}Once these variables are declared, you can directly usebg-brand, text-brand-light,font-display or p-gutter in your HTML, exactly like any native Tailwind class.
<button class="bg-brand hover:bg-brand-light font-display px-gutter py-3 rounded-lg text-white">
Button with the brand color
</button>The rule is simple: the beginning of the variable name indicates the category, and the end becomes the class name. So --color-brandgives bg-brand, text-brand andborder-brand, while --font-display givesfont-display.
Another advantage: theme variables are real CSS variables. You can therefore reuse them in plain CSS with var(--color-brand), for example to style an element you can't modify directly.
Adding a new variable in @theme (like--color-brand above) extends the existing theme: all of Tailwind's default colors (indigo, slate, emerald...) remain available. If you would rather start from an empty scale for a specific category (for example, to define all available colors yourself), you can reset the matching variables with the initial keyword before starting from scratch. This is a more advanced operation, best kept for very strict design systems.
@theme {
--color-*: initial;
--color-brand: #7c3aed;
--color-white: #ffffff;
}With this configuration, only the brand andwhite colors still exist: bg-indigo-500 will no longer work.
--color-*: adds colors (generates bg-*, text-*, border-*...)--font-*: adds font families (generates font-*)--spacing-*: adds spacing values (generates p-*, m-*, gap-*...)--breakpoint-*: adds or changes responsive breakpoints--radius-*: adds or overrides border-radius valuesOther categories exist, such as --shadow-* for shadows and--text-* for font sizes. As for spacing, Tailwind 4 already computes every numeric value from a base unit: a class likep-18 works even without any declaration. Declaring a variable is still useful to give a meaningful name to a value, like--spacing-gutter above.
Let's imagine a brand with a purple main color, a specific heading font and very rounded corners for its components. Note that--radius-2xl already exists in the default theme, so declaring it overrides the default value:
@import "tailwindcss";
@theme {
--color-brand-50: #f5f3ff;
--color-brand-500: #7c3aed;
--color-brand-700: #5b21b6;
--font-heading: "Sora", sans-serif;
--radius-2xl: 1.25rem;
}<div class="bg-brand-50 rounded-2xl p-6">
<h2 class="font-heading text-brand-700 text-2xl font-bold">
Welcome to our platform
</h2>
</div>You can also create your own breakpoint. By declaring a--breakpoint-* variable, you get a new responsive prefix:
@theme {
--breakpoint-3xl: 120rem;
}You can then write 3xl:grid-cols-6 to apply a class only from 1920px of width and up (120rem).
Declaring --font-heading in @theme tells Tailwind which family to use, but it doesn't download the font. You also have to load it, for example with a Google Fonts link in your page's<head>, or with an @font-face rule in your CSS. Without it, the browser will use the fallback font listed after the comma (sans-serif).
It is tempting to redefine the whole theme at the start of a project. In practice, it's better to start with Tailwind's default theme (already very polished and consistent), then only add the variables that your brand identity really needs, such as the brand color and the main font, as the project moves forward.
For a brand color, you usually define several shades, just like Tailwind does with its own colors. This lets you use light shades for backgrounds and dark shades for text:
@theme {
--color-brand-100: #ede9fe;
--color-brand-300: #c4b5fd;
--color-brand-500: #7c3aed;
--color-brand-700: #5b21b6;
--color-brand-900: #3b0764;
}You can then write bg-brand-100,hover:bg-brand-300 or text-brand-900, following the same shade logic as the native colors (from light to dark). To pick your shades, an online palette generator will save you time.
Since every theme variable is a CSS variable, you can read it in any CSS rule you write yourself, without duplicating the value:
.my-component {
border-left: 4px solid var(--color-brand-500);
font-family: var(--font-heading);
}If you change the value of --color-brand-500 in@theme, it is updated everywhere: in Tailwind classes as well as in your custom CSS.
One thing to know: by default, Tailwind 4 only outputs the theme variables that are actually used. If you need a variable to always be available as a CSS variable (for example, to read it from a component's<style> block or from JavaScript), declare your theme with@theme static instead of @theme.
brand or heading are more meaningful thanviolet-2.@theme in the main CSS file, so you can easily find each value.@import "tailwindcss"; and @theme is actually imported in your layout or page.--brandinstead of --color-brand doesn't generate any color class.@theme before the import: always keep @import "tailwindcss"; on the first line.Keep the core idea in mind: you don't have to choose between the default theme and a fully custom one. Most of the time, you keep Tailwind's solid foundation and simply add your visual identity on top, in a few lines of@theme.
Practical exercise
--color-brand variable to your CSS file with a color of your choice, then use bg-brand and text-brand on a button. Check that the class works as expected in your browser.