Sub topic 2
Setting up Tailwind and how it generates CSS
This chapter assumes you know basic HTML, core CSS concepts such as the box model and specificity, and how to run npm commands. It covers installing Tailwind v4, and how it turns the class names in your files into a stylesheet.
Installing Tailwind
Tailwind v4 works as a PostCSS plugin, a Vite plugin, or a standalone CLI. The CLI needs the fewest moving parts. Install it in your project:
npm install tailwindcss @tailwindcss/cliCreate src/input.css with one line:
@import "tailwindcss";Then build the stylesheet. The --watch flag rebuilds on every save:
npx @tailwindcss/cli -i ./src/input.css -o ./dist/output.css --watchLink the output file from your HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="./dist/output.css" rel="stylesheet" />
<title>Tailwind setup</title>
</head>
<body>
<h1 class="p-6 text-3xl font-bold text-indigo-600">Hello, Tailwind</ié>
</body>
</html>How CSS is generated
Tailwind does not ship a large prebuilt stylesheet. It builds one that contains only the utilities you use. The build has three steps:
- Tailwind scans your project files as plain text. It does not parse HTML or run JavaScript.
- It collects every string that looks like a class name, such as
p-6orhover:bg-indigo-700. - For each string that matches a known utility, it writes the matching CSS rule to the output file.
Because only matched utilities are written, the output stays small. Your unused classes cost nothing.
What the output contains
For the heading above, the output includes rules like these, shown here in simplified form:
.p-6 {
padding: calc(var(--spacing) * 6);
}
.text-3xl {
font-size: var(--text-3xl);
}
.font-bold {
font-weight: var(--font-weight-bold);
}The rules use CSS variables generated from the theme. This is why a later chapter can change the whole design by editing tokens instead of individual rules.
Detection is textual
The scanner reads text, so a class name only counts if it appears in full in a scanned file. Tailwind ignores files listed in .gitignore and binary files. It also scans your project from the working directory by default.
This means three practical rules:
- A class in a comment or a string is still detected, even if no element uses it. The output may contain a few extra rules. That is harmless.
- A class assembled at runtime, such as `
text-${color}-600`, is never detected. A later chapter covers this in detail. - If styles are missing, check that the file containing the class is inside the scanned area.
Common setup errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Page has no styling at all | The HTML links to the wrong path, or the build never ran | Check the `href` and rerun the build command |
| One class has no effect | The class name is misspelled or built dynamically | Write the full name literally |
| Styles appear only after a manual rebuild | The build is not in watch mode | Add `--watch` or use your framework's dev server |
| Classes work in some files only | Those files are outside the scanned area or are gitignored | Move them, or register the path with `@source` |
To scan an extra directory, add a @source line to your CSS:
@import "tailwindcss";
@source "../node_modules/my-ui-library";Exercise
You add class="bg-${tone}-500" in a JavaScript template string, and the background never appears. Why, and what is the fix?
Show answer
Tailwind scans files as plain text and never evaluates the template, so it sees only the fragment bg- and never the full class name. Write every complete class name literally, for example by mapping each tone to a full string like 'bg-red-500' in an object, and look the string up at runtime.
Exercise
Your build runs without errors, but classes used in a component under node_modules/my-ui-library have no styles. What do you add to your CSS?
Show answer
Add @source "../node_modules/my-ui-library"; after the import, adjusting the relative path to your CSS file. Tailwind skips node_modules by default, so it must be told to scan that directory.