Chapter 2 of 14 · Tailwind CSS for Beginners — Complete Free Tutorial
Installing and Configuring Tailwind
This chapter of the Tailwind CSS for Beginners series gets Tailwind running in a real project: which packages to install, the tiny CSS entry file everything starts from, and how Tailwind decides which classes to generate. By the end you will have a watch process that compiles a small, optimised stylesheet straight from your templates.
What you need first
Tailwind v4 is a Node-based build tool, so a couple of things should be in place before you start:
- Node.js 20 or newer — the tooling, including the official upgrade tool, expects a current Node release.
- A project folder — a plain folder of HTML pages works, and so does a Laravel, React, Vue or Astro app.
- Basic CSS knowledge — Tailwind generates CSS, so ideas like the box model and specificity from Introduction to CSS still apply.
Option 1: the Tailwind CLI
The standalone command line tool suits static sites and any framework where you just want a stylesheet at the end of the process. Initialise a project and install both packages — in v4 the CLI lives in its own package instead of the main one:
npm init -y
npm install tailwindcss @tailwindcss/cli
npx @tailwindcss/cli -i ./src/input.css -o ./dist/output.css --watch
Expected output:
done in 41ms
wrote dist/output.css, 8.6 kB
watching for changes…
In v3 the same command was npx tailwindcss -i … -o …, so older tutorials will name a package that no longer contains the CLI. Add --minify when you build for deployment to strip whitespace and comments.
Option 2: the Vite plugin
If your project already uses Vite — including Laravel's default frontend setup — the dedicated plugin is the smoothest route, because it hooks into the dev server and handles hot reloading for you:
npm install tailwindcss @tailwindcss/vite
npm run dev
Then register it in vite.config.js by importing tailwindcss from @tailwindcss/vite and adding tailwindcss() to the plugins array. A third option exists for older setups: the PostCSS plugin, which now ships as @tailwindcss/postcss rather than living inside the tailwindcss package as it did in v3.
The CSS entry file
However you build, Tailwind starts from one stylesheet that you create, and in v4 the whole framework arrives through a single import:
/* src/input.css */
@import "tailwindcss";
/* your own base styles can follow */
body {
font-family: var(--font-sans);
color: var(--color-slate-800);
}
That import expands into three parts: the theme layer holding every default token, the base layer holding Preflight (Tailwind's modern reset), and the utilities layer that your class names draw from. In v3 you wrote those as three separate directives — @tailwind base;, @tailwind components; and @tailwind utilities; — and they are gone in v4. Finally, link the file your build writes, never the input file:
<link rel="stylesheet" href="/dist/output.css">
Expected output:
The page gains Tailwind's reset — margins removed, headings
unbolded by default, images set to display block — and every
utility class used in your markup starts working.
How Tailwind finds your classes
Source detection is where beginners get stuck, because a class that is never scanned can never be generated. Tailwind v4 starts from the directory of your CSS file and walks up to the project root, scanning every source file it finds while respecting the folders listed in your .gitignore file, so node_modules and your build output are skipped automatically. You only intervene for special cases:
- Templates living in an unusual place:
@source "../admin/**/*.blade.php"; - Class names built at runtime, such as
"bg-" + color: declare them with@source inline("bg-red-400 text-center");so they are always generated. - Legacy JavaScript configuration: load it explicitly with
@config "../../tailwind.config.js";.
v4 configuration compared with v3
| Task | In v3 | In v4 |
|---|---|---|
| Load Tailwind | Three @tailwind directives | @import "tailwindcss"; |
| List files to scan | content: [...] in tailwind.config.js | Automatic, plus @source for exceptions |
| Add a colour, font or breakpoint | theme.extend in JavaScript | @theme { --color-brand: … } in CSS |
| Register a custom utility | @layer utilities { … } | @utility name { … } |
| Config file required? | Yes, it is the starting point | No, load a legacy one with @config |
Common mistakes
- Forgetting the import — an input file without
@import "tailwindcss";compiles to almost nothing, so every class appears broken. - Linking the wrong file — the browser must receive
output.css; the input file contains directives no browser understands. - Building class names by concatenation — the scanner reads literal strings, so
"text-" + sizewill not generatetext-lgunless you safelist it. - Expecting v3 behaviour — a
tailwind.config.jssitting in the folder is ignored in v4 until you load it with@config. - Targeting ancient browsers — v4 needs Safari 16.4, Chrome 111 or Firefox 128 or newer; older projects should stay on v3.4.
Practice checklist
- Create a project folder and run the CLI install commands from this chapter.
- Write
src/input.csscontaining only@import "tailwindcss";. - Build an
index.htmlthat uses three utilities and link it todist/output.css. - Run the build with
--watch, add a new class in the HTML, and confirm the output file grows. - Delete the import line, rebuild, and confirm the CSS shrinks to nothing — then put it back.
With a working build behind you, it is time to think the Tailwind way — The Utility-First Approach.
Frequently Asked Questions
Tailwind is a utility-first CSS framework: instead of writing custom classes like .card-title, you compose elements from small single-purpose utilities such as text-lg font-semibold text-slate-800. The styles ship as a generated stylesheet containing only the classes your project uses, so the CSS stays small and consistent.
Bootstrap ships a opinionated component layer (buttons, navbars with Bootstrap's own look) that you override with custom CSS. Tailwind ships primitive utilities and lets you design freely — two sites built with Tailwind can look completely different, while two Bootstrap sites tend to look related. You can still build components; you just compose them from utilities first.
You need the fundamentals: the box model, specificity, Flexbox, Grid, media queries and pseudo-classes. Tailwind does not replace CSS knowledge — it renames it. Understanding what padding, margin, gap and z-index mean makes every utility class obvious instead of something to memorise.
v4 is CSS-first: configuration lives in CSS with @import "tailwindcss" and @theme tokens instead of a JavaScript tailwind.config.js, the engine is rewritten in Rust for much faster builds, and modern CSS features (container queries, cascade layers) are built in. This tutorial teaches v4 and notes where v3 differed.
Long class lists are a real trade-off, and the tutorial covers two fixes: consistent ordering (layout → spacing → typography → colour) and small component classes for repeated combinations. The generated CSS contains only classes you actually use — Tailwind removes everything else at build time, so the stylesheet is typically smaller than an unused framework.
Three utilities cover most cases: mx-auto centres a fixed-width block horizontally, flex items-center justify-centre centres children inside a flex parent, and grid place-items-center does the same for grids. Chapter 8 and chapter 9 show both patterns with examples.
Yes — Tailwind is a build step that scans your template files for class names and generates CSS, so it works with any framework or none. It does not ship runtime JavaScript, and the classes look the same in JSX, Blade, PHP or static HTML files.
In v4 you define design tokens in CSS with @theme, for example --color-brand-500: #4f46e5; creates bg-brand-500, text-brand-500 and friends. For a one-off value you can always use arbitrary values like bg-[#0f172a], which need no configuration at all.
A variant prefixes a utility with the condition it applies under: hover:bg-indigo-600 on hover, focus:ring-2 while focused, dark:bg-slate-900 in dark mode, and sm: / md: / lg: at breakpoints. Variants stack, such as md:hover:text-lg, and chapter 11 covers the full pattern.
Yes. Tailwind compiles to a static stylesheet with no JavaScript requirement, and because it ships only used classes, pages stay fast — a Core Web Vitals win that helps SEO. Accessibility, semantic markup and good content remain your job; Tailwind only styles what you write.