When you learned CSS, styling one element meant going back and forth. You made up a class name in the HTML, switched to the CSS file, wrote the rule, then switched back to the HTML. Tailwind CSS removes that trip. Tailwind is a CSS framework (a collection of ready-made code). It holds thousands of small classes, and each class does one thing: p-4 adds padding, bg-white gives a white background, rounded-lg rounds the corners. You don't write CSS rules yourself. You only put those classes together, right in the element's class attribute.
A small class that does one thing is called a utility. Building a style out of utilities is called utility-first. It is like eating at a food stall with the dishes on display. There you don't order "set meal number 7" and then check the menu to see what is in it. You point at the dishes one by one: rice, chicken, vegetables, chilli sauce. Your order shows what is on your plate. In Tailwind, each class is one dish, and the class attribute is your order: the list of classes shows how the element looks. A class name you make up yourself, such as card, is like "set meal number 7", because you have to search the CSS file to see what is in it.
The same card, two ways
We will make a white card with 16px of padding and corners rounded by 8px. This is the plain CSS version:
<div class="card">My First Card</div>
<style>
.card {
padding: 16px;
background-color: white;
border-radius: 8px;
}
</style>
This is the Tailwind version:
<div class="p-4 bg-white rounded-lg">My First Card</div>
The two cards look exactly the same. The difference is how you write them:
- With plain CSS you have to think of a name (
card), then write its rule somewhere else, usually in a separate CSS file. To change the padding, you switch files, find.card, and change it there. - With Tailwind, the styling sits on the tag. You don't switch files and you don't make up class names. You read the HTML and you know right away how it looks.
One class, one CSS rule
Tailwind isn't magic. Each class becomes one ordinary CSS rule, which Tailwind writes for you:
| Class | The CSS Tailwind writes | Its value |
|---|---|---|
p-4 |
padding: calc(var(--spacing) * 4) |
1rem = 16px |
bg-white |
background-color: var(--color-white) |
#fff |
rounded-lg |
border-radius: var(--radius-lg) |
0.5rem = 8px |
text-white |
color: var(--color-white) |
#fff |
bg-blue-600 |
background-color: var(--color-blue-600) |
dark blue |
var(--spacing),var(--color-white)and the others like them are Tailwind's built-in CSS variables. A CSS variable is a value with a name, so it can be used in many places.--spacingholds0.25rem, sopadding: calc(var(--spacing) * 4)means0.25remtimes 4, which is1rem.- The first part of the class name says the property:
pfor padding,bgfor background,textfor text colour,roundedfor border-radius. - So the rule
.btn { padding: 1rem; background: #2563eb; color: white; border-radius: 0.5rem; }is the same as the classesp-4 bg-blue-600 text-white rounded-lg. The colour#2563ebis blue-600 in the palette (the list of colours) of Tailwind v3. v4 rewrote that palette in theoklchcolour format and made it a little brighter, but the name is stillblue-600.
The number in the class name
Before you continue, guess: how many pixels of padding does p-4 add?
The number in p-4 is not a number of pixels. It is a number of steps, and one step = 0.25rem = 4px. (The rem unit follows the browser's default font size: 1rem = 16px.)
| Class | Calculation | Result |
|---|---|---|
p-1 |
1 × 4px | 4px |
p-2 |
2 × 4px | 8px |
p-4 |
4 × 4px | 16px |
p-8 |
8 × 4px | 32px |
So the answer is: p-4 means 16px of padding on all four sides, not 4px. Margin, width, height and other spacing use the same scale. The next lesson covers this scale in full.
Installing Tailwind v4
To try it out, one script line in <head> is enough. That script loads Tailwind from a CDN (a public server that offers ready-made files). The Playground on this page already adds it for you:
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
This CDN script is only for learning. Do not use it on a live website (production): the Tailwind docs say the Play CDN is for development only.
A real project usually uses a build tool such as Vite: a program that turns the project's files into files that are ready to send to the browser. With Vite there are three steps. First, install tailwindcss and @tailwindcss/vite in a terminal:
npm install tailwindcss @tailwindcss/vite
Second, register Tailwind's plugin (an add-on program for Vite) in the file vite.config.js:
// vite.config.js
import { defineConfig } from "vite"
import tailwindcss from "@tailwindcss/vite"
export default defineConfig({
plugins: [tailwindcss()],
})
Third, import Tailwind in your main CSS file:
/* src/style.css */
@import "tailwindcss";
@import "tailwindcss";loads every part of Tailwind. v4 needs notailwind.config.jsfile.- At build time (when Vite processes your project), Tailwind reads your project's files as text and looks for the class names you wrote. Tailwind only makes CSS for the classes it finds. So of the thousands of classes available, only the ones you use are sent to the browser, and the CSS file stays small.
Vite isn't the only way. Other options:
| Way | Package | When to pick it |
|---|---|---|
| Vite (used above) | @tailwindcss/vite |
new projects, including React, Vue and Laravel. Pick this one if unsure |
| Tailwind CLI | @tailwindcss/cli |
plain HTML with no build tool: npx @tailwindcss/cli -i ./src/input.css -o ./src/output.css --watch |
| PostCSS | @tailwindcss/postcss |
projects that already use PostCSS, for example Next.js |
| Play CDN | @tailwindcss/browser |
only for trying things out |
🤔 Isn't this the same as the style attribute?
It looks similar, but there are three big differences:
- The values come from a scale.
styletakes any number (15px, 17px, 19px). Tailwind's classes guide you to use the same scale across the whole page, so the spacing stays tidy. - It can do conditions. A class can have a condition:
hover:bg-blue-700applies only while the pointer is over the element, andmd:p-8applies only on wide screens. Thestyleattribute can do neither hover nor screen sizes. Both are covered in later lessons. - The CSS is shared. The rule
.p-4is made once, then used by every element that has that class.
⚠️ Old habits that follow you into Tailwind
- Going back to
style="...".style="padding: 16px"does work, but it isn't Tailwind, and this exercise rejects thestyleattribute. Write the styling as classes:class="p-4". - Writing pixels in the class name. The class
p-16pxdoesn't exist, andp-16means 64px. Divide the number of pixels by 4: 16px becomesp-4. - Building a class name from pieces of text. An example is
"p-" + sizein JavaScript. When Tailwind reads your files, it does not find the textp-4there, so the CSS forp-4is never made. Write the class name whole. - Following a Tailwind v3 tutorial. Old tutorials start the CSS file with
@tailwind base;,@tailwind components;,@tailwind utilities;and need atailwind.config.jsfile. In v4 one line is enough:@import "tailwindcss";.
TL;DR: Tailwind CSS is a utility-first CSS framework (a collection of ready-made code): it holds thousands of small classes (utilities), each class has one job, and you put the classes right in the class attribute. For example, p-4 bg-white rounded-lg gives 16px of padding, a white background and 8px corners, without writing your own CSS and without switching files. The number in a class name is a number of steps, and one step = 4px (0.25rem). So p-4 = 16px, not 4px. Each class becomes one ordinary CSS rule, for example p-4 becomes padding: calc(var(--spacing) * 4), which is one step (--spacing) times 4. Tailwind only makes CSS for the classes it finds written whole in your files. The CSS rule .btn { padding: 1rem; background: #2563eb; color: white; border-radius: 0.5rem; } is written p-4 bg-blue-600 text-white rounded-lg in Tailwind. In v4, the CSS file needs only @import "tailwindcss";, and to try Tailwind out, one script line from a CDN (a server of ready-made files) is enough.
⚡ The styling sits on the tag
Write:
<div class="p-4 bg-white rounded-lg">My First Card</div>
Meaning: This is Tailwind CSS: a collection of small ready-made classes for styling. p-4 adds padding, bg-white gives a white background, and rounded-lg rounds the corners. Each class does one thing. So you don't make up class names yourself, and you don't switch to a CSS file.
⚡ The number isn't pixels
Guess first: how many pixels of padding does the Tailwind class p-4 add?
16px, not 4px. The number in the class name is a number of steps, and one step = 0.25rem = 4px.
Meaning: Multiply the number by 4: p-2 = 8px, p-8 = 32px. And if you want 16px of padding, divide 16 by 4. The result is p-4.
⚡ One class, one CSS rule
Meaning: Tailwind writes ordinary CSS for you: the class p-4 becomes the rule padding: calc(var(--spacing) * 4), which is one step (--spacing, 4px) times 4: 16px of padding. Tailwind reads your files and only makes CSS for the class names it finds written whole. So write class names whole, never built from pieces of text such as "p-" + size.
This exercise rejects the style attribute. Write the styling as classes.