Lesson 32 of 52Tailwind CSS
Tailwind Basics
The utility-first idea, setting up Tailwind CSS v4 with Vite, and the core classes for spacing, colour, type and borders, read as plain CSS.
CSS Architecture ended with utility-first CSS. Tailwind is the most popular tool built on that idea. Tailwind CSS is a big set of small, ready-made utility classes. Instead of writing .card { padding: 1rem; border-radius: 0.5rem; } in a stylesheet, you write class="p-4 rounded-lg" in your HTML. It sounds like a step backwards the first time you see it, and then many people find they never want to go back. Tailwind is one of the most popular ways to style websites today, and you will meet it in job posts and codebases everywhere. This lesson explains the idea, sets it up in a real project, and teaches you to read any Tailwind class as the CSS it stands for.
The utility-first idea
In CSS Architecture you wrote a handful of utility classes by hand: .p-4 for padding, .flex for flexbox. Tailwind is that idea taken all the way. It gives you a utility for almost every CSS property and value you need, all built on a consistent design scale.
Here is a recipe card styled only with Tailwind classes. There is no <style> element at all.
Preview
Make some changes and watch the preview: p-6 to p-10, bg-slate-900 to bg-orange-600, rounded-xl to rounded-none, text-2xl to text-4xl. You are changing the design without leaving the HTML.
Why do people like this?
- No naming. You never stop to invent a name like
recipe-card__cta--primary. - No context switching. Structure and style sit in one place.
- Consistency. You pick from a scale (
p-4,p-6,p-8), not from any pixel value, so spacing and colours stay in harmony across the site. - The CSS stops growing. A new page reuses the same classes, so your stylesheet barely gets bigger.
- Safe changes. Changing a class on one element cannot break another page.
How Tailwind makes only the CSS you use
Tailwind can produce many thousands of possible classes, counting every colour, size and variant. Shipping all of them would be huge. So Tailwind does something clever: it scans your source files for class names, then generates CSS for only the ones it finds.
You write class="p-4 bg-white" in your templates. Tailwind treats your files as plain text.
This has one important consequence: class names must appear whole in your source code. Tailwind does not run your JavaScript, it only reads the text.
// Broken: Tailwind never sees "bg-red-500" or "bg-green-500"
const cls = `bg-${color}-500`;
// Works: both full names appear in the file
const cls = color === "red" ? "bg-red-500" : "bg-green-500";text-${size}, and size is lg. The text is not large. Why?Setting up Tailwind in a real project
The previews on this page load Tailwind straight in the browser, which is great for learning and quick experiments but not meant for real sites. In a real project, Tailwind runs as part of your build. The most common setup uses Vite, a fast development server and build tool. Tailwind version 4 needs no config file to start.
1. Create a Vite project and install Tailwind.
npm create vite@latest my-sitecd my-sitenpm install tailwindcss @tailwindcss/vite2. Add the plugin to vite.config.js.
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [tailwindcss()],
});3. Import Tailwind at the top of your main CSS file.
/* src/style.css */
@import "tailwindcss";4. Make sure that CSS file is loaded, either imported from your main JavaScript file (Vite templates already do this) or linked from index.html. Then run npm run dev, add a class like text-3xl font-bold to a heading, and it works.
- my-site/
- index.htmluse classes here
- package.jsonlists tailwindcss and @tailwindcss/vite
- vite.config.jsadds the tailwindcss() plugin
- src/
- main.jsimports style.css
- style.css@import "tailwindcss";
Other tools have their own small adapters: Next.js uses the PostCSS plugin @tailwindcss/postcss, and there is a command line tool, @tailwindcss/cli, for projects without a bundler. The CSS side is always the same single @import line.
Put the Vite setup steps in order.
- Tap the steps below in the right order.
The spacing scale
Almost every size in Tailwind comes from one number: the spacing unit, 0.25rem, which is 4 pixels at the default font size. A utility's number is how many units you get.
| Class | CSS | Pixels |
|---|---|---|
p-1 | padding: 0.25rem | 4 |
p-2 | padding: 0.5rem | 8 |
p-4 | padding: 1rem | 16 |
p-6 | padding: 1.5rem | 24 |
p-8 | padding: 2rem | 32 |
The same numbers work for padding (p), margin (m), gap (gap), width (w), height (h) and more. Add a side to target one direction:
px-4is left and right,py-2is top and bottom.pt,pr,pb,plare top, right, bottom and left.mt-4is a top margin,mx-autocentres a block horizontally.space-y-4puts space between stacked children.
With the default root font size of 16px, how many pixels of top padding does this paragraph get? Type a number.
<p class="pt-6 pb-2">Hello</p>
Colour, type and borders
Colour. Tailwind ships a palette of named colours (slate, gray, red, orange, amber, green, sky, blue, violet, pink and more), each with shades from 50 (palest) to 950 (darkest). Put a property prefix in front: text- for text colour, bg- for background, border- for border colour. Add a slash for opacity: bg-black/50 is black at 50% opacity.
Typography. text-sm, text-base, text-lg, text-xl up to text-9xl set font size (with a matching line height). font-medium, font-semibold and font-bold set weight. leading-relaxed sets line height, tracking-tight sets letter spacing, text-center aligns, uppercase transforms, italic slants.
Borders and shape. border adds a 1px border, border-2 a 2px one. In version 4 a plain border uses the current text colour, so add a colour like border-slate-200. rounded-md, rounded-lg, rounded-xl and rounded-full round corners. shadow-sm to shadow-xl add shadows.
Preview
Swap violet for emerald in every class to restyle the profile. Try bg-violet-100 as bg-violet-600 text-white for a bolder avatar, or rounded-full on the avatar as rounded-2xl.
Reading Tailwind classes as CSS
Every Tailwind class is a short name for one or two CSS declarations. Once you can translate them back, you can read any Tailwind codebase, and anything you know about CSS still applies.
| Tailwind | CSS |
|---|---|
flex | display: flex |
items-center | align-items: center |
mx-auto | margin-inline: auto |
w-full | width: 100% |
max-w-md | max-width: 28rem |
text-lg | font-size: 1.125rem (plus a line height) |
font-bold | font-weight: 700 |
bg-white | background-color: white |
hidden | display: none |
When you are not sure what a class does, inspect the element in the browser devtools: the Styles panel shows the real CSS rule, just like for hand-written CSS. Editor extensions like Tailwind CSS IntelliSense for VS Code also show the CSS when you hover a class, and suggest class names as you type.
Match each Tailwind class to its CSS.
- utility-first
- Styling by combining many small single-purpose classes in the HTML instead of writing custom CSS per component.
- Tailwind CSS
- A framework that generates utility classes from a design scale, shipping only the classes your files use.
- Vite
- A fast development server and build tool. Tailwind plugs into it with
@tailwindcss/vite. - spacing scale
- Tailwind's sizes are multiples of one unit (0.25rem, 4px by default):
p-4is 1rem. - palette
- Tailwind's named colours, each with shades from 50 (lightest) to 950 (darkest).
- opacity modifier
- A slash after a colour class, like
bg-black/50, that sets that colour's transparency.