Tailwind CSS
1. Why Tailwind?
Tailwind lets you style elements directly in HTML with small, ready-made classes.
CSS:
<h1 class="title">Hello</h1>.title { color: blue; font-size: 48px; font-weight: bold; }Tailwind:
<h1 class="text-blue-600 text-5xl font-bold">Hello</h1>Why developers love it:
- Faster development: no jumping between HTML and CSS files
- No naming: no more inventing names like .card-wrapper-inner-left
- No growing CSS file: your styles live where you use them
- Consistent design: the same spacing and colors everywhere
- Industry standard: it's the default choice in React and Next.js projects
2. Setup
Let's get Tailwind running in 4 steps.
1. Create a Vite project and install Tailwind
npm create vite@latest my-tailwind-app # Vanilla β JavaScriptcd my-tailwind-appnpm install tailwindcss @tailwindcss/vite2. Add the plugin in vite.config.js
import { defineConfig } from 'vite'import tailwindcss from '@tailwindcss/vite'
export default defineConfig({ plugins: [tailwindcss()] })3. Import Tailwind in src/style.css (delete everything else)
@import "tailwindcss";4. Run it
npm run dev- π‘ Install the Tailwind CSS IntelliSense extension in VS Code to get autocomplete
3. The Pattern
Learn this once, and you can read any Tailwind class.
{property}-{value}| Class | Property | Value |
|---|---|---|
| text-center | text | center |
| p-4 | padding | 4 (16px) |
| rounded-lg | rounded | lg |
| bg-red-500 | background | red-500 |
π Numbers = Γ 4px
p-4 = 16px Β· w-64 = 256px
π Sizes = t-shirt sizes
sm β md β lg β xl
4. Colors
One color system for text, backgrounds, and borders.
{text | bg | border}-{color}-{shade}<p class="text-red-500">Red text</p><div class="bg-slate-900">Dark box</div>| Option | Values |
|---|---|
| Colors | slate, gray, red, orange, yellow, green, blue, indigo, purple, pink |
| Shades | 50 (lightest) β 500 (normal) β 950 (darkest) |
| No shade | white, black |
| Opacity | bg-black/50 |
| Custom | bg-[#0F172A] |
5. Typography
Make your text readable and give it a clear hierarchy.
<h1 class="text-5xl font-bold text-center">Title</h1>| Property | Classes |
|---|---|
| Size | text-xs, sm, base, lg, xl, 2xl β¦ 9xl |
| Weight | font-light, normal, medium, semibold, bold |
| Align | text-left, text-center, text-right |
| Line height | leading-tight, leading-normal, leading-relaxed |
| Decoration | underline, no-underline |
| Transform | uppercase, lowercase, capitalize |
6. Make It Yours
Define your project's colors and fonts once, then use them everywhere.
In style.css:
@import "tailwindcss";
@theme { --color-midnight: #0B1026; /* background */ --color-snow: #F8FAFC; /* text */ --color-amber: #FBBF24; /* accent */
--font-poppins: "Poppins", sans-serif; --font-inter: "Inter", sans-serif;}Use them like any other class:
<body class="bg-midnight text-snow font-inter"> <h1 class="font-poppins text-amber">Hello</h1> <button class="bg-amber text-midnight">Click me</button></body>| You define | You get |
|---|---|
| --color-amber | bg-amber, text-amber, border-amber |
| --font-poppins | font-poppins |
- π‘ Set colors and fonts once on <body>, and everything inside inherits them
- π‘ Think of a night event: dark sky, white text, and amber light that draws you in
- π‘ Change a hex value once, and the whole site updates
- β οΈ Load the fonts from Google Fonts first
7. Box Model
Give your elements room to breathe.
{p | m}{side}-{size}| Side | Meaning |
|---|---|
| t, b, l, r | top, bottom, left, right |
| x | left + right |
| y | top + bottom |
| (none) | all sides |
Sizes: 0, 1, 2, 4, 6, 8, 12, 16, auto β π 1 = 4px
<div class="p-4 mt-8">Padding 16px, margin top 32px</div>Border
<div class="border border-white/10 rounded-2xl">Subtle card</div><footer class="border-t border-white/10">Line on top only</footer>| Property | Classes |
|---|---|
| Width | border (1px), border-2, border-4 |
| Side | border-t, border-b, border-l, border-r |
| Color | border-gray-200, border-amber |
| Opacity | border-white/10 |
- π‘ Always add border first. A color alone shows nothing
Corners and shadow
| Property | Classes |
|---|---|
| Corners | rounded-sm, md, lg, xl, 2xl, full |
| Shadow | shadow-sm, md, lg, xl |
- π‘ Tailwind already uses border-box for every element
8. Sizing
Control how wide and tall elements are.
| Property | Classes |
|---|---|
| Width | w-full, w-1/2, w-1/3, w-64 |
| Height | h-32, h-screen, min-h-screen |
| Max width | max-w-sm, md, lg, xl β¦ 7xl |
- π w-64 = 64 Γ 4 = 256px
- Fractions are percentages: w-1/2 = 50%
Max width sizes (π t-shirt sizes, not Γ 4):
| sm | md | lg | xl | 2xl | 3xl | 4xl | 6xl |
|---|---|---|---|---|---|---|---|
| 384px | 448px | 512px | 576px | 672px | 768px | 896px | 1152px |
Centered page container (you'll use this on every site):
<div class="max-w-4xl mx-auto">...</div>9. Flexbox
Place elements side by side, stack them, or center them.
<div class="flex justify-between items-center gap-4">...</div>| Tailwind | CSS |
|---|---|
| flex | display: flex |
| flex-row / flex-col | flex-direction |
| justify-start / center / end / between | justify-content |
| items-start / center / end | align-items |
| gap-4 | gap: 16px |
| flex-1 | flex: 1 |
| flex-wrap | flex-wrap: wrap |
Center anything:
<div class="flex justify-center items-center h-screen">...</div>10. States
Make your page react to the user.
<button class="bg-amber hover:bg-snow text-midnight transition">Hover me</button>| Prefix | When |
|---|---|
| hover: | Mouse over |
| focus: | Input selected |
| active: | Being clicked |
- π‘ Add transition to make the change smooth
11. Responsive
Make one design work on every screen.
Tailwind is mobile-first: a class with no prefix applies to all screens, and a class with a prefix applies from that size and up.
| Prefix | Screen |
|---|---|
| sm: | 640px+ |
| md: | 768px+ |
| lg: | 1024px+ |
| xl: | 1280px+ |
<div class="flex flex-col md:flex-row">Stacked on mobile, row on tablet+</div><nav class="hidden md:flex">Hidden on mobile, visible on tablet+</nav>- Design for mobile first
- Add md: and lg: for bigger screens
π Tailwind 80/20 Cheatsheet
The classes you'll use most, all in one place.
| Category | Classes | What it does |
|---|---|---|
| Text color | text-gray-700, text-blue-600, text-white | Color of the text |
| Background | bg-white, bg-gray-100, bg-blue-500 | Background color |
| Opacity | bg-black/50, text-white/70 | Make a color see-through |
| Font size | text-sm, text-base, text-xl, text-4xl | How big the text is |
| Font weight | font-medium, font-semibold, font-bold | How thick the text is |
| Align | text-left, text-center | Text alignment |
| Padding | p-4, px-6, py-2 | Space inside (1 = 4px) |
| Margin | mt-4, mb-8, mx-auto | Space outside, mx-auto centers a block |
| Border | border, border-t, border-gray-200, border-white/10 | Line around an element, or one side |
| Corners | rounded-md, rounded-lg, rounded-full | Rounded corners, full = circle or pill |
| Shadow | shadow-sm, shadow-md, shadow-lg | Drop shadow |
| Width | w-full, w-1/2, w-64 | Element width |
| Height | h-12, h-screen, min-h-screen | Element height |
| Max width | max-w-md, max-w-4xl | Width limit for containers |
| Flex | flex, flex-col | Turn on Flexbox, stack vertically |
| Justify | justify-between, justify-center | Arrange items on the main axis |
| Align items | items-center | Arrange items on the cross axis |
| Gap | gap-2, gap-4, gap-8 | Space between flex items |
| Hover | hover:bg-blue-700, hover:underline | Style when the mouse is over it |
| Smooth | transition | Animate the change |
| Responsive | md:flex-row, lg:text-5xl | Apply from that screen size and up |
| Hide/show | hidden, md:block, md:flex | Show or hide per screen size |