Sub topic 5
Import Tailwind in your CSS
This chapter assumes you have a Vite project with tailwindcss and @tailwindcss/vite installed, and that the plugin is registered in vite.config.ts. You should also be comfortable with CSS @import and with the idea that Vite treats CSS as part of the module graph. The next chapter covers loading the stylesheet from your app. Here the focus is the CSS file itself.
The one-line setup
In Tailwind v4, the CSS entry file needs one line:
@import "tailwindcss";Put it in the stylesheet your app will load, for example src/style.css. The name does not matter, as long as the file you import from JavaScript or HTML is the one that contains this line.
This line replaces the three directives that v3 projects used:
@tailwind base;
@tailwind components;
@tailwind utilities;Those v3 directives are gone in v4. If you leave them in the file, Tailwind will not generate your utilities. The v4 line is a standard CSS @import statement, and Tailwind gives it special treatment.
What the import does
@import "tailwindcss" is not a link to a file you can open and edit. When the @tailwindcss/vite plugin sees a CSS file that imports Tailwind, it resolves that import to the package's own stylesheet and builds the final CSS from it. That stylesheet organizes everything into CSS cascade layers. This is roughly its shape:
@layer theme, base, components, utilities;
@import "./theme.css" layer(theme);
@import "./preflight.css" layer(base);
@import "./utilities.css" layer(utilities);You never write this yourself. It is useful to know because it explains how Tailwind behaves:
| Layer | Contents | Notes |
|---|---|---|
| `theme` | Design tokens as CSS variables, such as colors, spacing, and fonts | Values you define with `@theme` end up here |
| `base` | Preflight, Tailwind's reset | Removes default margins, sets `box-sizing`, and resets heading sizes |
| `components` | Reserved for component-level styles | Empty by default, available for your own rules |
| `utilities` | Classes such as `text-3xl` and `bg-blue-600` | Generated only for classes Tailwind finds in your source |
The layer order matters. Utilities come last, so they win over base styles and component styles without needing higher specificity. This is why a class="underline" on a heading overrides the reset.
The utilities layer starts out empty of specific classes. Tailwind scans your project files and adds a rule only for each class name it finds. Chapter 10 covers that scanning.
How the plugin handles the file
The data flow is short:
Two inputs reach the plugin: your CSS entry file and the class names it finds in your other files. The output is plain CSS that Vite serves in development and bundles in production. In development, Vite sends updates to the browser as you edit, so adding a new class to your markup updates the page without a full reload.
The plugin only transforms CSS files that contain Tailwind directives. A stylesheet that never mentions Tailwind passes through Vite unchanged.
Adding your own CSS
You can write ordinary CSS in the same file, below the import:
@import "tailwindcss";
@layer base {
a {
text-decoration-thickness: 2px;
}
}
.card-shadow {
box-shadow: 0 1px 3px rgb(0 0 0 / 0.2);
}The two rules behave differently:
- The rule inside
@layer basejoins Tailwind's base layer. Utility classes can still override it. - The
.card-shadowrule is not in any layer. In CSS, unlayered styles take priority over layered styles. This rule will beat a utility such asshadow-lgon the same element, regardless of the order in which the rules appear.
That second point surprises many people. If a utility class has no effect, check whether you have an unlayered rule targeting the same property.
Placement rules for the import
CSS has a strict rule: @import statements must come before any other rules, except @charset and @layer statements that only declare names. If you put a normal rule above the import, browsers ignore the import. Vite and Tailwind may also warn you. Keep the import on the first line of the file.
/* Wrong: the import comes after a rule */
body {
margin: 0;
}
@import "tailwindcss";/* Right */
@import "tailwindcss";
body {
margin: 0;
}A few other details:
- Single and double quotes both work. Use double quotes to match the rest of this tutorial.
- The semicolon is required. A missing semicolon can cause the next line to be swallowed into the import statement.
- Import Tailwind once per project, in one entry stylesheet. Importing it from several files duplicates the base styles and the generated utilities.
Other imports in the same file
You can combine the Tailwind import with other CSS imports. Place them all at the top, in the order you want them:
@import "tailwindcss";
@import "./typography.css";Vite resolves relative imports as usual. If a file is not Tailwind-aware, it is treated as regular CSS. A common structure is to keep one entry file and split custom rules into small files that the entry imports.
Special cases: CSS modules and component styles
The import works in a plain .css file that your app loads. Two situations behave differently:
- CSS modules (
*.module.css) and component<style>blocks (in Vue or Svelte, for example) are processed separately from your main stylesheet. They do not automatically see your theme variables or custom utilities. If you only need to read theme values there, Tailwind provides@referenceto import them without duplicating the CSS. Avoid putting@import "tailwindcss"in each component, since that would repeat the whole stylesheet. - Sass, Less, and Stylus are not designed to work alongside Tailwind v4. Tailwind v4 is built to be your preprocessor, with nesting and imports available natively. If a project requires one of them, you will need to plan carefully, or look at the PostCSS route covered in a later chapter.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Page looks unstyled and the heading is small, with default margins | The stylesheet is not loaded, or the import line is missing | Confirm the CSS file is imported from your app and contains `@import "tailwindcss";` |
| Page loads but utility classes do nothing, while the reset applies | The scan did not find your classes | Check where the class names are written. See the source detection chapter |
| `@tailwind` directives remain in the file and there is no output | v3 syntax in a v4 setup | Replace the three directives with the single import |
| Import ignored | Another rule appears before it | Move `@import "tailwindcss";` to the top |
| Error that Tailwind could not be resolved | The `tailwindcss` package is not installed | Run `npm install tailwindcss @tailwindcss/vite` |
| Utility is overridden by your own rule | An unlayered rule beats layered utilities | Move your rule into `@layer base` or `@layer components`, or remove it |
If the Vite plugin is missing from vite.config.ts, the browser may try to load a CSS file that still contains the raw @import "tailwindcss". It fails there, because browsers cannot resolve a bare package name. A network error for tailwindcss in the browser's developer tools is a strong sign that the plugin is not registered.
Check your work
After saving the file, your CSS entry should look like this at minimum:
@import "tailwindcss";With the dev server running, Vite should show no errors in the terminal or the browser console. The visible effects come in the next two chapters, when you load the stylesheet and add classes to markup.
Exercise
A teammate's project has body { margin: 0; } on line 1 of style.css and @import "tailwindcss"; on line 2. The page shows no Tailwind styles and the terminal shows no error. What is the likely cause, and how do you fix it?
Show answer
CSS requires @import statements to come before other rules, so browsers and the build ignore an import placed after the body rule. Move @import "tailwindcss"; to the first line of the file, and keep the body rule below it.
Exercise
You add .btn { background: gray; } to your stylesheet after the Tailwind import, and then use <button class="btn bg-blue-600">. The button stays gray. Why, and what are two ways to fix it?
Show answer
The .btn rule is unlayered, and unlayered CSS takes priority over rules in Tailwind's utilities layer, so bg-blue-600 loses. Either wrap the rule in @layer components { ... } so utilities can override it, or remove the conflicting property from .btn.
Exercise
What does the single line @import "tailwindcss"; replace from the v3 setup, and why does it not work to keep both?
Show answer
It replaces the @tailwind base;, @tailwind components;, and @tailwind utilities; directives. The v3 directives are removed in v4, so Tailwind does not process them. The leftover lines produce no output and add confusion.