Sub topic 5
Import Tailwind in your CSS
This chapter assumes you have a Vite project with tailwindcss and @tailwindcss/vite installed, and the plugin registered in vite.config.ts. You should also be comfortable with basic CSS and the @import rule.
What this step does
The Vite plugin does nothing until your CSS asks for Tailwind. The request is a single line in your main stylesheet. When the plugin sees it, Tailwind generates the base styles, the theme variables, and the utility classes your project uses.
Create or open the stylesheet your app will load, for example src/style.css, and put this in it:
@import "tailwindcss";That is the whole setup for a typical project. You don't need a tailwind.config.js file and you don't need a separate PostCSS configuration.
How it replaces the v3 directives
In Tailwind v3, you wrote three directives:
@tailwind base;
@tailwind components;
@tailwind utilities;In v4, the single @import "tailwindcss"; line replaces all three. Don't keep both forms in the same file. The old @tailwind directives are not the v4 way to load the framework, and mixing them can cause confusing results.
What the import brings in
The import pulls in the parts of Tailwind in a fixed order, so later layers can override earlier ones.
| Layer | Purpose |
|---|---|
| theme | Design tokens such as colors, fonts, and spacing, defined as CSS variables |
| base | A reset (Preflight) and default element styles |
| components | A place for component classes you write yourself |
| utilities | Classes such as `text-3xl`, `font-bold`, and `underline` |
Tailwind does not write every possible class into the output. It scans your project files and emits only the utilities it finds in use. A later chapter covers how that scanning works.
Adding your own CSS
You can add your own rules after the import. Anything you write below it follows the Tailwind layers in the cascade:
@import "tailwindcss";
.page-title {
letter-spacing: 0.02em;
}Keep the @import "tailwindcss"; line at the top of the file. CSS requires @import rules to come before other rules, and a misplaced import is ignored by the browser's rules and may cause build warnings.
Common mistakes
- Using the v3 directives. If you copied
@tailwind base;from an older tutorial, replace it with the single import. - Typing a path. Write
"tailwindcss"exactly, with no./and no file extension. It is a package name that Vite resolves fromnode_modules, not a file in your project. - Importing in the wrong file. The import only has an effect in a stylesheet that your app actually loads. Creating
style.cssis not enough. The next chapter connects it to your entry file. - Skipping the plugin. Without
tailwindcss()invite.config.ts, Vite tries to treat the import as a plain CSS import, and no utility classes are generated.
Check your work
At this point, src/style.css should contain the import line, and nothing is visible in the browser yet because the stylesheet is not loaded. If you run npm run dev now, it should start without errors. The visual check comes after you load the stylesheet.
Exercise
A teammate's stylesheet starts with @tailwind base;, @tailwind components;, and @tailwind utilities;, but the project uses Tailwind v4 with the Vite plugin. What should the file contain instead, and why must the plugin also be registered?
Show answer
Replace the three directives with @import "tailwindcss";. The import only names the framework. The @tailwindcss/vite plugin, registered in vite.config.ts, is what processes it and generates the utility classes. Without the plugin, no utilities are produced.
Exercise
Why should you write @import "tailwindcss"; and not @import "./tailwindcss";?
Show answer
tailwindcss is a package name that is resolved from node_modules. A path starting with ./ points to a file in your own project, which does not exist.