Sub topic 2
Configuring Tailwind in CSS
This chapter assumes you know basic CSS, including custom properties and var(). It also assumes you have used Tailwind utility classes in HTML and have installed Tailwind v4 in a project. Here you will see where configuration lives in v4 and what the main directives do. The next chapters cover @theme in detail.
From a JavaScript file to a CSS file
In Tailwind v3, you customized the framework in tailwind.config.js. You listed colors, fonts, and breakpoints in a JavaScript object under theme.extend.
Tailwind v4 moves this configuration into the CSS file that imports Tailwind. This is called CSS-first configuration. The design tokens (named values such as a brand color) sit next to the rest of your styles. They are written as CSS variables, so the browser can use them at runtime as well.
A minimal v4 entry file looks like this:
@import "tailwindcss";
@theme {
--color-brand: #3f3cbb;
}This file does two things:
@import "tailwindcss";loads Tailwind's base styles, its default theme, and its utilities.@theme { ... }adds your own tokens to that default theme.
Tailwind sees --color-brand and generates utilities such as bg-brand, text-brand, and border-brand. You never touch a JavaScript file.
How the pieces fit together
The build step reads the default theme from the import and merges in your @theme block. It then scans your templates for class names and writes only the utilities you use.
Directives you will meet
Tailwind adds a few CSS at-rules, called directives, that ordinary CSS does not have. Two matter for this tutorial:
| Directive | Purpose |
|---|---|
| `@import "tailwindcss"` | Loads Tailwind itself. Every project needs it once. |
| `@theme` | Declares design tokens that Tailwind turns into utilities and variants. |
Other directives exist, such as @config for loading an old tailwind.config.js. The last chapter of this tutorial covers that, so you can ignore it for now.
Common errors
- Editing a file the build never reads. Put
@import "tailwindcss";and your@themeblock in the CSS file that your build tool processes. A second stylesheet that is only linked in HTML does not go through Tailwind. - Nesting
@themeinside a selector or media query.@thememust sit at the top level of the file. Tailwind needs to read it as a global declaration, not as a rule that applies to some elements. - Using a name that matches no namespace.
--brand-coloris a valid CSS variable, but Tailwind generates no utility from it. The name must start with a known prefix such as--color-. The next chapters explain these prefixes. - Expecting a config file to be auto-detected. In v4,
tailwind.config.jsis not loaded automatically. You must opt in with@config.
Exercise
A teammate adds @theme { --color-sky-brand: #0af; } to a stylesheet that is only linked in the HTML <head>. It is not imported by the file your build tool processes. Why does bg-sky-brand do nothing, and how do you fix it?
Show answer
Tailwind only reads CSS that goes through its build step, so the @theme block is never seen. Move the block into the CSS file that contains @import "tailwindcss";, or import the stylesheet from that file. Then bg-sky-brand is generated.
Exercise
Which two lines make up the smallest useful Tailwind v4 configuration file with one custom color?
Show answer
@import "tailwindcss"; to load Tailwind, and a top-level @theme { --color-brand: #3f3cbb; } block to add the token. The block can span several lines, but it needs only these two pieces.