Skip to content

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.

18 min read0/4 questions

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";

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).

Quick check
You join a Next.js project and want to check how Tailwind is wired in. Which file is most likely to mention @tailwindcss/postcss?
Need a nudge? A hint rules out a wrong answer.

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.

A Vite + Tailwind project with tooling
  • 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.

The layers Tailwind creates, in cascade order
  1. unlayered CSSyour plain <style> rules: beat every layer
  2. utilitiesp-4, text-lg, hover:bg-sky-600
  3. componentsempty: for your own classes
  4. basePreflight, the reset
  5. theme--color-*, --spacing, --font-* variables

theme defines the design tokens as CSS variables on :root, like --color-sky-500 and --spacing. Utilities read from them.

1/5

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:

HTML + Tailwind
<style> /* plain CSS, in no layer */ p { color: crimson; }</style> <p class="text-sky-700 text-lg font-semibold">I asked for sky blue.</p><p class="mt-2 text-sm">Fix it by deleting the rule, or wrapping it in @layer base.</p>

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:

  • h1 to h6 have the same size and weight as normal text.
  • Paragraphs, headings and lists have no margins.
  • ul and ol have no bullets or numbers.
  • Images and videos are display: block and never wider than their container.
  • Borders are reset to 0 solid, so border alone draws a 1px line in the current text colour.
  • Buttons have no background or border.
HTML + Tailwind
<h1>A big heading?</h1><p>Body text right underneath, with no gap.</p><ul> <li>No bullets</li> <li>No indent</li></ul><button>Am I a button?</button> <h1 class="mt-6 text-3xl font-bold tracking-tight">Styled with classes</h1><ul class="mt-2 list-disc pl-5 text-slate-700"> <li>Bullets come back with list-disc</li></ul><button class="mt-3 rounded-md bg-slate-900 px-3 py-1.5 text-white">A real button</button>

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.

Quick check
A beginner says Tailwind is broken because <h1>Welcome</h1> looks like normal text. What do you tell them?
Need a nudge? A hint rules out a wrong answer.

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_modules that 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 the pairs0/4 matched

Match each line of CSS to what it does.

Choose an item, then its match on the other side.

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 @theme tokens, with a colour swatch beside colour classes.
  • Hover previews: hover px-4 and you see padding-inline: calc(var(--spacing) * 4);.
  • Linting: it underlines conflicts like p-4 p-6 on 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.

HTML + Tailwind
<button class="rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white shadow-sm hover:bg-indigo-500"> Inspect me</button><p class="mt-4 max-w-sm text-sm text-slate-600"> In the Styles panel, look for <code>.px-4</code>. Its value is written with <code>var(--spacing)</code>, and the variable is defined on <code>:root</code>.</p>

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.

  1. 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.
  2. 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" }.
  3. The file is not scanned. It lives in an ignored folder or a package outside the project. Add an @source line.
  4. Something else wins. An unlayered CSS rule, an inline style, or a conflicting utility like p-4 p-6. DevTools shows the crossed out declaration.
  5. 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.
HTML + Tailwind
<article class="max-w-xs rounded-xl border border-slate-200 p-6 shadow-md"> <h2 class="text-xl font-bold text-slate-900">Fix this card</h2> <p class="mt-2 text-slate-600 text-center;">Three classes below are wrong.</p> <button class="mt-4 rounded-lg bg-blu-600 padding-4 text-white">Subscribe</button></article>

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.

Put it in order4 steps

A class seems to do nothing. Put these checks in a sensible order, cheapest first.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.
Key terms
@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.