Sub topic 2
Setting up Tailwind and how it generates CSS
This chapter assumes you know basic HTML, the CSS box model, selectors and specificity, and how to run commands with npm. It covers how to install Tailwind CSS v4, how the build step produces your stylesheet, and the errors people hit most often when setting it up.
What Tailwind actually is
Tailwind is a build tool that reads your source files and writes a CSS file. It does not ship a large prebuilt stylesheet. Instead, it looks for class names in your files, matches each one to a rule it knows how to generate, and writes only those rules to the output.
This is why the output stays small. A project that uses 300 distinct classes gets CSS for those 300 classes, not for every utility Tailwind supports.
Installing Tailwind v4
There are three common ways to run Tailwind v4: as a Vite plugin, as a PostCSS plugin, or through the standalone CLI. The Vite plugin is the simplest if you already use Vite. The CLI works for plain HTML projects.
This example uses the CLI with a plain HTML project.
- Create a project folder and initialize npm.
- Install Tailwind and its CLI.
- Create an input CSS file.
- Run the build.
- Link the output file in your HTML.
mkdir tailwind-demo
cd tailwind-demo
npm init -y
npm install tailwindcss @tailwindcss/cli
mkdir srcCreate src/input.css with a single line:
@import "tailwindcss";Create index.html in the project root:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tailwind demo</title>
<link href="./dist/output.css" rel="stylesheet" />
</head>
<body class="bg-slate-100 p-8">
<h1 class="text-2xl font-bold text-indigo-600">Hello, Tailwind</h1>
</body>
</html>Build the stylesheet once, or keep it rebuilding as you edit:
npx @tailwindcss/cli -i ./src/input.css -o ./dist/output.css
npx @tailwindcss/cli -i ./src/input.css -o ./dist/output.css --watchOpen index.html in a browser. The heading appears bold, indigo, and larger than body text.
The build pipeline
The build has four steps. Understanding them explains most setup problems.
- Tailwind reads your input CSS, which pulls in its base layers and your own rules.
- It scans your project's source files as plain text.
- It extracts every string that could be a class name.
- It generates the CSS for each candidate that matches a known utility or variant, and writes the result.
Scanning is text matching
Tailwind does not parse your HTML or run your JavaScript. It treats each file as a string and pulls out anything that looks like a class name. A string such as "flex" inside a comment, a JSON file, or a Markdown file counts as a candidate just as much as a real class attribute.
Two consequences follow:
- A class name works in any file type that Tailwind scans, including
.js,.jsx,.vue,.svelte,.php, and.md. - A class name that never appears as a complete string in a scanned file produces no CSS.
The second point is the source of the most common bug, which a later chapter on dynamic class names covers in detail. For now, remember the rule: write complete class names.
What gets scanned
In v4, Tailwind detects sources automatically. It starts from the current working directory and skips several things on its own:
- Files and folders listed in
.gitignore - The
node_modulesdirectory - Binary files such as images and videos
- CSS files
- Lock files
This is why dist/output.css does not feed back into itself, and why node_modules does not slow the build.
Adding sources that are not detected
If you use classes from a package in node_modules, or from a folder your .gitignore excludes, register that folder with @source in your CSS:
@import "tailwindcss";
@source "../node_modules/some-ui-library/dist";The path is relative to the CSS file that contains the directive. You can also exclude paths with @source not:
@import "tailwindcss";
@source not "../legacy";What the generated CSS contains
Open dist/output.css after a build. You will see several sections inside CSS cascade layers.
| Layer | Contents |
|---|---|
| `theme` | CSS variables for the design tokens in use, such as colors and spacing |
| `base` | A reset (called Preflight) and default element styles |
| `components` | Empty by default, available for your own component classes |
| `utilities` | One rule per class that Tailwind found in your sources |
Cascade layers are a CSS feature that sets priority between groups of rules, regardless of selector specificity. Later layers win over earlier ones. Your utilities sit in the last layer, so a utility such as p-4 overrides a base style on the same element.
Preflight
Preflight is Tailwind's reset. It removes default margins from headings, paragraphs, and lists, sets box-sizing: border-box on all elements, and makes images block-level. This surprises people who expect <h1> to look large by default. Without text-2xl font-bold, a heading looks like plain body text.
Only used values are emitted
Tailwind writes the CSS variable declarations for theme values only when a utility uses them, and each utility is one small rule. Searching the output for text-indigo-600 shows something like this:
.text-indigo-600 {
color: var(--color-indigo-600);
}The variable --color-indigo-600 is defined in the theme layer, and it appears only because that class is in use. Delete the class from index.html, rebuild, and the rule disappears from the output.
Verifying your setup
Do this short check whenever a new project is set up.
- Run the watch command.
- Add
class="underline"to the heading inindex.html. - Save the file. The CLI rebuilds without you restarting it.
- Refresh the page. The heading is underlined.
- Search
dist/output.cssfor.underline. The rule is there.
If step 4 fails, the cause is almost always one of the cases in the next section.
Common setup errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No styles at all | The HTML links to the wrong path, or the build never ran | Check the `href` against the `-o` path, and run the build |
| Preflight applies but your utilities do not | The file with the classes is not scanned | Check `.gitignore`, or add an `@source` directive |
| Utilities work in some files but not others | The folder is git-ignored or outside the working directory | Add `@source` for that folder |
| New classes appear only after you restart | The watcher is not running | Use `--watch`, or the dev server for your tool |
| `@tailwind base` does nothing | You are using a v3 guide with v4 | Replace the directives with `@import "tailwindcss";` |
| `tailwind.config.js` seems ignored | v4 does not load it automatically | Move settings into CSS with `@theme` |
| A class works in development but not in production | The class name is built from fragments at runtime | Write the full class name in source |
Running from the wrong directory
Automatic detection starts at the directory where you run the command. If you run the CLI from a parent folder, it scans a different tree than you expect. Run it from the project root, or use @source with explicit paths.
Caching the old build
Browsers cache output.css. If your rebuilt file has the right rules but the page does not change, do a hard refresh, or open the browser's network tab and disable caching while developing.
Choosing a setup method
| Method | Best for | Trade-off |
|---|---|---|
| Vite plugin | New projects built with Vite, including React, Vue, and Svelte templates | Requires Vite |
| PostCSS plugin | Existing projects with a PostCSS pipeline, or frameworks that expect it | One more config file to maintain |
| Standalone CLI | Plain HTML, server-rendered pages, and quick experiments | You manage the watch process yourself |
All three use the same scanner and generator, so the utilities behave identically. Pick the one that matches your existing toolchain.
Production builds
For production, add the --minify flag to shrink the output:
npx @tailwindcss/cli -i ./src/input.css -o ./dist/output.css --minifyYou do not need a separate purge step. The scanning stage already limits the output to the classes in use, so there is nothing further to remove. A typical small site ends up with an output file of a few kilobytes to a few tens of kilobytes before compression.
Exercise
You add class="text-rose-500" to an element in src/pages/about.html, and the CLI is running with --watch. The color does not change. You then notice that src/pages is listed in .gitignore. What is the cause, and what is the fix?
Show answer
Tailwind v4 skips files and folders that .gitignore lists, so it never scanned about.html and generated no rule for text-rose-500. Add the folder explicitly in src/input.css with @source "./pages"; (the path is relative to the CSS file), or remove the folder from .gitignore if that is appropriate. After the next rebuild the rule appears in dist/output.css.
Exercise
A teammate says Tailwind "removes unused CSS from a big stylesheet" at build time. Describe more accurately how the output stays small.
Show answer
Tailwind does not start with a big stylesheet and trim it. It scans your source files as plain text, extracts candidate class names, and generates a rule only for each candidate that matches a known utility or variant. Classes that never appear in the scanned text are never generated.