Lesson 33 of 52Tailwind CSS
Tailwind Setup and Tooling
Install Tailwind v4 with Vite, Next.js or the CLI, understand what the import and Preflight do, and set up the editor tools that catch mistakes.
In the last lesson you got Tailwind running with Vite in four steps. That is enough to start, but sooner or later something will feel wrong: your h1 looks like plain body text, a class you typed does nothing, or a component from another folder comes out unstyled. Every one of those has a simple cause once you know what Tailwind does behind the scenes. This lesson looks at the three common ways to install Tailwind, what the single @import line actually pulls in, the reset called Preflight, and the editor tools that make Tailwind pleasant to write. It ends with a checklist for the moment a class "does nothing".
Three ways to install
Tailwind version 4 is one engine with small adapters for different build tools. Pick the adapter that matches your project. The CSS side never changes: one line, @import "tailwindcss";.
Vite is the common choice for plain HTML sites, React, Vue and Svelte apps. The Vite plugin is the fastest option.
npm install tailwindcss @tailwindcss/vite// vite.config.js
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [tailwindcss()],
});/* src/style.css */
@import "tailwindcss";Next.js builds CSS with PostCSS, a tool that runs plugins over your CSS files. Tailwind plugs in as a PostCSS plugin.
npm install tailwindcss @tailwindcss/postcss postcss// postcss.config.mjs
const config = {
plugins: {
"@tailwindcss/postcss": {},
},
};
export default config;/* app/globals.css */
@import "tailwindcss";The root layout, app/layout.tsx, imports ./globals.css once, and every page can use classes. New projects made with create-next-app can set all of this up for you if you say yes to Tailwind.
No bundler at all? A single index.html and a CSS file? Use the standalone command line tool. It watches your files and writes a finished CSS file.
npm install tailwindcss @tailwindcss/clinpx @tailwindcss/cli -i ./src/input.css -o ./src/output.css --watchinput.css holds @import "tailwindcss";. Your HTML links the output file: <link href="./src/output.css" rel="stylesheet">. For production, run it once with --minify instead of --watch. Tailwind also publishes standalone executables that need no Node.js; see the installation docs if you want one.
All three do the same job: watch your source files, find class names, and write CSS. The difference is only who calls Tailwind (Vite, PostCSS or the CLI itself).
@tailwindcss/postcss?A Vite project, file by file
Here is a slightly fuller Vite project than last time, with the editor and formatter files you will add later in this lesson.
- my-site/
- index.htmlyour markup and classes
- package.jsontailwindcss, @tailwindcss/vite, prettier plugin
- vite.config.jsplugins: [tailwindcss()]
- .prettierrcsorts classes on save
- .gitignorenode_modules, dist: never scanned
- .vscode/
- extensions.jsonrecommends Tailwind CSS IntelliSense
- src/
- main.jsimport "./style.css"
- style.css@import "tailwindcss"; plus @theme
- components/
- card.jsclasses here are scanned too
- dist/build output, ignored
What @import "tailwindcss"; pulls in
That one line is a shortcut. Expanded, it is roughly this:
@layer theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/preflight.css" layer(base);
@import "tailwindcss/utilities.css" layer(utilities);It uses cascade layers (@layer), a CSS feature that sorts rules into named groups. A rule in a later layer beats a rule in an earlier layer, no matter how specific the selectors are. So a utility class always beats the reset, and you never fight specificity between them.
theme defines the design tokens as CSS variables on :root, like --color-sky-500 and --spacing. Utilities read from them.
You can watch that last rule bite. The paragraph below asks for blue text with text-sky-700, but a plain CSS rule says red:
Preview
The paragraph stays red even though a class is more specific than a p selector, because unlayered rules sit above every layer. Change the <style> to <style type="text/tailwindcss"> and wrap the rule in @layer base { ... }: now the utility wins. When you mix your own CSS with Tailwind, put element defaults in @layer base and your own small classes in @layer components.
Preflight: why your headings look plain
Preflight is Tailwind's reset, built on top of a well known one called modern-normalize. It removes the browser's default styles so every element starts from the same place. The visible effects surprise people on day one:
h1toh6have the same size and weight as normal text.- Paragraphs, headings and lists have no margins.
ulandolhave no bullets or numbers.- Images and videos are
display: blockand never wider than their container. - Borders are reset to
0 solid, soborderalone draws a 1px line in the current text colour. - Buttons have no background or border.
Preview
This is on purpose. Browser defaults differ slightly between browsers, and a heading's visual size should come from your design, not from its level. An h2 in a sidebar might be small; an h1 on a landing page might be huge. The semantics stay in the tag, the look comes from classes.
If you want sensible defaults for raw elements back, add them yourself in the base layer:
@import "tailwindcss";
@layer base {
h1 { font-size: var(--text-3xl); font-weight: 700; }
a { text-decoration: underline; }
}For whole articles of Markdown or CMS content, the typography plugin (next lesson) is a better fit.
<h1>Welcome</h1> looks like normal text. What do you tell them?Telling Tailwind where to look: @source
Tailwind detects your source files automatically. It scans the project folder but skips anything in .gitignore (so node_modules and dist), binary files like images, and CSS files. That covers most projects. Two cases need a hint:
- A UI library inside
node_modulesthat ships Tailwind classes, such as your company's shared components. - A monorepo where your CSS file lives in one package and templates live in a sibling folder.
The @source directive adds a path to scan, relative to the CSS file:
@import "tailwindcss";
@source "../node_modules/@acme/ui";
@source "../../packages/shared/src";You can also exclude a folder with @source not "./legacy";, or set the starting folder for detection on the import itself with @import "tailwindcss" source("../src");. Newer releases also accept @source inline("...") to force specific classes to be generated even if no file contains them. Check the "Detecting classes in source files" page of the docs for the exact options in your version.
Match each line of CSS to what it does.
Editor tooling
Two tools turn Tailwind from "guess the class name" into something closer to autocomplete for CSS.
Tailwind CSS IntelliSense is the official VS Code extension (search for it in the Extensions view; the publisher is Tailwind Labs). Once installed, it finds the CSS file with your @import and gives you:
- Autocomplete for every class, including your own
@themetokens, with a colour swatch beside colour classes. - Hover previews: hover
px-4and you seepadding-inline: calc(var(--spacing) * 4);. - Linting: it underlines conflicts like
p-4 p-6on the same element, and typos in directives.
VS Code may also complain about "unknown at rule @theme" in your CSS. Set "files.associations": { "*.css": "tailwindcss" } in your workspace settings and the extension takes over CSS files too.
The Prettier plugin sorts classes into a consistent order every time you format: layout first, then box model, then typography, then colours, with variants like hover: and md: grouped after. It ends review arguments about ordering and makes duplicates easy to spot.
npm install -D prettier prettier-plugin-tailwindcss{
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindStylesheet": "./src/style.css"
}The tailwindStylesheet option points at your main CSS file so the plugin knows about your custom tokens. Turn on "format on save" in your editor and you never think about class order again.
Reading the generated CSS in DevTools
Your browser's DevTools are the final word. Right-click an element, choose Inspect, and the Styles panel shows each utility as a normal rule. Try it in the preview below: inspect the button.
Preview
You will see three useful things. The rule for .px-4 uses calc(var(--spacing) * 4), so you can trace any size back to a token. Colours appear as var(--color-indigo-600), which DevTools resolves to an oklch(...) value when you hover it. And each rule shows which layer it came from, so you can tell a utility apart from Preflight. If a declaration is crossed out, something else won, and DevTools shows you what.
When a class does nothing: a checklist
Run down this list in order. It catches almost every case.
- Typo or a class that does not exist.
bg-blu-500,text-center;,padding-4. Tailwind silently ignores names it does not know. IntelliSense underlines nothing for these, but it also offers no hover preview, which is your clue. - The class name is built from pieces.
`bg-${color}-500`is never seen as a whole name, so it is never generated. Map values to full names instead:{ red: "bg-red-500", green: "bg-green-500" }. - The file is not scanned. It lives in an ignored folder or a package outside the project. Add an
@sourceline. - Something else wins. An unlayered CSS rule, an inline
style, or a conflicting utility likep-4 p-6. DevTools shows the crossed out declaration. - The CSS file is not loaded. Nothing styled at all? Check that the page actually imports or links the stylesheet with
@import "tailwindcss";in it.
Preview
The button is unreadable and the paragraph is not centred. Fix text-center; (no semicolon), bg-blu-600 (bg-blue-600) and padding-4 (px-4 py-2).
One caution about these previews: they use Tailwind's in-browser build, which watches the live page, so a class built in a script would work here. A real build reads your files before any code runs, so it would not. Do not take "it works in the playground" as proof.
A class seems to do nothing. Put these checks in a sensible order, cheapest first.
- Tap the steps below in the right order.
- @tailwindcss/vite
- Tailwind's Vite plugin. The usual setup for Vite projects.
- @tailwindcss/postcss
- Tailwind as a PostCSS plugin, used by Next.js and other PostCSS-based tools.
- Preflight
- Tailwind's base reset. It removes default margins, heading sizes, list bullets and button styles.
- cascade layer
- A named group of CSS rules created with
@layer. Later layers win regardless of specificity; unlayered rules beat all layers. - @source
- A directive that tells Tailwind to scan an extra path, such as a library in
node_modules. - IntelliSense
- The official VS Code extension for Tailwind: autocomplete, hover to see CSS, and linting.
- prettier-plugin-tailwindcss
- A Prettier plugin that sorts Tailwind classes into a consistent order when you format.