What is Tailwind CSS?

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…

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:

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

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

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:

⚠️ Old habits that follow you into Tailwind

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.

What you will learn