Skip to content

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.

18 min read0/4 questions

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.

HTML + Tailwind
<article class="max-w-sm rounded-xl border border-slate-200 bg-white p-6 shadow-md"> <p class="text-sm font-semibold uppercase tracking-wide text-orange-600">Dinner</p> <h2 class="mt-1 text-2xl font-bold text-slate-900">Weeknight ramen</h2> <p class="mt-2 text-slate-600">Soy broth, soft eggs and crispy garlic in 25 minutes.</p> <button class="mt-4 rounded-lg bg-slate-900 px-4 py-2 font-semibold text-white"> Save recipe </button></article>

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.

From your files to a small CSS file
Your files: HTML, JSX, VueYour filesHTML, JSX, VueTailwind: scans for class namesTailwindscans for class namesOutput CSS: only used classesOutput CSSonly used classesBrowser: applies stylesBrowserapplies stylesreadgenerateload

You write class="p-4 bg-white" in your templates. Tailwind treats your files as plain text.

1/4

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";
Quick check
A React button builds its class with a template string, text-${size}, and size is lg. The text is not large. Why?
Need a nudge? A hint rules out a wrong answer.

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/vite

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

A Vite project with Tailwind
  • 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 it in order5 steps

Put the Vite setup steps in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

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.

ClassCSSPixels
p-1padding: 0.25rem4
p-2padding: 0.5rem8
p-4padding: 1rem16
p-6padding: 1.5rem24
p-8padding: 2rem32

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-4 is left and right, py-2 is top and bottom.
  • pt, pr, pb, pl are top, right, bottom and left.
  • mt-4 is a top margin, mx-auto centres a block horizontally.
  • space-y-4 puts space between stacked children.
Predict the outputHTML

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>
Need a nudge? Hints walk you through it.

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.

HTML + Tailwind
<div class="flex flex-col items-center gap-3 p-6"> <div class="flex size-20 items-center justify-center rounded-full bg-violet-100 text-2xl font-bold text-violet-700"> AM </div> <h2 class="text-xl font-semibold text-slate-900">Ana Moreno</h2> <p class="text-sm text-slate-500">Pastry chef, Lisbon</p> <p class="max-w-xs text-center leading-relaxed text-slate-700"> Teaches laminated dough on weekends and writes about butter far too much. </p> <a href="#" class="rounded-full border-2 border-violet-600 px-5 py-1.5 font-medium text-violet-700"> Follow </a></div>

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.

TailwindCSS
flexdisplay: flex
items-centeralign-items: center
mx-automargin-inline: auto
w-fullwidth: 100%
max-w-mdmax-width: 28rem
text-lgfont-size: 1.125rem (plus a line height)
font-boldfont-weight: 700
bg-whitebackground-color: white
hiddendisplay: 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 the pairs0/5 matched

Match each Tailwind class to its CSS.

Choose an item, then its match on the other side.
Key terms
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-4 is 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.