Sub topic 2
Configuring Tailwind in CSS
This chapter assumes you can write basic CSS, including custom properties and var(). It also assumes you have used Tailwind utility classes in HTML and know Tailwind is imported from a CSS file. You don't need to have used @theme yet. The next chapters cover it in detail.
What "CSS-first" means
In Tailwind v3, configuration lived in a JavaScript file, tailwind.config.js. It held your colors, fonts, content paths and plugins. A separate CSS file only contained the three @tailwind directives.
Tailwind v4 moves configuration into the CSS file. You customize Tailwind with CSS at-rules, special statements that start with @ and that Tailwind's compiler understands. Your main stylesheet is now both the entry point and the configuration.
This has practical effects:
- The design system sits next to the CSS that uses it.
- Design tokens (named values such as a brand color) become real CSS variables in the browser.
- Most projects no longer need a JavaScript config file.
The entry stylesheet
Every v4 project starts with one import:
@import "tailwindcss";This single line replaces the three v3 directives @tailwind base, @tailwind components and @tailwind utilities. Your build tool processes the file. Tailwind is available as a Vite plugin (@tailwindcss/vite), a PostCSS plugin (@tailwindcss/postcss) and a command-line tool (@tailwindcss/cli). Whichever you use, it reads this CSS file and generates the output stylesheet.
The import brings in three parts of Tailwind, each placed in a CSS cascade layer:
| Part | Layer | Contents |
|---|---|---|
| Theme | `theme` | The default design tokens as CSS variables |
| Preflight | `base` | Reset styles that remove browser defaults |
| Utilities | `utilities` | Classes such as `p-4` and `text-center` |
A cascade layer is a CSS feature (@layer) that sets priority between groups of rules, independent of selector specificity. Layers later in the order win over earlier ones. This is why a utility class beats a base style without needing a more specific selector.
You can import the parts separately if you want more control, for example to skip Preflight:
@layer theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/utilities.css" layer(utilities);This is the same as the single import, minus the reset styles. Most projects should keep @import "tailwindcss";.
The directives you configure with
Tailwind v4 adds a small set of at-rules. Together they cover what tailwind.config.js used to do.
| Directive | Purpose | v3 equivalent |
|---|---|---|
| `@theme` | Define design tokens that generate utilities | `theme` and `theme.extend` |
| `@source` | Add files Tailwind should scan for class names | `content` |
| `@plugin` | Load a plugin | `plugins` |
| `@utility` | Define a custom utility class | `addUtilities()` in a plugin |
| `@custom-variant` | Define a custom variant such as `dark:` | `addVariant()` or `darkMode` |
| `@config` | Load a legacy JavaScript config | the config file itself |
This chapter introduces the directives other than @theme and shows where they fit. @theme gets its own chapters.
A first look at @theme
@theme is the directive you will use most. Inside it, you declare CSS variables whose names follow Tailwind's naming rules. Tailwind turns those variables into utility classes.
@import "tailwindcss";
@theme {
--color-brand: #3f3cbb;
--font-display: "Satoshi", "sans-serif";
}After this, bg-brand, text-brand and font-display all work in your HTML. The naming rules, called namespaces, are the subject of a later chapter. For now, note that the configuration is plain CSS: no JavaScript object, no restart of a config loader.
Finding your class names: automatic detection and @source
In v3 you listed template paths in content. In v4 Tailwind finds your source files automatically. It scans your project, starting from the current working directory, and skips:
- files listed in
.gitignore, sonode_modulesand build output are ignored - binary files such as images and videos
- CSS files and common lock files
Tailwind reads each file as plain text and looks for strings that look like class names. It doesn't parse your code, so a class built by joining strings, like ` bg-${color}-500 `, is never found. Write complete class names in your source.
When a file is ignored but contains classes you need, add it with @source. A common case is a UI library installed in node_modules:
@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib";The path is relative to the CSS file that contains the directive. It can point to a file or a directory.
Loading plugins with @plugin
Plugins that ship as npm packages load with @plugin. Install the package first, then reference it by name:
@import "tailwindcss";
@plugin "@tailwindcss/typography";The typography plugin adds the prose classes. The directive replaces the plugins: [require(...)] array from v3.
Custom utilities and variants
A utility is a class that sets one thing, such as tab-4. Define one with @utility:
@import "tailwindcss";
@utility tab-4 {
tab-size: 4;
}Tailwind treats tab-4 like a built-in. It works with variants such as hover:tab-4 and md:tab-4, and it is included in the output only if a source file uses it.
A variant is a prefix such as hover: or dark: that applies a utility under a condition. Define your own with @custom-variant. This example makes dark: depend on a .dark class instead of the system color scheme:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));With this in place, dark:bg-black applies when the element or any ancestor has the dark class. A later chapter builds on this for runtime theming.
Using several directives together
A realistic entry file combines everything above:
@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib";
@plugin "@tailwindcss/typography";
@custom-variant dark (&:where(.dark, .dark *));
@theme {
--color-brand: #3f3cbb;
--font-display: "Satoshi", "sans-serif";
}
@utility tab-4 {
tab-size: 4;
}You can put your own ordinary CSS below these directives. The order that matters is that @import "tailwindcss"; comes first. Any other @import statements must also come before regular rules, as in all CSS.
Which build step reads this file
The CSS file does nothing until a build step processes it. The Vite plugin is the simplest setup for projects that use Vite:
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [tailwindcss()],
});The plugin has no Tailwind options. All Tailwind configuration lives in your CSS. Link or import the CSS file from your app as usual, for example import "./app.css" in your entry script.
Keeping a JavaScript config
Existing projects can keep their v3 config file by pointing to it from the CSS:
@import "tailwindcss";
@config "../../tailwind.config.js";This loads the legacy file for the theme and plugins it defines. Some v3 options are not supported in v4, including corePlugins, safelist and separator. A later chapter covers migration. For new work, prefer the CSS directives.
Common errors
- Utilities from your theme don't appear. The file containing
@thememust be processed by Tailwind. Check that it is the same file with@import "tailwindcss";, or that it is imported into that file. - Classes from a library have no styles. The library is in
node_modules, which is ignored by default. Add an@sourcefor it. - Dynamic class names are missing. Tailwind scans for complete strings. Replace `
text-${size}with a lookup of full class names such as"text-sm"and"text-lg"`. - v3 directives still in the file.
@tailwind base;and similar lines don't work in v4. Replace them with@import "tailwindcss";. - Config silently ignored. A
tailwind.config.jsis not loaded automatically in v4. Add@configif you still need it.
Exercise
Your project uses a component library installed in node_modules, and its Tailwind classes such as bg-brand produce no styles. Your CSS file already starts with @import "tailwindcss"; and defines --color-brand in @theme. What is the most likely cause, and what line fixes it? Assume the CSS file is at src/app.css and the library is @acme/ui.
Show answer
Automatic source detection ignores node_modules (it follows .gitignore), so Tailwind never scans the library for class names. Add @source "../node_modules/@acme/ui"; to src/app.css. The path is relative to the CSS file, so it goes up one level from src to the project root.
Exercise
Write the CSS that makes the dark: variant apply when an element or an ancestor has the class dark, and explain why dynamically built class names such as ` bg-${color}-500 ` fail.
Show answer
Use @custom-variant dark (&:where(.dark, .dark *)); after the import. Tailwind scans source files as plain text for complete class name strings. It doesn't run your code, so a name assembled at runtime never appears in the text and no CSS is generated for it. Write full class names in the source instead.