The Complete Tailwind CSS 4 Guide and Cheat Sheet
By Flavio
Published · Updated
Tagged: CSS
Learn Tailwind CSS 4 from setup to production: spacing, typography, OKLCH colors, Flexbox, Grid, container queries, dark mode, @theme, and a full project.
If you know basic HTML and CSS and want to build clean, responsive interfaces without wrestling with giant stylesheets or inventing class names all afternoon, this guide is for you.
I was skeptical of Tailwind CSS when I first saw it. Putting a string of tiny classes directly inside HTML looked messy compared to the neat semantic CSS classes I had written for years.
Then I tried it on a real project. Within two days I stopped switching back and forth between HTML and CSS files, stopped worrying about whether deleting a rule would break another page, and shipped cleaner layouts in half the time.
This guide is an in-depth guide to Tailwind CSS, version 4. We start from zero with what Tailwind is and how to install it, then walk through Preflight, spacing, typography, colors, borders, Flexbox, CSS Grid, responsive design, interactive states, dark mode, @theme design tokens, arbitrary values, container queries, forms, animations, eight copy-and-paste UI recipes, a complete landing page project, ten hands-on exercises with solutions, and a CSS-to-Tailwind quick reference at the end.
Every CLI command, timing, and output size in this guide was tested live on my Mac:
| Software | Version |
|---|---|
Tailwind CSS (tailwindcss) | 4.3.5 |
| CLI & Vite plugins | @tailwindcss/cli 4.3.5, @tailwindcss/vite 4.3.5 |
| Node.js | 22.23.5 |
What is Tailwind CSS (and what changed in v4)?
When we style a web app with traditional CSS, we invent class names for every component and every tiny wrapper: .card, .card-header, .card-header-inner, .card-title-lg.
Over a few months, that stylesheet grows into an append-only file. Nobody feels safe deleting old rules because it is hard to prove which template still depends on .sidebar-box-alt.
Tailwind takes the opposite path. Instead of writing custom classes that bundle ten CSS properties together, we compose small utility classes right in our HTML where each class does one single job:
p-6setspadding: 1.5remrounded-xlsetsborder-radius: 0.75rembg-whitesetsbackground-color: #ffffffshadow-smadds a soft drop shadow
Let’s compare a small card written in traditional CSS versus Tailwind.
With plain CSS, we split the markup and styling across two files:
<!-- index.html -->
<article class="book-card">
<h2 class="book-card-title">Tailwind CSS Handbook</h2>
<p class="book-card-summary">Build responsive interfaces fast.</p>
</article>
/* styles.css */
.book-card {
max-width: 24rem;
padding: 1.5rem;
border: 1px solid #e2e8f0;
border-radius: 0.75rem;
background-color: #ffffff;
}
.book-card-title {
font-size: 1.125rem;
font-weight: 600;
color: #0f172a;
}
.book-card-summary {
margin-top: 0.5rem;
font-size: 0.875rem;
color: #475569;
}
With Tailwind, everything lives in one spot:
<article class="max-w-sm rounded-xl border border-slate-200 bg-white p-6">
<h2 class="text-lg font-semibold text-slate-900">Tailwind CSS Handbook</h2>
<p class="mt-2 text-sm text-slate-600">Build responsive interfaces fast.</p>
</article>
When you delete that card next month, its styles disappear automatically. Nothing lingers in a forgotten stylesheet.
How Tailwind CSS v4 builds your stylesheet
A common beginner worry is: “Does Tailwind ship thousands of unused CSS classes to my users?”
No. Tailwind v4 includes a Rust-powered engine called Oxide that scans your HTML, Astro, JSX, Vue, Svelte, or PHP files at build time and emits only the utility classes it actually saw in your code.
A typical production site ends up with a tiny 5 KB to 15 KB gzipped stylesheet, no matter how many pages you add.
What changed in Tailwind CSS v4
If you read tutorials written before 2025, you will see tailwind.config.js files and @tailwind base; directives everywhere. Tailwind v4 simplified almost all of that:
| Area | Tailwind v3 | Tailwind v4 |
|---|---|---|
| CSS entry | @tailwind base; @tailwind components; @tailwind utilities; | Single @import "tailwindcss"; |
| Configuration | JavaScript tailwind.config.js | Native CSS @theme { ... } right in your stylesheet |
| Template scanning | Manual content: ['./src/**/*.{html,js}'] glob array | Automatic project scanning (respects .gitignore) |
| Spacing scale | Fixed list of predefined numbers | Dynamic --spacing scale (any multiplier like p-13 works) |
| Color space | RGB / hex palette | Wide-gamut OKLCH palette exposed as CSS custom properties |
| Container queries | Required @tailwindcss/container-queries plugin | Built straight into core (@container, @md:) |
| Gradients | bg-gradient-to-r | bg-linear-to-r, bg-radial, bg-conic |
| Important modifier | Leading ! (!text-red-600) | Trailing ! (text-red-600!) |
Every example in this handbook uses clean, modern Tailwind CSS v4 syntax.
Installing Tailwind CSS v4
Let’s wire Tailwind into a real project right away. Pick the setup that matches your stack.
Option 1: Vite, Astro, React, or Svelte (@tailwindcss/vite)
If your project uses Vite (including Astro, React with Vite, Vue, or SvelteKit), install Tailwind and its official Vite plugin:
npm install -D tailwindcss @tailwindcss/vite
Add the plugin to vite.config.ts (or astro.config.mjs under vite.plugins):
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [tailwindcss()],
})
Create src/app.css with a single line:
@import "tailwindcss";
Import ./app.css in your main JavaScript entry file or layout template, start npm run dev, and every Tailwind utility works immediately with instant hot reload.
Option 2: Standalone CLI (@tailwindcss/cli)
If you are styling plain HTML files, Laravel Blade, Rails, Django, Go templates, or PHP without a JavaScript bundler, the standalone CLI is all you need.
Create a project folder and install the CLI:
mkdir tailwind-demo
cd tailwind-demo
npm init -y
npm install -D tailwindcss @tailwindcss/cli
Create src/app.css:
@import "tailwindcss";
@theme {
--color-brand: #2563eb;
--color-paper: #faf9f6;
--font-display: "Instrument Serif", Georgia, serif;
}
Create index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tailwind v4 Demo</title>
<link rel="stylesheet" href="./dist/app.css" />
</head>
<body class="bg-paper text-slate-900 antialiased">
<main class="mx-auto max-w-2xl px-6 py-16">
<h1 class="font-display text-4xl font-bold tracking-tight">Tailwind v4</h1>
<button class="mt-6 rounded-lg bg-brand px-4 py-2 text-sm font-medium text-white hover:opacity-90">
Get started
</button>
</main>
</body>
</html>
During development, run Tailwind with --watch so it recompiles every time you save a file:
npx @tailwindcss/cli -i src/app.css -o dist/app.css --watch
For production, swap --watch for --minify:
npx @tailwindcss/cli -i src/app.css -o dist/app.css --minify
Here is a real Tailwind CSS 4.3.5 build of that exact project on my Mac, finishing in 24 ms and outputting 5.9 KB minified (2.0 KB gzipped):

Option 3: Next.js and PostCSS (@tailwindcss/postcss)
Frameworks built on PostCSS (such as Next.js) use @tailwindcss/postcss:
npm install -D tailwindcss @tailwindcss/postcss postcss
Add the plugin in postcss.config.mjs:
export default {
plugins: {
'@tailwindcss/postcss': {},
},
}
Then put @import "tailwindcss"; at the top of app/globals.css.
Editor setup: Tailwind CSS IntelliSense
Before we go further, install the official Tailwind CSS IntelliSense extension in VS Code or Cursor.
It gives you autocomplete for every class name (including your custom @theme tokens), shows color swatches beside color classes, and reveals the exact CSS output when you hover over any class.
If VS Code shows a yellow warning saying Unknown at rule @theme or Unknown at rule @utility inside your CSS file, associate .css files with Tailwind CSS (or set "css.lint.unknownAtRules": "ignore" in your editor settings). I wrote a quick fix guide on fixing unknown at-rule warnings for Tailwind if you run into it.
Preflight and how Tailwind thinks about CSS
The first time you add @import "tailwindcss"; to an existing HTML page, you might notice something surprising: your <h1> shrinks to normal body size, bullet points disappear from <ul>, and default margins vanish.
That comes from Preflight, Tailwind’s built-in modern CSS reset.
What Preflight resets for you
Preflight smooths out browser inconsistencies so Chrome, Safari, and Firefox start from the same clean baseline:
- Sets
box-sizing: border-boxeverywhere sopaddingandbordernever blow up your widths. - Removes default margins from headings, paragraphs,
figure, andblockquote. - Strips default font sizes and bold weights from
h1throughh6so you pick headings for HTML semantics, not visual size. - Removes default bullets and left padding from
ulandol(since half the lists on modern sites are navbars and menus). - Makes
img,svg, andvideodisplay: blockandmax-width: 100%so images never leave a weird 4px gap underneath or overflow narrow phone screens.
CSS Cascade Layers in Tailwind v4
Tailwind v4 organizes its generated CSS using native CSS @layer rules in a strict priority order:
Because utilities is the last layer, a utility class like p-8 always overrides a base style in @layer base or a component class in @layer components without fighting specificity wars.
One warning: any raw CSS rule you write outside a @layer (unlayered CSS) beats every layered rule in the CSS spec! Always wrap custom global styles inside @layer base { ... } so Tailwind utilities can still override them.
Stacking variants and the ! important modifier
You can chain as many prefixes (called variants) as you want before any utility class, separated by colons:
<button class="bg-blue-600 text-white hover:bg-blue-700 md:px-6 dark:bg-blue-500 dark:hover:bg-blue-400">
Subscribe
</button>
When you genuinely need !important (for example, overriding a third-party widget’s inline stylesheet), put ! at the end of the class name in Tailwind v4:
<div class="bg-white! text-slate-900!">Forced override</div>
Thinking in utility classes (and mobile-first design)
If you are coming from traditional CSS files, looking at a button with eight class names can feel noisy at first. Give it one afternoon on a real page and something clicks: you stop inventing class names like .card-header-inner-wrapper and start styling right where you see the HTML.
Reading Tailwind classes like plain English
Almost every Tailwind class follows a simple property-value pattern:
p-4meanspadding: 1rem(4 units on the spacing scale, where 1 unit =0.25rem=4px).px-4means horizontal padding (left + right),py-2means vertical padding (top + bottom).mt-6meansmargin-top: 1.5rem,mb-2meansmargin-bottom: 0.5rem.text-lg font-bold text-zinc-900sets font size, weight, and color.bg-blue-600 text-white rounded-lgsets background, text color, and rounded corners.
How mobile-first breakpoints work
This is the main detail that confuses beginners in Tailwind: unprefixed classes apply to mobile screens first, and sm:, md:, lg: mean “at this width and wider.”
<!-- 1 column on phones, 2 columns on tablets, 3 columns on desktop -->
<div class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3">
<article class="rounded-lg border p-4">Card 1</article>
<article class="rounded-lg border p-4">Card 2</article>
<article class="rounded-lg border p-4">Card 3</article>
</div>
Never write sm:text-sm thinking it means “only on small phones”. Write text-sm md:text-base so phones get text-sm and tablets and desktops upgrade to text-base.
Arbitrary values when you need one exact pixel
You are never locked into Tailwind’s default scale. Whenever a design needs one specific value, wrap it in square brackets [...]:
<div class="w-[340px] bg-[#f8f5f0] top-[18px]">
Custom exact sizing
</div>
I use arbitrary values sparingly for one-off tweaks, and add repeated brand colors or fonts to @theme so the whole project stays consistent.
Spacing, sizing, and layout primitives
We use spacing and sizing classes on almost every element, so learning how the scale works pays off immediately.
How the dynamic --spacing scale works
In Tailwind v4, every spacing utility (p-*, m-*, gap-*, w-*, h-*, inset-*) multiplies a single CSS variable: --spacing, which defaults to 0.25rem (4px at the standard 16px browser root font size).
That makes mental math effortless: multiply the Tailwind number by 4 to get pixels:
| Utility | Calculation | Result |
|---|---|---|
p-0.5 | 0.5 * 4px | 2px (0.125rem) |
p-1 | 1 * 4px | 4px (0.25rem) |
p-2 | 2 * 4px | 8px (0.5rem) |
p-3 | 3 * 4px | 12px (0.75rem) |
p-4 | 4 * 4px | 16px (1rem) |
p-6 | 6 * 4px | 24px (1.5rem) |
p-8 | 8 * 4px | 32px (2rem) |
p-12 | 12 * 4px | 48px (3rem) |
Because Tailwind v4 computes calc(var(--spacing) * N) dynamically, any number works out of the box. Need mt-13 (52px) or w-75 (300px)? Just write it.
Padding, margin, and logical directions
Control individual sides with directional letters:
p-4/m-4: all four sidespx-4/mx-4: left and rightpy-2/my-6: top and bottompt-6,pr-4,pb-6,pl-4: top, right, bottom, leftps-4,pe-4,ms-auto,me-2: logical inline-start and inline-end (these flip cleanly if your site ever supports right-to-left languages like Arabic or Hebrew)mx-auto: centers a block that has amax-w-*or fixed width-mt-6: negative margin (prefix with a dash-)
gap-* vs space-y-*
When you want consistent breathing room between sibling elements, I reach for gap-* inside a flex or grid container 95% of the time:
<div class="flex flex-col gap-4">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</div>
space-y-4 adds top margin to every child except the first, which works on plain block containers, while gap-4 handles wrapped rows cleanly and never leaves stray margins when elements hide or reorder.
Width, height, size-*, and viewport units
- Container widths:
max-w-sm(24rem),max-w-md(28rem),max-w-lg(32rem),max-w-2xl(42rem),max-w-5xl(64rem),max-w-7xl(80rem), andmax-w-prose(65ch, ideal line length for comfortable reading!). - Fractions and full width:
w-full(100%),w-1/2,w-1/3,w-2/3,w-fit,w-auto. - Equal width and height: Use
size-10instead of typingw-10 h-10on avatars, icons, and square buttons. - Full-height pages on mobile: Prefer
min-h-dvh(100dvh, dynamic viewport height) overmin-h-screen(100vh). On iOS Safari,100vhincludes the area hidden behind the address bar, which pushes footers off-screen.min-h-dvhadjusts cleanly as the mobile bar expands and collapses.
Display, overflow, and aspect ratios
Control image framing and overflow cleanly:
<div class="aspect-video w-full overflow-hidden rounded-xl bg-slate-100">
<img
src="/images/desk.jpg"
alt="Workspace"
class="size-full object-cover"
/>
</div>
aspect-video locks a 16 / 9 box (use aspect-square for 1 / 1 or aspect-4/3 for 4 / 3), and size-full object-cover crops the photo cleanly without stretching it.
Typography
Clean typography makes even a simple layout look like a designer touched it. Tailwind covers font families, sizes, weights, line heights, letter spacing, and modern wrapping rules.
Font scale, weight, and line height
<header class="max-w-2xl space-y-3">
<p class="text-xs font-semibold tracking-wider text-blue-600 uppercase">
Handbook
</p>
<h1 class="text-4xl/tight font-bold tracking-tight text-balance text-slate-900">
Style modern web apps faster with Tailwind CSS v4
</h1>
<p class="text-lg/relaxed text-pretty text-slate-600">
Once you learn twenty core utilities, you can build almost any layout without leaving your HTML template.
</p>
</header>
Notice three tiny utilities in that header that elevate readability immediately:
text-4xl/tightandtext-lg/relaxed: the slash shorthand sets bothfont-sizeandline-heightin one token (you can also pass spacing numbers liketext-base/7).text-balance: asks the browser to balance line lengths on multi-line headings so you never get one awkward lonely word on the second line.text-pretty: avoids single-word orphans at the end of body paragraphs.
Truncation, line clamping, and tabular-nums
truncateclips single-line text with an ellipsis (...) when space runs out.line-clamp-2orline-clamp-3limits multi-line card excerpts to two or three lines with a clean ellipsis.tabular-numsgives every digit (0through9) the same width. Always puttabular-numson prices, countdown timers, dashboard metrics, and table columns so numbers align straight down vertically:
<div class="flex items-baseline justify-between border-b border-slate-200 py-2 text-sm">
<span class="text-slate-600">Monthly revenue</span>
<span class="font-mono font-semibold text-slate-900 tabular-nums">$12,480.00</span>
</div>
Links and lists
Because Preflight strips bullets from lists, opt back in whenever you want a real bulleted or numbered list inside your UI:
<ul class="list-disc space-y-1.5 pl-5 text-slate-700 marker:text-blue-600">
<li>Zero JavaScript config required</li>
<li>Compiles production CSS in milliseconds</li>
<li>Built-in container queries and OKLCH colors</li>
</ul>
For inline links, underline decoration-slate-300 underline-offset-4 hover:decoration-blue-600 looks much sharper than the default browser underline glued straight to the letters.
Colors, backgrounds, and gradients
Tailwind v4 ships an updated color palette defined in OKLCH, a modern perceptual color space that keeps brightness consistent across hues and unlocks richer colors on modern displays.
Reading the 50 to 950 shade ladder
Every color family runs across 11 shades (50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950). Once you learn what each range is for, picking harmonious colors takes seconds:
| Shade range | Best used for | Example |
|---|---|---|
50 to 100 | Soft page tints, badge backgrounds, subtle hover fills | bg-blue-50, hover:bg-slate-100 |
200 to 300 | Borders, dividers, muted input outlines | border-slate-200, divide-slate-200 |
400 to 500 | Placeholder text, subtle icons, primary accents | text-slate-400, bg-blue-500 |
600 to 700 | Primary buttons, links, strong badges (high contrast with white text!) | bg-blue-600 text-white, text-emerald-700 |
800 to 950 | Headings, body text, dark-mode surfaces | text-slate-900, dark:bg-slate-950 |
Pick ONE neutral gray family per project
Tailwind includes five neutral gray families with slightly different undertones:
slate: cool blue-gray (my favorite default for developer tools and SaaS apps)gray: slightly softer cool grayzinc: crisp neutral-cool gray (great for minimal monochrome UIs)neutral: pure zero-tint graystone: warm paper-like gray (great for blogs, editorial sites, and reading apps)
Pick one neutral family at the start of a project and stick to it everywhere. Mixing slate-200 borders with stone-500 text makes a page feel subtly off without being obvious why.
Opacity modifiers and arbitrary colors
Append /opacity to any color utility (bg-, text-, border-, ring-, shadow-) to adjust transparency cleanly:
<span class="inline-flex items-center gap-1.5 rounded-full bg-emerald-500/15 px-3 py-1 text-xs font-medium text-emerald-700 ring-1 ring-emerald-600/20">
<span class="size-1.5 rounded-full bg-emerald-500"></span>
Live
</span>
When you need a one-off hex or OKLCH value without adding it to @theme, wrap it in square brackets: bg-[#faf9f6] or text-[oklch(0.45_0.2_260)].
Gradients in Tailwind v4
Tailwind v4 modernized gradient utilities to match standard CSS gradient names:
- Linear gradients:
bg-linear-to-r,bg-linear-to-b,bg-linear-to-br, or exact angles likebg-linear-45. - Radial and conic gradients:
bg-radialandbg-conic. - Color stops:
from-*, optionalvia-*, andto-*.
<div class="rounded-2xl bg-linear-to-br from-blue-600 via-indigo-600 to-violet-600 p-8 text-white">
<h2 class="text-2xl font-bold">Ship faster with Tailwind v4</h2>
<p class="mt-2 text-blue-100">Modern linear, radial, and conic gradients built right in.</p>
</div>
You can also create gradient headline text in one line with bg-linear-to-r from-blue-600 to-violet-600 bg-clip-text text-transparent.
Borders, shadows, rings, and positioning
Once your spacing, type, and colors feel right, borders, elevation, and positioning give your interface clear structure.
Borders, corners, and dividers
In Tailwind v4, the default border color changed from gray (gray-200 in v3) to currentColor so it matches standard CSS. Always pass an explicit border color like border-slate-200:
<div class="rounded-2xl border border-slate-200 bg-white shadow-xs">
<div class="border-b border-slate-200 px-6 py-4">
<h3 class="font-semibold text-slate-900">Recent deploys</h3>
</div>
<ul class="divide-y divide-slate-200">
<li class="px-6 py-3 text-sm text-slate-700">Production (2 minutes ago)</li>
<li class="px-6 py-3 text-sm text-slate-700">Staging (18 minutes ago)</li>
</ul>
</div>
Notice divide-y divide-slate-200 on the <ul>. It draws crisp 1px lines between child items without putting an extra stray line above the first item or below the last item!
For corner radii, rounded-md, rounded-lg, rounded-xl, rounded-2xl, and rounded-full cover 99% of UI elements.
Shadows, rings, and frosted glass
Tailwind v4 refined its shadow scale to start softer (shadow-2xs, shadow-xs, shadow-sm, shadow-md, shadow-lg, shadow-xl), and added inset-shadow-xs and inset-ring for subtle tactile buttons.
For frosted-glass sticky headers, pair semi-transparent backgrounds with backdrop-blur-md:
<header class="sticky top-0 z-20 border-b border-slate-200/80 bg-white/80 backdrop-blur-md">
<div class="mx-auto flex max-w-5xl items-center justify-between px-6 py-3">
<a href="/" class="font-bold text-slate-900">BookShelf</a>
<a href="/pricing" class="text-sm font-medium text-slate-600 hover:text-slate-900">Pricing</a>
</div>
</header>
Positioning (relative, absolute, fixed, sticky)
relativeon a parent creates the positioning anchor for anabsolutechild inside it (for example, placing an unread dot in the top-right corner of an avatar):
<div class="relative inline-block">
<img src="/avatar.jpg" alt="Flavio" class="size-10 rounded-full object-cover" />
<span class="absolute -top-0.5 -right-0.5 size-3 rounded-full bg-emerald-500 ring-2 ring-white"></span>
</div>
fixed inset-0 z-50covers the whole viewport (for modal backdrops and drawers).sticky top-0 z-20scrolls normally until it reaches the top of the viewport, then pins itself there.
Flexbox layout in depth
Flexbox lays out items along a single axis: either a horizontal row (flex) or a vertical column (flex-col).
The alignment table that stops confusion forever
Almost every beginner mixes up items-* and justify-* at first. Remember one rule: justify-* moves along the main axis, and items-* moves along the cross axis.
| Class | In a horizontal row (flex) | In a vertical column (flex-col) |
|---|---|---|
justify-start | Push left | Push top |
justify-center | Center horizontally | Center vertically |
justify-between | First left, last right, even gaps | First top, last bottom |
justify-end | Push right | Push bottom |
items-start | Align to top edge | Align to left edge |
items-center | Vertically center items | Horizontally center items |
items-stretch | Equal heights (default) | Full width (default) |
items-baseline | Align text baselines | Align text baselines |
Controlling how children grow and shrink
flex-1lets a child expand to eat all remaining empty space (awesome for main content between a header and footer, or a search input beside a button).shrink-0prevents an icon, avatar, or badge from getting squished when a long title next to it runs out of room.min-w-0on a flex child enablestruncateto work inside a flex row (a famous CSS gotcha!).
Four everyday Flexbox patterns
- Sticky footer page shell (footer always sits at the bottom even on short pages):
<body class="flex min-h-dvh flex-col bg-white text-slate-900">
<header class="border-b border-slate-200 px-6 py-4">Header</header>
<main class="mx-auto w-full max-w-4xl flex-1 px-6 py-12">Main content</main>
<footer class="border-t border-slate-200 px-6 py-6 text-sm text-slate-500">Footer</footer>
</body>
- Row with avatar, truncating title, and fixed badge:
<div class="flex items-center gap-3 rounded-xl border border-slate-200 p-4">
<div class="flex size-10 shrink-0 items-center justify-center rounded-lg bg-blue-600 font-bold text-white">
TS
</div>
<div class="min-w-0 flex-1">
<p class="truncate font-semibold text-slate-900">TypeScript Handbook</p>
<p class="truncate text-sm text-slate-500">Practical types, narrowing, and Zod validation</p>
</div>
<span class="shrink-0 rounded-full bg-slate-100 px-2.5 py-1 text-xs font-medium text-slate-700">
43 pages
</span>
</div>
- Icon + label button:
inline-flex items-center gap-2 rounded-lg bg-slate-900 px-4 py-2 text-sm font-medium text-white. - Wrapping tag cloud:
flex flex-wrap items-center gap-2.
CSS Grid layout in depth
Whenever you want a two-dimensional layout (rows and columns), equal-width cards, or a sidebar next to fluid content, CSS Grid is cleaner than nesting three flex containers.
Responsive card grids and spanning
<section class="grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3">
<article class="rounded-xl border border-slate-200 p-6">Card 1</article>
<article class="rounded-xl border border-slate-200 p-6">Card 2</article>
<article class="rounded-xl border border-slate-200 p-6 sm:col-span-2 lg:col-span-1">
Card 3
</article>
</section>
Want a sidebar of exactly 260px next to a fluid main panel on desktop? Use an arbitrary grid template:
<div class="grid grid-cols-1 gap-8 lg:grid-cols-[260px_1fr]">
<aside class="rounded-xl border border-slate-200 p-4">Sidebar navigation</aside>
<main class="min-w-0">Article content</main>
</div>
Want cards that wrap automatically to fit any width without writing breakpoint classes at all? Combine CSS repeat(auto-fit, minmax(...)):
<div class="grid grid-cols-[repeat(auto-fit,minmax(240px,1fr))] gap-6">
<!-- Any number of cards -->
</div>
Aligning nested card footers with subgrid
Ever built a 3-column grid where one card has a longer title and pushes its footer button out of alignment with its neighbors?
CSS subgrid solves that cleanly by letting each card inherit the parent grid’s row tracks (grid-rows-subgrid row-span-3):
<div class="grid grid-cols-1 gap-6 md:grid-cols-3">
<article class="row-span-3 grid grid-rows-subgrid gap-3 rounded-xl border border-slate-200 p-6">
<h3 class="text-lg font-semibold">Short title</h3>
<p class="text-sm text-slate-600">One short line.</p>
<a href="#" class="self-end text-sm font-medium text-blue-600">Read more -></a>
</article>
<article class="row-span-3 grid grid-rows-subgrid gap-3 rounded-xl border border-slate-200 p-6">
<h3 class="text-lg font-semibold">A much longer multi-line card title</h3>
<p class="text-sm text-slate-600">Two or three sentences explaining the feature in full detail.</p>
<a href="#" class="self-end text-sm font-medium text-blue-600">Read more -></a>
</article>
</div>
When to pick Flexbox vs CSS Grid
| Layout need | Best choice | Starting classes |
|---|---|---|
| Navbar, toolbar, button with icon, tag pills | Flexbox | flex items-center justify-between gap-4 |
| Center one box horizontally and vertically | Grid (shortest!) | grid place-items-center min-h-dvh |
| Multi-column cards or pricing tiers | Grid | grid grid-cols-1 md:grid-cols-3 gap-6 |
| Fixed sidebar + fluid main area | Grid | grid grid-cols-1 lg:grid-cols-[260px_1fr] gap-8 |
| Bento dashboard with mixed card spans | Grid | grid grid-cols-1 md:grid-cols-4 gap-4 + md:col-span-2 |
Mastering Flexbox and CSS Grid layouts in practice
Knowing the individual flex and grid classes is step one. Combining them to build real page structures without overflow or alignment bugs is where Tailwind starts feeling fast.
Let’s build the layout structures you will reach for on almost every project. You can paste each snippet into an HTML file and resize your browser to watch how it reacts.
Centering anything in one line
Centering a box both horizontally and vertically used to be a running joke in CSS. In Tailwind, you have two one-line options that work on any container.
The shortest option uses CSS Grid:
<div class="grid min-h-64 place-items-center bg-slate-50 p-6">
<div class="rounded-xl border border-slate-200 bg-white p-6">
<h2 class="text-lg font-semibold text-slate-900">
Centered with Grid
</h2>
<p class="mt-1 text-sm text-slate-600">
One class handles both axes at once.
</p>
</div>
</div>
grid place-items-center sets both align-items: center and justify-items: center. Give the wrapper a height like min-h-64 or min-h-dvh, and the child sits dead center.
When you want to center a vertical stack of elements (like a heading with a short note and button), use flex flex-col items-center justify-center:
<section
class="flex min-h-80 flex-col items-center justify-center
rounded-2xl border border-dashed border-slate-300
bg-slate-50 p-8 text-center"
>
<h2 class="text-lg font-semibold text-slate-900">
No projects yet
</h2>
<p class="mt-1 max-w-sm text-sm text-slate-600">
Create your first project to start tracking tasks and notes.
</p>
<button
class="mt-5 rounded-lg bg-blue-600 px-4 py-2 text-sm
font-medium text-white hover:bg-blue-700"
>
New project
</button>
</section>
I use grid place-items-center to center a single card or modal, and flex flex-col items-center justify-center for empty states and centered hero blocks.
Toolbars and split headers with flex, items-center, and justify-between
Most dashboards, cards, and modals start with a horizontal bar: a title on the left, action buttons on the right, and consistent spacing between them.
<div
class="flex flex-wrap items-center justify-between gap-4
rounded-xl border border-slate-200 bg-white px-5 py-4"
>
<div>
<h2 class="text-base font-semibold text-slate-900">
Team members
</h2>
<p class="text-sm text-slate-500">
Manage roles and pending invitations.
</p>
</div>
<div class="flex items-center gap-2">
<button
class="rounded-lg border border-slate-300 bg-white px-3
py-1.5 text-sm font-medium text-slate-700
hover:bg-slate-50"
>
Export CSV
</button>
<button
class="rounded-lg bg-blue-600 px-3 py-1.5 text-sm
font-medium text-white hover:bg-blue-700"
>
Invite user
</button>
</div>
</div>
Adding flex-wrap to the outer row prevents the buttons from spilling off a narrow phone screen. When space gets tight, the button group drops below the title, and gap-4 keeps clean breathing room between both rows.
What if you have three groups in a bar (logo on the left, links in the center, login button on the right)? Put grid grid-cols-3 items-center on the bar and use justify-self-center on the middle nav and justify-self-end on the button so the links stay centered regardless of how wide the logo or button is.
Responsive grids and col-span-2 bento layouts
A standard card list starts at one column on phones, moves to two columns at sm:, and opens into three columns at lg::
<div
class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3"
>
<article
class="rounded-xl border border-slate-200 bg-white p-5"
>
<p class="text-xs font-medium text-slate-500 uppercase">
Active users
</p>
<p class="mt-2 text-3xl font-bold text-slate-900 tabular-nums">
2,841
</p>
</article>
<article
class="rounded-xl border border-slate-200 bg-white p-5"
>
<p class="text-xs font-medium text-slate-500 uppercase">
Conversion rate
</p>
<p class="mt-2 text-3xl font-bold text-slate-900 tabular-nums">
4.6%
</p>
</article>
<article
class="rounded-xl border border-slate-200 bg-white p-5
sm:col-span-2 lg:col-span-1"
>
<p class="text-xs font-medium text-slate-500 uppercase">
Monthly revenue
</p>
<p class="mt-2 text-3xl font-bold text-slate-900 tabular-nums">
$18,420
</p>
</article>
</div>
Look at the third card: sm:col-span-2 lg:col-span-1. On a 2-column tablet layout (sm:), a 3-card grid leaves an awkward empty gap in the bottom-right corner.
Spanning the third card across two columns fills that second row evenly. Once the screen hits lg:, lg:col-span-1 restores a normal single column.
You can take spanning further to build an asymmetric bento grid where one featured panel spans two columns and two rows:
<section class="grid grid-cols-1 gap-4 md:grid-cols-3">
<div
class="rounded-2xl border border-slate-200 bg-slate-900 p-6
text-white md:col-span-2 md:row-span-2"
>
<span class="text-xs font-semibold text-blue-400 uppercase">
Featured
</span>
<h2 class="mt-3 text-2xl font-bold">
Instant builds with Oxide
</h2>
<p class="mt-2 max-w-md text-sm text-slate-300">
Tailwind v4 compiles incremental CSS edits in milliseconds,
even across hundreds of templates.
</p>
</div>
<div class="rounded-2xl border border-slate-200 bg-white p-6">
<h3 class="font-semibold text-slate-900">Zero config</h3>
<p class="mt-1 text-sm text-slate-600">
Drop in one CSS import and start writing utilities.
</p>
</div>
<div class="rounded-2xl border border-slate-200 bg-white p-6">
<h3 class="font-semibold text-slate-900">Native CSS tokens</h3>
<p class="mt-1 text-sm text-slate-600">
Every color and spacing value is a real CSS variable.
</p>
</div>
</section>
On phones, every card stacks neatly in HTML order. On tablets and desktops (md:), the dark card occupies the left two-thirds while the two smaller cards stack on the right.
Sticky header, sidebar, and main content shell
Docs sites, admin panels, and SaaS dashboards share the same core frame: a sticky top header, a sidebar navigation column on desktop, and a fluid main content area.
Here is a complete app shell that works smoothly on both phones and wide monitors:
<div class="min-h-dvh bg-slate-50 text-slate-900">
<header
class="sticky top-0 z-30 border-b border-slate-200
bg-white/90 backdrop-blur-md"
>
<div
class="mx-auto flex h-14 max-w-7xl items-center
justify-between gap-4 px-4 sm:px-6"
>
<a href="/" class="font-bold tracking-tight text-slate-900">
DocsApp
</a>
<div class="flex items-center gap-3">
<input
type="search"
placeholder="Search docs..."
class="hidden w-56 rounded-lg border border-slate-300
px-3 py-1.5 text-sm sm:block"
/>
<a
href="/login"
class="rounded-lg bg-slate-900 px-3 py-1.5 text-sm
font-medium text-white hover:bg-slate-800"
>
Sign in
</a>
</div>
</div>
</header>
<div
class="mx-auto grid max-w-7xl grid-cols-1 gap-8 px-4 py-8
sm:px-6 lg:grid-cols-[240px_1fr]"
>
<aside class="lg:sticky lg:top-20 lg:self-start">
<nav class="space-y-1 text-sm">
<a
href="#intro"
class="block rounded-lg bg-blue-50 px-3 py-2
font-medium text-blue-700"
>
Introduction
</a>
<a
href="#install"
class="block rounded-lg px-3 py-2 text-slate-600
hover:bg-slate-100 hover:text-slate-900"
>
Installation
</a>
<a
href="#layout"
class="block rounded-lg px-3 py-2 text-slate-600
hover:bg-slate-100 hover:text-slate-900"
>
Layout patterns
</a>
</nav>
</aside>
<main
class="min-w-0 rounded-2xl border border-slate-200
bg-white p-6 sm:p-8"
>
<h1 class="text-2xl font-bold text-slate-900">
Introduction
</h1>
<p class="mt-3 text-sm/relaxed text-slate-600">
Start here to set up your workspace and build your first
page with Tailwind CSS v4.
</p>
</main>
</div>
</div>
Three small details keep this shell bug-free:
lg:self-starton<aside>is required forlg:stickyto stick inside a CSS Grid. By default, grid items stretch to the full row height, leaving a sticky sidebar with nowhere to move.lg:top-20offsets the sticky sidebar below theh-14(56px) sticky header so they never collide.min-w-0on<main>prevents wide tables or code blocks from pushing the1frgrid track past the viewport edge.
Quick Flexbox and Grid debugging checklist
When a layout behaves strangely, check these four causes first:
| Symptom | Usual cause | Tailwind fix |
|---|---|---|
Long text refuses to truncate in a flex row | Flex children default to min-width: auto | Add min-w-0 to the text wrapper |
| Avatar or icon gets squished on small screens | Flex children default to flex-shrink: 1 | Add shrink-0 to the avatar or icon |
| Sticky sidebar inside a grid refuses to stick | Grid items stretch to full row height | Add self-start to <aside> |
| Buttons overflow off the right edge on phones | Flex rows do not wrap by default | Add flex-wrap to the flex parent |
Responsive design and container queries
Tailwind is mobile-first. Every unprefixed class applies at 0px and up (phones first), and breakpoint prefixes layer overrides as the viewport grows wider.
| Prefix | Minimum width | CSS media query |
|---|---|---|
| (none) | 0px | Default (all screens) |
sm: | 40rem (640px) | @media (width >= 40rem) |
md: | 48rem (768px) | @media (width >= 48rem) |
lg: | 64rem (1024px) | @media (width >= 64rem) |
xl: | 80rem (1280px) | @media (width >= 80rem) |
2xl: | 96rem (1536px) | @media (width >= 96rem) |
Here is the mistake almost everyone makes on day one: writing sm:text-center thinking sm: means “only on small screens”. It means 640px and wider!
To center text on phones and left-align it from tablets up, write text-center md:text-left.
If you genuinely need a style to apply only below a breakpoint (like hiding a desktop sidebar below md), Tailwind v4 supports max-* variants too: max-md:hidden or range variants like md:max-lg:grid-cols-2.
Built-in container queries (@container)
Viewport breakpoints break down when the same reusable card sits inside a wide hero section on one page and a narrow 300px sidebar on another.
Tailwind v4 builds CSS container queries right into core with zero plugins. Put @container on the parent wrapper, then style children based on the parent’s width with @sm: (24rem), @md: (28rem), @lg: (32rem), and @xl: (36rem):
<div class="@container">
<article class="flex flex-col gap-4 rounded-xl border border-slate-200 bg-white p-4 @md:flex-row @md:items-center">
<img
src="/images/cover.jpg"
alt="Handbook cover"
class="h-36 w-full rounded-lg object-cover @md:size-28 @md:shrink-0"
/>
<div class="space-y-1">
<h3 class="font-semibold text-slate-900">Tailwind CSS Handbook</h3>
<p class="text-sm text-slate-600">
Stacks vertically in a narrow sidebar and switches to a horizontal row in a wide column.
</p>
</div>
</article>
</div>
If you nest containers inside each other, name them (@container/sidebar and @md/sidebar:flex-row) so inner components listen to the exact ancestor they care about.
Interactive states, dark mode, and accessibility
Prefix any utility with state variants to handle hover, focus, sibling inputs, parent hovers, data attributes, and dark mode without writing a single CSS selector by hand.
Hover, focus, active, and disabled
<button
class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-blue-700 active:scale-98 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 disabled:cursor-not-allowed disabled:opacity-50"
>
Save changes
</button>
I always use focus-visible: instead of focus: on buttons and links. That keeps keyboard Tab navigation crystal clear while stopping mouse clicks from leaving a lingering blue box behind.
Relational states: group, peer, and has-
- Style a child when its parent is hovered (
group+group-hover:):
<a
href="/ebooks/tailwind-css-handbook/"
class="group flex items-center justify-between rounded-xl border border-slate-200 p-4 hover:border-blue-500"
>
<span class="font-medium text-slate-900 group-hover:text-blue-600">Read the handbook</span>
<span class="text-slate-400 transition-transform group-hover:translate-x-1 group-hover:text-blue-600">
->
</span>
</a>
- Style a sibling based on an input (
peer+peer-checked:):
<label class="flex cursor-pointer items-center gap-3">
<input type="checkbox" class="peer sr-only" />
<span class="flex size-5 items-center justify-center rounded border border-slate-300 peer-checked:border-blue-600 peer-checked:bg-blue-600 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-blue-600"></span>
<span class="text-sm text-slate-700 peer-checked:font-medium peer-checked:text-slate-900">
Remember me
</span>
</label>
- Style a parent based on what is inside it (
has-[...]):
Modern CSS :has() is supported directly in Tailwind v4 and makes radio cards ridiculously clean:
<label class="flex cursor-pointer items-start gap-3 rounded-xl border border-slate-200 p-4 has-checked:border-blue-600 has-checked:bg-blue-50/50">
<input type="radio" name="plan" value="pro" class="mt-1 accent-blue-600" />
<div>
<p class="font-semibold text-slate-900">Pro Plan</p>
<p class="text-sm text-slate-600">Unlimited projects and priority support.</p>
</div>
</label>
You can also style all direct children at once with *: (for example, *:px-3 on a button group) or match ARIA/data attributes directly (aria-expanded:rotate-180, data-[open]:block).
Dark mode (OS preference vs manual toggle)
Out of the box, the dark: variant follows the user’s operating system setting (prefers-color-scheme: dark):
<div class="border border-slate-200 bg-white text-slate-900 dark:border-slate-800 dark:bg-slate-900 dark:text-slate-100">
Works in both light and dark themes automatically
</div>
If your site includes a light/dark theme toggle button that puts a .dark class on <html>, add one line right below @import "tailwindcss"; in src/app.css:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
That tells Tailwind v4 to activate dark: utilities whenever .dark is present on <html> (or any ancestor). Pair it with color-scheme: light dark; so browser scrollbars and native date pickers match your active theme too.
Accessibility utilities you should use every week
sr-only: hides text visually while keeping it readable to screen readers (essential for icon-only search or close buttons).not-sr-only: reveals a “Skip to main content” link when keyboard users press Tab (sr-only focus:not-sr-only focus:fixed focus:top-4 focus:left-4).motion-reduce:transition-noneandmotion-reduce:animate-none: respects users who turned on Reduce Motion in macOS, iOS, or Windows.print:hidden: hides navbars, sidebars, and footer clutter when someone prints your page.
Interactive states with group, peer, and has
Basic hover: styles are easy. Real interfaces also need parent-to-child hover effects, sibling validation messages, and containers that react when a checkbox or input inside them changes.
Tailwind v4 gives you three relational variants (group-*, peer-*, and has-*) that replace hundreds of lines of custom CSS and JavaScript event listeners.
Mastering hover:, focus-visible:, active:, and disabled:
Every interactive control should communicate four states clearly: resting, hovered, pressed, and focused via keyboard. Disabled controls also need muted colors and a clear cursor hint.
<div class="flex flex-wrap items-center gap-3">
<button
class="cursor-pointer rounded-lg bg-blue-600 px-4 py-2
text-sm font-medium text-white transition
duration-150 hover:bg-blue-700 active:scale-97
focus-visible:outline-2
focus-visible:outline-offset-2
focus-visible:outline-blue-600
motion-reduce:transition-none"
>
Publish post
</button>
<button
disabled
class="rounded-lg bg-blue-600 px-4 py-2 text-sm
font-medium text-white disabled:cursor-not-allowed
disabled:opacity-50"
>
Saving...
</button>
</div>
Notice two habits worth keeping on every button:
- Prefer
focus-visible:overfocus:for buttons and links. Keyboard users pressing Tab get a sharp outline, while mouse clicks stay clean. - Always pair
disabled:opacity-50withdisabled:cursor-not-allowedso nobody wonders why a button ignores clicks.
Styling children on parent hover with group and group-hover:
Put group on a clickable card or link, then prefix child utilities with group-hover: or group-focus-visible: so every inner piece responds together.
<a
href="/ebooks/tailwind-css-handbook/"
class="group block max-w-sm overflow-hidden rounded-2xl
border border-slate-200 bg-white shadow-2xs
transition duration-200 hover:border-blue-400
hover:shadow-md focus-visible:outline-2
focus-visible:outline-offset-2
focus-visible:outline-blue-600
motion-reduce:transition-none"
>
<div class="aspect-video overflow-hidden bg-slate-100">
<img
src="/images/tailwind-cover.jpg"
alt="Tailwind CSS Handbook"
class="size-full object-cover transition duration-300
group-hover:scale-105
motion-reduce:transition-none"
/>
</div>
<div class="p-5">
<span
class="inline-block rounded-full bg-blue-50 px-2.5 py-0.5
text-xs font-medium text-blue-700"
>
Free ebook
</span>
<h3
class="mt-2 text-lg font-semibold text-slate-900
transition-colors group-hover:text-blue-600"
>
Tailwind CSS Handbook
</h3>
<p class="mt-1 text-sm text-slate-600">
Learn utility-first CSS from scratch with real projects.
</p>
<p
class="mt-4 inline-flex items-center gap-1 text-sm
font-medium text-blue-600"
>
<span>Read now</span>
<span
class="transition-transform duration-200
group-hover:translate-x-1
motion-reduce:transition-none"
>
->
</span>
</p>
</div>
</a>
Hovering anywhere on the card gently zooms the cover image (clipped cleanly by overflow-hidden), turns the title blue, and nudges the arrow right.
What if you nest a button inside a hoverable card and want separate hover effects? Name each group with a slash: put group/card on the outer card and group/btn on the inner button, then write group-hover/card:border-blue-400 and group-hover/btn:translate-x-1.
Floating labels and live validation with peer and peer-invalid:
The peer class lets an element react to a preceding sibling in HTML. That order matters: the element carrying peer must come before any element using peer-* classes!
Here is a floating-label input with zero JavaScript. It combines placeholder=" " (a single space), peer-placeholder-shown:, and peer-focus::
<div class="relative max-w-sm">
<input
id="full-name"
type="text"
placeholder=" "
class="peer block w-full rounded-lg border border-slate-300
bg-white px-3.5 pt-5 pb-2 text-sm text-slate-900
focus:border-blue-600 focus:outline-hidden"
/>
<label
for="full-name"
class="pointer-events-none absolute top-2 left-3.5
text-xs font-medium text-slate-500 transition-all
peer-placeholder-shown:top-3.5
peer-placeholder-shown:text-sm
peer-focus:top-2 peer-focus:text-xs
peer-focus:text-blue-600
motion-reduce:transition-none"
>
Full name
</label>
</div>
When the input is empty, the browser considers its blank placeholder shown, so the label sits inside the box at top-3.5. The moment you focus the field or type a character, the label glides up to top-2 and shrinks to text-xs.
You can also reveal an inline error message below an input using peer-invalid: (or peer-user-invalid: so the error waits until the user finishes editing):
<div class="max-w-sm">
<label
for="work-email"
class="block text-sm font-medium text-slate-700"
>
Work email
</label>
<input
id="work-email"
type="email"
required
placeholder="you@company.com"
class="peer mt-1.5 block w-full rounded-lg border
border-slate-300 bg-white px-3.5 py-2 text-sm
text-slate-900 focus:border-blue-600
focus:outline-hidden
user-invalid:border-red-500"
/>
<p
class="mt-1.5 hidden text-xs text-red-600
peer-user-invalid:block"
>
Please enter a valid email address.
</p>
</div>
If you want immediate feedback while typing instead of waiting for blur, swap peer-user-invalid:block for peer-invalid:block. Just remember that an empty required field starts invalid immediately with peer-invalid:, so peer-user-invalid: usually gives a friendlier experience.
Parent styling with has-[:checked]: and has-[:focus]:
CSS :has() flips styling direction: a parent can change its own borders, background, or shadow based on a child element inside it.
Here is a clean plan picker where clicking anywhere on a card highlights its border and background automatically:
<fieldset class="max-w-md space-y-3">
<legend class="mb-2 text-sm font-semibold text-slate-900">
Choose billing cycle
</legend>
<label
class="flex cursor-pointer items-center justify-between
rounded-xl border border-slate-200 bg-white p-4
transition-colors hover:border-slate-300
has-[:checked]:border-blue-600
has-[:checked]:bg-blue-50/60
has-[:focus-visible]:outline-2
has-[:focus-visible]:outline-offset-2
has-[:focus-visible]:outline-blue-600"
>
<span class="flex items-center gap-3">
<input
type="radio"
name="billing"
value="monthly"
checked
class="size-4 accent-blue-600"
/>
<span class="text-sm font-medium text-slate-900">
Monthly billing
</span>
</span>
<span class="text-sm font-semibold text-slate-900 tabular-nums">
$12/mo
</span>
</label>
<label
class="flex cursor-pointer items-center justify-between
rounded-xl border border-slate-200 bg-white p-4
transition-colors hover:border-slate-300
has-[:checked]:border-blue-600
has-[:checked]:bg-blue-50/60
has-[:focus-visible]:outline-2
has-[:focus-visible]:outline-offset-2
has-[:focus-visible]:outline-blue-600"
>
<span class="flex items-center gap-3">
<input
type="radio"
name="billing"
value="yearly"
class="size-4 accent-blue-600"
/>
<span class="text-sm font-medium text-slate-900">
Yearly billing (save 20%)
</span>
</span>
<span class="text-sm font-semibold text-slate-900 tabular-nums">
$9.60/mo
</span>
</label>
</fieldset>
You can also use has-[:focus]: on a search bar wrapper that holds an icon, an <input>, and a keyboard shortcut badge so the entire wrapper gets a crisp focus ring when the input is active:
<div
class="flex max-w-sm items-center gap-2 rounded-lg border
border-slate-300 bg-white px-3 py-2
has-[:focus]:border-blue-600
has-[:focus]:ring-3 has-[:focus]:ring-blue-600/20"
>
<span class="text-xs font-medium text-slate-400">Find</span>
<input
type="search"
placeholder="Search handbooks..."
class="w-full bg-transparent text-sm text-slate-900
placeholder:text-slate-400 focus:outline-hidden"
/>
<kbd
class="rounded border border-slate-200 bg-slate-50 px-1.5
py-0.5 font-mono text-xs text-slate-500"
>
/
</kbd>
</div>
Choosing between group, peer, and has
| Variant | Direction | Best used for |
|---|---|---|
group + group-hover: | Parent to child | Hovering a card to style its title, image, or arrow |
peer + peer-invalid: | Preceding sibling to following sibling | Floating labels, custom checkboxes, inline form errors |
has-[:checked]: / has-[:focus]: | Child to parent | Selectable radio cards, checklist rows, composite search inputs |
Customizing your design system with @theme
In Tailwind v4, we no longer maintain a separate tailwind.config.js file. Every design token lives directly in CSS inside a @theme block.
When you add a variable inside @theme, Tailwind v4 automatically generates both the matching utility classes and standard CSS custom properties in :root:
Let’s look at a clean design system file (src/app.css):
@import "tailwindcss";
@theme {
--color-brand: #2563eb;
--color-brand-dark: #1d4ed8;
--color-paper: #faf9f6;
--font-sans: "Inter", ui-sans-serif, system-ui, sans-serif;
--font-display: "Instrument Serif", Georgia, serif;
--font-mono: "Geist Mono", ui-monospace, monospace;
--breakpoint-3xl: 120rem;
--radius-card: 1rem;
}
Here is a real Tailwind CSS 4.3.5 build inspecting the exact CSS custom properties and utilities generated from that @theme block and a custom @utility (compiled in 20 ms):

@theme namespaces at a glance
Prefix in @theme | Generated utility classes | Example |
|---|---|---|
--color-* | bg-*, text-*, border-*, ring-*, fill-* | --color-brand: #2563eb; → bg-brand |
--font-* | font-* | --font-display: "Georgia", serif; → font-display |
--text-* | text-* (font sizes) | --text-hero: 3.5rem; → text-hero |
--spacing-* | p-*, m-*, w-*, h-*, gap-* | --spacing-sidebar: 16rem; → w-sidebar |
--breakpoint-* | Responsive prefixes (*:) | --breakpoint-3xl: 120rem; → 3xl:grid-cols-4 |
--radius-* | rounded-* | --radius-card: 1rem; → rounded-card |
--shadow-* | shadow-* | --shadow-card: 0 2px 8px rgb(0 0 0 / 0.08); → shadow-card |
--animate-* | animate-* | --animate-fade-in: fade-in 200ms ease-out; → animate-fade-in |
Adding custom web fonts
Load your .woff2 file with standard @font-face (or a Google Fonts <link> in <head>), then register the family inside @theme:
@import "tailwindcss";
@font-face {
font-family: "Instrument Serif";
src: url("/fonts/instrument-serif.woff2") format("woff2");
font-weight: 400;
font-display: swap;
}
@theme {
--font-display: "Instrument Serif", Georgia, serif;
}
Replacing defaults vs extending them
By default, anything you put in @theme extends Tailwind’s built-in tokens. If your team wants a strict brand palette where nobody can accidentally use random pink-400 or lime-300 classes, reset that namespace first with initial:
@theme {
--color-*: initial;
--color-white: #ffffff;
--color-ink: #111827;
--color-muted: #6b7280;
--color-brand: #2563eb;
}
Semantic light/dark tokens with @theme inline
When your theme token references another CSS variable that changes at runtime (for example, switching light and dark semantic colors on :root and .dark, or referencing next/font variables), add the inline keyword so Tailwind emits var(--surface) directly instead of baking the initial value in:
:root {
--surface: #faf9f6;
--ink: #0f172a;
}
.dark {
--surface: #0b1120;
--ink: #f8fafc;
}
@theme inline {
--color-surface: var(--surface);
--color-ink: var(--ink);
}
Now bg-surface text-ink adapts automatically between light and dark mode with zero dark: prefix repetition!
Custom utilities, variants, @layer, and @apply
Sometimes you want a custom helper class or element default that Tailwind does not ship out of the box.
Creating custom utilities with @utility
In Tailwind v4, declare custom utilities with @utility. Unlike a plain CSS class, a @utility automatically gains full support for hover:, md:, dark:, and focus-visible::
@utility content-auto {
content-visibility: auto;
}
@utility scrollbar-none {
scrollbar-width: none;
&::-webkit-scrollbar {
display: none;
}
}
Now md:content-auto and scrollbar-none work right alongside built-in classes.
Custom element defaults with @layer base
Put global styles for raw HTML elements inside @layer base so utility classes can still override them cleanly:
@layer base {
body {
background-color: var(--color-paper);
color: var(--color-slate-900);
}
a {
color: var(--color-brand);
}
}
My honest take on @apply
Tailwind lets you compose utility classes inside a traditional CSS rule using @apply:
@layer components {
.btn-primary {
@apply inline-flex items-center justify-center rounded-lg bg-brand px-4 py-2 text-sm font-medium text-white hover:bg-brand-dark;
}
}
I used @apply a lot when I first learned Tailwind, and then I stopped.
Why? Because extracting a real template component (an Astro <Button.astro>, React <Button />, Laravel <x-button>, or Rails partial) gives you one reusable source of truth for both markup and classes without inventing a parallel CSS naming system again.
Keep @apply for places where you cannot edit the HTML directly (like third-party library widgets or rendered Markdown) and prefer real components everywhere else.
Styling forms, tables, and Markdown prose
Forms, data tables, and long-form articles are where many Tailwind sites either shine or fall apart. Let’s make sure each of them looks sharp out of the box.
Clean, accessible forms
<form class="max-w-md space-y-5">
<div>
<label for="email" class="block text-sm font-medium text-slate-700 dark:text-slate-200">
Email address
</label>
<input
id="email"
type="email"
required
placeholder="flavio@flaviocopes.com"
class="mt-1.5 block w-full rounded-lg border border-slate-300 bg-white px-3.5 py-2 text-sm text-slate-900 shadow-2xs placeholder:text-slate-400 focus:border-blue-500 focus:ring-3 focus:ring-blue-500/20 focus:outline-hidden user-invalid:border-red-500 dark:border-slate-700 dark:bg-slate-900 dark:text-white"
/>
</div>
<div>
<label for="avatar" class="block text-sm font-medium text-slate-700 dark:text-slate-200">
Profile photo
</label>
<input
id="avatar"
type="file"
accept="image/*"
class="mt-1.5 block w-full text-sm text-slate-600 file:mr-4 file:rounded-lg file:border-0 file:bg-blue-50 file:px-4 file:py-2 file:text-sm file:font-semibold file:text-blue-700 hover:file:bg-blue-100"
/>
</div>
<label class="flex items-center gap-2.5 text-sm text-slate-700 dark:text-slate-300">
<input type="checkbox" class="size-4 rounded accent-blue-600" />
Send me new handbook releases
</label>
<button
type="submit"
class="w-full rounded-lg bg-blue-600 px-4 py-2.5 text-sm font-semibold text-white shadow-xs hover:bg-blue-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600"
>
Join newsletter
</button>
</form>
Notice three handy form utilities here:
accent-blue-600tints native checkboxes, radios, and range sliders in one class.file:*styles the ugly browser file-picker button cleanly.user-invalid:border-red-500highlights invalid fields only after the user edits or submits them (never on initial empty page load!).
Responsive data tables
Always wrap tables in overflow-x-auto so narrow mobile screens scroll the table horizontally instead of breaking the whole page layout:
<div class="overflow-x-auto rounded-xl border border-slate-200 bg-white dark:border-slate-800 dark:bg-slate-900">
<table class="min-w-full divide-y divide-slate-200 text-left text-sm dark:divide-slate-800">
<thead class="bg-slate-50 text-xs font-semibold tracking-wider text-slate-500 uppercase dark:bg-slate-950/60">
<tr>
<th class="px-5 py-3">Handbook</th>
<th class="px-5 py-3">Format</th>
<th class="px-5 py-3 text-right">Pages</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-200 dark:divide-slate-800">
<tr class="hover:bg-slate-50/80 dark:hover:bg-slate-800/50">
<td class="px-5 py-3.5 font-medium text-slate-900 dark:text-white">TypeScript Handbook</td>
<td class="px-5 py-3.5 text-slate-600 dark:text-slate-300">PDF + EPUB</td>
<td class="px-5 py-3.5 text-right font-mono tabular-nums">43</td>
</tr>
<tr class="hover:bg-slate-50/80 dark:hover:bg-slate-800/50">
<td class="px-5 py-3.5 font-medium text-slate-900 dark:text-white">Tailwind CSS Handbook</td>
<td class="px-5 py-3.5 text-slate-600 dark:text-slate-300">PDF + EPUB</td>
<td class="px-5 py-3.5 text-right font-mono tabular-nums">40</td>
</tr>
</tbody>
</table>
</div>
Long-form Markdown with @tailwindcss/typography
Because Preflight strips heading and list styles, rendered Markdown looks completely flat until you style it.
Install the official typography plugin and enable it with @plugin in src/app.css:
npm install -D @tailwindcss/typography
@import "tailwindcss";
@plugin "@tailwindcss/typography";
Wrap your rendered Markdown HTML in prose:
<article class="prose prose-slate dark:prose-invert mx-auto max-w-2xl prose-headings:tracking-tight prose-a:text-blue-600 prose-img:rounded-xl">
<!-- Rendered Markdown HTML goes here -->
</article>
Element modifiers like prose-a:text-blue-600, prose-code:before:content-none, and prose-img:rounded-xl let you fine-tune the typography plugin without overriding CSS selectors manually.
Transitions, transforms, and modern animations
Subtle motion makes buttons and cards feel responsive without getting in the reader’s way.
Everyday transitions and transforms
Pick narrow transition properties (transition-colors, transition-transform, transition-opacity) instead of transition-all so the browser stays fast and never animates unexpected layout shifts:
<a
href="/ebooks"
class="block rounded-xl border border-slate-200 bg-white p-6 transition duration-200 ease-out hover:-translate-y-0.5 hover:border-slate-300 hover:shadow-md motion-reduce:transition-none motion-reduce:hover:translate-y-0"
>
<h3 class="font-semibold text-slate-900">Browse all handbooks</h3>
<p class="mt-1 text-sm text-slate-600">Free PDF and EPUB guides for web developers.</p>
</a>
Built-in animations and custom @keyframes
Tailwind ships four handy built-in animations:
animate-spin(loading spinners)animate-pulse(skeleton loading placeholders)animate-ping(live status indicator dot)animate-bounce(scroll-down cue)
Add your own animation cleanly inside @theme:
@theme {
--animate-fade-up: fade-up 250ms ease-out both;
@keyframes fade-up {
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
}
Then apply animate-fade-up motion-reduce:animate-none anywhere in your HTML.
Entry animations with starting: (@starting-style)
One of the coolest additions in Tailwind v4 is the starting: variant, powered by CSS @starting-style.
Previously, fading in a native <dialog> or [popover] when it switched from display: none to display: block required JavaScript timers. Now it takes one class string:
<div
id="tip-popover"
popover
class="rounded-xl border border-slate-200 bg-white p-4 shadow-lg transition duration-200 open:opacity-100 open:scale-100 starting:open:opacity-0 starting:open:scale-95"
>
Smooth entry animation with zero JavaScript!
</div>
Dark mode, @theme tokens, arbitrary values, container queries, and motion
Once your base layout looks good in light mode on a laptop, five Tailwind v4 features turn a rough prototype into a polished production app: dark mode, @theme tokens, arbitrary values, container queries, and respectful animations.
Let’s walk through practical recipes for each one so you can drop them straight into your own projects.
Designing calm dark mode surfaces and a working toggle
The quickest way to make dark mode look harsh is slapping pure black (bg-black) behind pure white text (text-white). Real dark interfaces use layered dark grays so cards, borders, and popovers stay clearly separated without straining the eyes.
Here is the surface ladder I reach for in almost every Tailwind app:
| Surface role | Light mode | Dark mode |
|---|---|---|
| Page background | bg-slate-50 | dark:bg-slate-950 |
| Card or panel | bg-white | dark:bg-slate-900 |
| Subtle nested box | bg-slate-100 | dark:bg-slate-800/70 |
| Borders and dividers | border-slate-200 | dark:border-slate-800 |
| Primary headings | text-slate-900 | dark:text-slate-100 |
| Secondary body text | text-slate-600 | dark:text-slate-400 |
To let users switch themes manually (and remember their choice on reload), pair @custom-variant dark in CSS with a tiny script on your toggle button:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
<button
id="theme-toggle"
type="button"
class="rounded-lg border border-slate-300 bg-white px-3 py-1.5
text-sm font-medium text-slate-700 hover:bg-slate-50
dark:border-slate-700 dark:bg-slate-900
dark:text-slate-200 dark:hover:bg-slate-800"
>
Toggle theme
</button>
<script>
const root = document.documentElement
const saved = localStorage.getItem('theme')
if (saved === 'dark') root.classList.add('dark')
document
.getElementById('theme-toggle')
.addEventListener('click', () => {
const isDark = root.classList.toggle('dark')
localStorage.setItem('theme', isDark ? 'dark' : 'light')
})
</script>
Put the initial localStorage check inside a tiny <script> in <head> so returning dark-mode visitors never see a white flash before the page paints.
Building a portable design system with @theme tokens
Earlier we saw how @theme registers custom colors and fonts. The real payoff shows up when you name tokens by role (--color-accent, --color-surface, --radius-card) and reuse them across every component.
@import "tailwindcss";
@theme {
--color-accent: #2563eb;
--color-accent-hover: #1d4ed8;
--color-surface: #ffffff;
--color-surface-soft: #f8fafc;
--color-ink: #0f172a;
--color-ink-muted: #475569;
--radius-card: 1rem;
--shadow-card: 0 4px 20px rgb(15 23 42 / 0.06);
}
Now your HTML reads like your design system:
<article
class="rounded-card bg-surface p-6 text-ink shadow-card
border border-slate-200"
>
<h3 class="text-lg font-semibold">Weekly digest</h3>
<p class="mt-2 text-sm text-ink-muted">
Every component updates automatically when you tweak one
token in src/app.css.
</p>
<button
class="mt-4 rounded-lg bg-accent px-4 py-2 text-sm
font-medium text-white hover:bg-accent-hover"
>
Subscribe
</button>
</article>
When a client asks to switch the brand color from blue to emerald next quarter, you change two lines inside @theme and recompile. Every button, badge, and link across fifty pages updates in one shot.
Arbitrary values (w-[420px]) and when to use them
Sometimes a layout needs one exact measurement that does not belong in your global token list, like a 420px sidebar card or a calculated hero height.
Square brackets let you pass any valid CSS value directly inside a class:
<aside
class="w-full max-w-[420px] min-h-[calc(100dvh-4rem)]
rounded-2xl border border-slate-200 bg-[#faf9f6] p-6
shadow-[0_12px_32px_rgb(15_23_42/0.10)]"
>
<h2 class="text-lg font-semibold text-slate-900">
Exact dimensions when you need them
</h2>
<p class="mt-2 max-w-[48ch] text-sm text-slate-600">
Use underscores wherever CSS expects a space inside an
arbitrary value.
</p>
</aside>
Keep three small rules in mind when reaching for square brackets:
- Replace spaces with underscores (
_), such asgrid-cols-[180px_1fr_220px]orshadow-[0_4px_12px_rgb(0_0_0/0.1)]. - In Tailwind v4, you can read any existing CSS variable with parentheses instead of brackets:
bg-(--brand-bg)compiles tobackground-color: var(--brand-bg). - If you copy the same arbitrary value like
bg-[#faf9f6]into more than two components, promote it into@themeas--color-paperso it stays easy to maintain.
Portable components with container queries (@container, @md:flex-row)
Media queries (md:, lg:) only ask how wide the whole browser window is. That breaks down when you drop the same card component into a wide main column on one page and a narrow 320px sidebar on another.
With Tailwind v4’s built-in container queries, a component adapts to the width of its own parent wrapper instead:
| Container variant | Minimum container width | Typical context |
|---|---|---|
@xs: | 20rem (320px) | Narrow sidebar card |
@sm: | 24rem (384px) | Standard widget column |
@md: | 28rem (448px) | Medium dashboard card |
@lg: | 32rem (512px) | Wide split card |
@xl: | 36rem (576px) | Feature banner |
@2xl: | 42rem (672px) | Full content panel |
Wrap any reusable card in @container, then switch from stacked to horizontal at @md::
<div class="@container">
<article
class="flex flex-col gap-4 rounded-2xl border
border-slate-200 bg-white p-4
@md:flex-row @md:items-center @md:p-6"
>
<div
class="flex h-32 w-full shrink-0 items-center
justify-center rounded-xl bg-blue-50 font-mono
text-sm font-semibold text-blue-700
@md:size-24"
>
v4.3
</div>
<div class="min-w-0 flex-1">
<h3 class="text-base font-semibold text-slate-900">
Release notes
</h3>
<p class="mt-1 text-sm text-slate-600">
This card stacks cleanly in a narrow sidebar and
switches to a horizontal row in a wide column.
</p>
</div>
<a
href="/changelog"
class="shrink-0 rounded-lg bg-slate-900 px-3.5 py-2
text-center text-sm font-medium text-white
hover:bg-slate-800"
>
Read notes
</a>
</article>
</div>
Drop that exact markup into a 300px sidebar and it renders vertically. Drop it into a 600px dashboard panel and @md:flex-row turns it into a crisp horizontal strip with zero extra classes.
Clean, respectful animations with motion-reduce:
Good UI animations feel quick and quiet (duration-150 to duration-200), and they always respect users who turned on Reduce Motion in their operating system.
Here is a zero-JavaScript FAQ accordion built with native <details>, group-open:rotate-180, transition, and motion-reduce:transition-none:
<details
class="group max-w-xl rounded-xl border border-slate-200
bg-white p-5 open:border-blue-300"
>
<summary
class="flex cursor-pointer list-none items-center
justify-between gap-4 font-semibold text-slate-900"
>
<span>Does Tailwind v4 need a JavaScript config file?</span>
<span
class="inline-block text-slate-400 transition-transform
duration-200 group-open:rotate-180
motion-reduce:transition-none"
>
v
</span>
</summary>
<p class="mt-3 text-sm/relaxed text-slate-600">
No. You configure custom colors, fonts, and breakpoints in
CSS using the @theme block inside your main stylesheet.
</p>
</details>
For loading states, pair animate-pulse with motion-reduce:animate-none so readers who disable motion see a calm static skeleton card:
<div
class="max-w-sm rounded-xl border border-slate-200 bg-white
p-5"
aria-busy="true"
>
<div
class="animate-pulse space-y-3 motion-reduce:animate-none"
>
<div class="h-4 w-1/3 rounded bg-slate-200"></div>
<div class="h-3 w-full rounded bg-slate-200"></div>
<div class="h-3 w-5/6 rounded bg-slate-200"></div>
</div>
</div>
Complete project: building a responsive landing and pricing page
Let’s put everything we learned together by building a complete, production-ready storefront page (BookShelf, an independent developer ebook library) from scratch using Tailwind CSS v4.
Step 1: Set up the tokens in src/app.css
We declare our brand palette, semantic paper surface, font stack, and class-based dark mode toggle in one short file:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
@theme {
--color-brand: #2563eb;
--color-brand-dark: #1d4ed8;
--color-paper: #faf9f6;
--font-sans: "Inter", ui-sans-serif, system-ui, sans-serif;
--font-mono: "Geist Mono", ui-monospace, monospace;
}
@layer base {
html {
color-scheme: light dark;
}
}
Step 2: Sticky frosted-glass navbar
<header class="sticky top-0 z-30 border-b border-slate-200/80 bg-paper/85 backdrop-blur-md dark:border-slate-800 dark:bg-slate-950/85">
<div class="mx-auto flex max-w-6xl items-center justify-between px-6 py-4">
<a href="/" class="flex items-center gap-2 font-mono text-base font-bold tracking-tight text-slate-900 dark:text-white">
<span class="size-2.5 rounded-full bg-brand"></span>
BookShelf
</a>
<nav class="hidden items-center gap-7 text-sm font-medium text-slate-600 md:flex dark:text-slate-300">
<a href="#features" class="hover:text-slate-900 dark:hover:text-white">Features</a>
<a href="#pricing" class="hover:text-slate-900 dark:hover:text-white">Pricing</a>
<a href="#newsletter" class="hover:text-slate-900 dark:hover:text-white">Newsletter</a>
</nav>
<a
href="#pricing"
class="rounded-lg bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-800 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-slate-900 dark:bg-white dark:text-slate-900 dark:hover:bg-slate-200"
>
Get access
</a>
</div>
</header>
Step 3: Centered hero section
<section class="mx-auto max-w-4xl px-6 pt-20 pb-16 text-center md:pt-28">
<p class="inline-flex items-center gap-2 rounded-full border border-blue-200 bg-blue-50 px-3.5 py-1 text-xs font-semibold text-blue-700 dark:border-blue-900 dark:bg-blue-950/60 dark:text-blue-300">
Updated for Tailwind CSS v4.3
</p>
<h1 class="mt-6 text-4xl/tight font-bold tracking-tight text-balance text-slate-900 sm:text-5xl/tight md:text-6xl/tight dark:text-white">
Practical developer handbooks you can finish in a weekend
</h1>
<p class="mx-auto mt-6 max-w-2xl text-lg/relaxed text-pretty text-slate-600 dark:text-slate-300">
Skip thousand-page textbooks. Every handbook starts from zero, sticks to real runnable code, and ships in PDF (A4 and US Letter) plus EPUB.
</p>
<div class="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
<a
href="#pricing"
class="w-full rounded-lg bg-brand px-6 py-3 text-sm font-semibold text-white shadow-xs transition-colors hover:bg-brand-dark sm:w-auto"
>
Browse the library
</a>
<a
href="#features"
class="w-full rounded-lg border border-slate-300 bg-white px-6 py-3 text-sm font-semibold text-slate-700 transition-colors hover:bg-slate-50 sm:w-auto dark:border-slate-700 dark:bg-slate-900 dark:text-slate-200 dark:hover:bg-slate-800"
>
See what is inside
</a>
</div>
</section>
Step 4: Feature grid with @container cards
<section id="features" class="mx-auto max-w-6xl px-6 py-16">
<div class="grid grid-cols-1 gap-6 md:grid-cols-3">
<div class="@container">
<article class="flex h-full flex-col gap-3 rounded-2xl border border-slate-200 bg-white p-6 shadow-2xs @sm:p-7 dark:border-slate-800 dark:bg-slate-900">
<span class="font-mono text-xs font-semibold text-brand uppercase">01 / Formats</span>
<h2 class="text-lg font-semibold text-slate-900 dark:text-white">A4, US Letter, and EPUB</h2>
<p class="text-sm/relaxed text-slate-600 dark:text-slate-300">
Read on Kindle, Apple Books, iPad, or print clean 14pt pages on either European A4 or US Letter paper.
</p>
</article>
</div>
<div class="@container">
<article class="flex h-full flex-col gap-3 rounded-2xl border border-slate-200 bg-white p-6 shadow-2xs @sm:p-7 dark:border-slate-800 dark:bg-slate-900">
<span class="font-mono text-xs font-semibold text-brand uppercase">02 / Verified</span>
<h2 class="text-lg font-semibold text-slate-900 dark:text-white">Real runnable examples</h2>
<p class="text-sm/relaxed text-slate-600 dark:text-slate-300">
Every code snippet, terminal command, and output is tested live against current tools instead of recycled docs.
</p>
</article>
</div>
<div class="@container">
<article class="flex h-full flex-col gap-3 rounded-2xl border border-slate-200 bg-white p-6 shadow-2xs @sm:p-7 dark:border-slate-800 dark:bg-slate-900">
<span class="font-mono text-xs font-semibold text-brand uppercase">03 / Beginner-first</span>
<h2 class="text-lg font-semibold text-slate-900 dark:text-white">One concept at a time</h2>
<p class="text-sm/relaxed text-slate-600 dark:text-slate-300">
Chapters build in strict prerequisite order so you never hit jargon before learning the fundamentals first.
</p>
</article>
</div>
</div>
</section>
Step 5: Two-tier pricing cards with tabular-nums
<section id="pricing" class="mx-auto max-w-4xl px-6 py-16">
<div class="text-center">
<h2 class="text-3xl font-bold tracking-tight text-slate-900 dark:text-white">Simple pricing</h2>
<p class="mt-2 text-slate-600 dark:text-slate-300">Start free, upgrade only if you want workshops.</p>
</div>
<div class="mt-10 grid grid-cols-1 gap-6 md:grid-cols-2">
<div class="flex flex-col rounded-2xl border border-slate-200 bg-white p-8 dark:border-slate-800 dark:bg-slate-900">
<h3 class="text-lg font-semibold text-slate-900 dark:text-white">Free Library</h3>
<p class="mt-2 text-sm text-slate-600 dark:text-slate-300">Every programming handbook in PDF and EPUB.</p>
<p class="mt-6 text-4xl font-bold tracking-tight text-slate-900 tabular-nums dark:text-white">$0</p>
<ul class="mt-6 flex-1 space-y-2.5 text-sm text-slate-600 dark:text-slate-300">
<li>All 46 developer handbooks</li>
<li>A4, US Letter, and EPUB downloads</li>
<li>Lifetime updates when new versions ship</li>
</ul>
<a
href="/download/"
class="mt-8 block rounded-lg border border-slate-300 px-4 py-2.5 text-center text-sm font-semibold text-slate-900 hover:bg-slate-50 dark:border-slate-700 dark:text-white dark:hover:bg-slate-800"
>
Download free books
</a>
</div>
<div class="flex flex-col rounded-2xl border-2 border-brand bg-white p-8 shadow-md dark:bg-slate-900">
<div class="flex items-center justify-between">
<h3 class="text-lg font-semibold text-slate-900 dark:text-white">Pro Workshops</h3>
<span class="rounded-full bg-blue-50 px-2.5 py-0.5 text-xs font-semibold text-brand dark:bg-blue-950">
Popular
</span>
</div>
<p class="mt-2 text-sm text-slate-600 dark:text-slate-300">Hands-on projects, video walkthroughs, and community.</p>
<p class="mt-6 text-4xl font-bold tracking-tight text-slate-900 tabular-nums dark:text-white">$99</p>
<ul class="mt-6 flex-1 space-y-2.5 text-sm text-slate-600 dark:text-slate-300">
<li>Everything in Free Library</li>
<li>Complete real-world project repos</li>
<li>Direct Q&A and code reviews</li>
</ul>
<a
href="/courses/"
class="mt-8 block rounded-lg bg-brand px-4 py-2.5 text-center text-sm font-semibold text-white hover:bg-brand-dark"
>
Explore workshops
</a>
</div>
</div>
</section>
Step 6: Newsletter bar and clean footer
<section id="newsletter" class="mx-auto max-w-3xl px-6 py-16">
<div class="rounded-2xl border border-slate-200 bg-white p-8 text-center shadow-2xs dark:border-slate-800 dark:bg-slate-900">
<h2 class="text-2xl font-bold tracking-tight text-slate-900 dark:text-white">Get new handbooks first</h2>
<p class="mt-2 text-sm text-slate-600 dark:text-slate-300">One practical email per week. Unsubscribe in one click.</p>
<form class="mx-auto mt-6 flex max-w-md flex-col gap-3 sm:flex-row">
<label for="nl-email" class="sr-only">Email address</label>
<input
id="nl-email"
type="email"
required
placeholder="you@domain.com"
class="min-w-0 flex-1 rounded-lg border border-slate-300 bg-white px-3.5 py-2.5 text-sm text-slate-900 placeholder:text-slate-400 focus:border-brand focus:ring-3 focus:ring-blue-500/20 focus:outline-hidden dark:border-slate-700 dark:bg-slate-950 dark:text-white"
/>
<button
type="submit"
class="shrink-0 rounded-lg bg-brand px-5 py-2.5 text-sm font-semibold text-white hover:bg-brand-dark"
>
Subscribe
</button>
</form>
</div>
</section>
Compile that entire landing page with npx @tailwindcss/cli -i src/app.css -o dist/app.css --minify, and the whole production stylesheet lands under 4 KB gzipped with dark mode, responsive grids, container queries, and focus states included!
Real-world workflow, pitfalls, and upgrading from v3
Once you use Tailwind on production apps for a few weeks, five practical habits save hours of debugging.
1. Never construct class names dynamically with string interpolation
This is the #1 Tailwind mistake. Because Oxide scans your source files as static plain text without running JavaScript, dynamically glued strings are invisible to the scanner:
// BROKEN: Tailwind never sees "bg-red-600" or "bg-emerald-600" in source code
const badgeClass = `bg-${color}-600 text-white`
Always write complete, unbroken class strings inside a lookup map:
const BADGE_CLASSES = {
error: 'bg-red-600 text-white',
success: 'bg-emerald-600 text-white',
info: 'bg-blue-600 text-white',
}
const badgeClass = BADGE_CLASSES[status]
I cover this in detail in Tailwind CSS cannot generate classes dynamically.
2. Scanning shared UI packages in monorepos (@source)
Tailwind v4 ignores node_modules and gitignored folders by default. If your app imports components from a sibling workspace package or external folder, register it with @source right inside src/app.css:
@import "tailwindcss";
@source "../packages/ui/src";
3. Merging conflicting props in reusable components (tailwind-merge)
Suppose you write a <Button className="bg-blue-600 px-4 py-2" /> component in React or Astro that accepts an extra class prop, and a caller passes <Button class="bg-red-600" />.
In CSS, whichever class appears later in the generated stylesheet wins, not whichever came second in the HTML class string!
Combine clsx and tailwind-merge into a 4-line cn() helper so caller overrides always win predictably:
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
cn('bg-blue-600 px-4 py-2', 'bg-red-600')
// 'px-4 py-2 bg-red-600'
4. Automatic class sorting with Prettier
Stop debating class order in code reviews. Install prettier-plugin-tailwindcss:
npm install -D prettier prettier-plugin-tailwindcss
Every time you save a file, Prettier sorts utility classes into Tailwind’s recommended order (layout → spacing → sizing → typography → colors → borders → states) automatically.
5. Upgrading a Tailwind v3 project to v4
Run Tailwind’s official automated upgrade CLI on a clean git branch:
npx @tailwindcss/upgrade
Review the diff and check these five common v3-to-v4 renames:
| Tailwind v3 | Tailwind v4 | Why it changed |
|---|---|---|
shadow-sm / shadow | shadow-xs / shadow-sm | Scale shifted down one step for softer defaults |
rounded-sm / rounded | rounded-xs / rounded-sm | Matches the updated 2xs/xs/sm naming |
bg-gradient-to-r | bg-linear-to-r | Aligns with bg-radial and bg-conic |
outline-none | outline-hidden | Keeps accessible outlines in Windows Forced Colors mode |
!flex | flex! | Trailing ! avoids conflicts with other syntax |
border (default gray) | border border-slate-200 | Default border color is now currentColor |
Eight copy-and-paste UI recipes in Tailwind v4
Most real websites are built from the same handful of core components. Keep these eight self-contained Tailwind v4 recipes handy whenever you spin up a new page or prototype.
Every recipe uses standard HTML, supports responsive screens, and works cleanly with zero JavaScript frameworks required.
1. Responsive navbar with a zero-JS mobile menu
Desktop visitors see a clean horizontal link row, while phone visitors get a native <details> dropdown that opens and closes with zero JavaScript.
<header
class="sticky top-0 z-30 border-b border-slate-200
bg-white/90 backdrop-blur-md"
>
<div
class="mx-auto flex max-w-6xl items-center justify-between
gap-4 px-4 py-3.5 sm:px-6"
>
<a
href="/"
class="flex items-center gap-2 font-bold text-slate-900"
>
<span class="size-2.5 rounded-full bg-blue-600"></span>
<span>DevNotes</span>
</a>
<nav
class="hidden items-center gap-6 text-sm font-medium
text-slate-600 md:flex"
>
<a href="/guides" class="hover:text-slate-900">Guides</a>
<a href="/pricing" class="hover:text-slate-900">Pricing</a>
<a href="/blog" class="hover:text-slate-900">Blog</a>
</nav>
<div class="hidden items-center gap-3 md:flex">
<a
href="/login"
class="text-sm font-medium text-slate-600
hover:text-slate-900"
>
Log in
</a>
<a
href="/signup"
class="rounded-lg bg-blue-600 px-3.5 py-2 text-sm
font-medium text-white hover:bg-blue-700"
>
Start free
</a>
</div>
<details class="relative md:hidden">
<summary
class="cursor-pointer list-none rounded-lg border
border-slate-300 px-3 py-1.5 text-sm font-medium
text-slate-700"
>
Menu
</summary>
<div
class="absolute right-0 mt-2 w-48 rounded-xl border
border-slate-200 bg-white p-2 shadow-lg"
>
<a
href="/guides"
class="block rounded-lg px-3 py-2 text-sm
text-slate-700 hover:bg-slate-100"
>
Guides
</a>
<a
href="/pricing"
class="block rounded-lg px-3 py-2 text-sm
text-slate-700 hover:bg-slate-100"
>
Pricing
</a>
<a
href="/signup"
class="mt-1 block rounded-lg bg-blue-600 px-3 py-2
text-center text-sm font-medium text-white"
>
Start free
</a>
</div>
</details>
</div>
</header>
Notice hidden md:flex on the desktop nav and md:hidden on the mobile <details> menu so only one navigation control appears at any screen width.
2. Hero section with trust metrics
A sharp hero needs clear hierarchy: a small eyebrow badge, a balanced headline, a short two-line summary, clear action buttons, and grounded proof underneath.
<section class="mx-auto max-w-4xl px-6 py-20 text-center">
<span
class="inline-flex items-center rounded-full border
border-blue-200 bg-blue-50 px-3 py-1 text-xs
font-semibold text-blue-700"
>
New in Tailwind CSS v4
</span>
<h1
class="mt-6 text-4xl/tight font-bold tracking-tight
text-balance text-slate-900 sm:text-5xl/tight"
>
Build fast interfaces without fighting giant stylesheets
</h1>
<p
class="mx-auto mt-5 max-w-2xl text-base/relaxed text-pretty
text-slate-600 sm:text-lg/relaxed"
>
Compose small utility classes directly in your markup and
ship a tiny CSS bundle built for production.
</p>
<div
class="mt-8 flex flex-col justify-center gap-3 sm:flex-row"
>
<a
href="#start"
class="rounded-lg bg-blue-600 px-6 py-3 text-sm
font-semibold text-white shadow-xs
hover:bg-blue-700"
>
Get started
</a>
<a
href="#recipes"
class="rounded-lg border border-slate-300 bg-white px-6
py-3 text-sm font-semibold text-slate-700
hover:bg-slate-50"
>
Browse examples
</a>
</div>
<dl
class="mx-auto mt-14 grid max-w-xl grid-cols-3 gap-6
border-t border-slate-200 pt-8"
>
<div>
<dt class="text-xs text-slate-500">Build time</dt>
<dd class="mt-1 text-xl font-bold text-slate-900">
24 ms
</dd>
</div>
<div>
<dt class="text-xs text-slate-500">Gzipped CSS</dt>
<dd class="mt-1 text-xl font-bold text-slate-900">
4 KB
</dd>
</div>
<div>
<dt class="text-xs text-slate-500">Config files</dt>
<dd class="mt-1 text-xl font-bold text-slate-900">0</dd>
</div>
</dl>
</section>
Putting text-balance on the <h1> keeps multi-line headings visually even, while flex-col sm:flex-row gives phone visitors full-width tap targets.
3. Three-tier pricing card grid
Here is a balanced three-column pricing grid with an accented middle plan and aligned bottom buttons across cards of different feature lengths.
<section class="mx-auto max-w-6xl px-6 py-12">
<div class="grid grid-cols-1 gap-6 md:grid-cols-3">
<div
class="flex flex-col rounded-2xl border border-slate-200
bg-white p-7"
>
<h3 class="text-base font-semibold text-slate-900">
Starter
</h3>
<p class="mt-4 text-4xl font-bold text-slate-900 tabular-nums">
$0
</p>
<p class="mt-1 text-sm text-slate-500">
For personal side projects.
</p>
<ul class="mt-6 flex-1 space-y-2.5 text-sm text-slate-600">
<li>Up to 3 projects</li>
<li>Community forum access</li>
<li>Basic analytics</li>
</ul>
<a
href="/signup"
class="mt-8 block rounded-lg border border-slate-300
px-4 py-2.5 text-center text-sm font-semibold
text-slate-900 hover:bg-slate-50"
>
Start free
</a>
</div>
<div
class="flex flex-col rounded-2xl border-2 border-blue-600
bg-white p-7 shadow-md"
>
<div class="flex items-center justify-between">
<h3 class="text-base font-semibold text-slate-900">
Pro
</h3>
<span
class="rounded-full bg-blue-50 px-2.5 py-0.5 text-xs
font-semibold text-blue-700"
>
Popular
</span>
</div>
<p class="mt-4 text-4xl font-bold text-slate-900 tabular-nums">
$19
<span class="text-sm font-normal text-slate-500">/mo</span>
</p>
<p class="mt-1 text-sm text-slate-500">
For active indie developers.
</p>
<ul class="mt-6 flex-1 space-y-2.5 text-sm text-slate-600">
<li>Unlimited projects</li>
<li>Custom domains and SSL</li>
<li>Daily automated backups</li>
<li>Email support</li>
</ul>
<a
href="/signup?plan=pro"
class="mt-8 block rounded-lg bg-blue-600 px-4 py-2.5
text-center text-sm font-semibold text-white
hover:bg-blue-700"
>
Choose Pro
</a>
</div>
<div
class="flex flex-col rounded-2xl border border-slate-200
bg-white p-7"
>
<h3 class="text-base font-semibold text-slate-900">
Team
</h3>
<p class="mt-4 text-4xl font-bold text-slate-900 tabular-nums">
$49
<span class="text-sm font-normal text-slate-500">/mo</span>
</p>
<p class="mt-1 text-sm text-slate-500">
For small product teams.
</p>
<ul class="mt-6 flex-1 space-y-2.5 text-sm text-slate-600">
<li>Up to 10 team seats</li>
<li>Role-based permissions</li>
<li>Shared audit logs</li>
</ul>
<a
href="/signup?plan=team"
class="mt-8 block rounded-lg border border-slate-300
px-4 py-2.5 text-center text-sm font-semibold
text-slate-900 hover:bg-slate-50"
>
Start team trial
</a>
</div>
</div>
</section>
Because each card uses flex flex-col and every <ul> carries flex-1, the bottom action buttons line up horizontally even when the Pro card lists extra features.
4. Clean form with an inline error state
Clear validation feedback pairs a red border with accessible aria-invalid and aria-describedby attributes so both sighted and screen-reader users know what to fix.
<form
class="max-w-md space-y-5 rounded-2xl border border-slate-200
bg-white p-6"
>
<div>
<label
for="contact-name"
class="block text-sm font-medium text-slate-700"
>
Name
</label>
<input
id="contact-name"
type="text"
value="Flavio"
class="mt-1.5 block w-full rounded-lg border
border-slate-300 px-3.5 py-2 text-sm
text-slate-900 focus:border-blue-600
focus:ring-3 focus:ring-blue-600/20
focus:outline-hidden"
/>
</div>
<div>
<label
for="contact-email"
class="block text-sm font-medium text-slate-700"
>
Email address
</label>
<input
id="contact-email"
type="email"
value="flavio@"
aria-invalid="true"
aria-describedby="email-error"
class="mt-1.5 block w-full rounded-lg border
border-red-500 bg-red-50/40 px-3.5 py-2 text-sm
text-slate-900 focus:border-red-600
focus:ring-3 focus:ring-red-500/20
focus:outline-hidden"
/>
<p id="email-error" class="mt-1.5 text-xs text-red-600">
Enter a complete email address like name@domain.com.
</p>
</div>
<div>
<label
for="contact-msg"
class="block text-sm font-medium text-slate-700"
>
Message
</label>
<textarea
id="contact-msg"
rows="3"
placeholder="How can we help?"
class="mt-1.5 block w-full rounded-lg border
border-slate-300 px-3.5 py-2 text-sm
text-slate-900 focus:border-blue-600
focus:ring-3 focus:ring-blue-600/20
focus:outline-hidden"
></textarea>
</div>
<button
type="submit"
class="w-full rounded-lg bg-blue-600 px-4 py-2.5 text-sm
font-semibold text-white hover:bg-blue-700
focus-visible:outline-2
focus-visible:outline-offset-2
focus-visible:outline-blue-600"
>
Send message
</button>
</form>
A faint red background tint (bg-red-50/40) paired with border-red-500 draws the eye straight to the broken field without screaming across the whole form.
5. Alert banners (info, warning, and error)
Alert banners communicate status quickly when each tone pairs a soft 50 background, a crisp 200 or 300 border, and high-contrast 900 text.
<div class="max-w-xl space-y-3">
<div
role="status"
class="flex items-start justify-between gap-4 rounded-xl
border border-blue-200 bg-blue-50 p-4 text-sm
text-blue-900"
>
<div>
<p class="font-semibold">Update available</p>
<p class="mt-0.5 text-blue-800">
Tailwind CSS 4.3.5 is ready with faster rebuilds.
</p>
</div>
<a
href="/changelog"
class="shrink-0 font-semibold underline
underline-offset-2"
>
View
</a>
</div>
<div
role="status"
class="rounded-xl border border-amber-300 bg-amber-50 p-4
text-sm text-amber-900"
>
<p class="font-semibold">Trial ends in 3 days</p>
<p class="mt-0.5 text-amber-800">
Add a billing method to keep your scheduled deploys live.
</p>
</div>
<div
role="alert"
class="rounded-xl border border-red-300 bg-red-50 p-4
text-sm text-red-900"
>
<p class="font-semibold">Deploy failed</p>
<p class="mt-0.5 text-red-800">
Missing API_KEY environment variable in production.
</p>
</div>
</div>
Use role="status" for informational or warning notices, and reserve role="alert" for urgent failures that need immediate attention.
6. Data table with zebra rows
Alternating row stripes (odd:bg-white even:bg-slate-50) make wide tables much easier to scan across rows without adding heavy visual clutter.
<div
class="overflow-x-auto rounded-xl border border-slate-200
bg-white"
>
<table class="min-w-full text-left text-sm">
<thead
class="border-b border-slate-200 bg-slate-100 text-xs
font-semibold tracking-wider text-slate-600
uppercase"
>
<tr>
<th class="px-4 py-3">Invoice</th>
<th class="px-4 py-3">Customer</th>
<th class="px-4 py-3">Status</th>
<th class="px-4 py-3 text-right">Amount</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-200">
<tr class="odd:bg-white even:bg-slate-50 hover:bg-blue-50/40">
<td class="px-4 py-3 font-mono text-slate-900">#1041</td>
<td class="px-4 py-3 text-slate-700">Elena Rossi</td>
<td class="px-4 py-3">
<span
class="rounded-full bg-emerald-100 px-2.5 py-0.5
text-xs font-medium text-emerald-800"
>
Paid
</span>
</td>
<td
class="px-4 py-3 text-right font-mono font-medium
text-slate-900 tabular-nums"
>
$49.00
</td>
</tr>
<tr class="odd:bg-white even:bg-slate-50 hover:bg-blue-50/40">
<td class="px-4 py-3 font-mono text-slate-900">#1042</td>
<td class="px-4 py-3 text-slate-700">Marco Bianchi</td>
<td class="px-4 py-3">
<span
class="rounded-full bg-amber-100 px-2.5 py-0.5
text-xs font-medium text-amber-800"
>
Pending
</span>
</td>
<td
class="px-4 py-3 text-right font-mono font-medium
text-slate-900 tabular-nums"
>
$129.00
</td>
</tr>
<tr class="odd:bg-white even:bg-slate-50 hover:bg-blue-50/40">
<td class="px-4 py-3 font-mono text-slate-900">#1043</td>
<td class="px-4 py-3 text-slate-700">Sara Conti</td>
<td class="px-4 py-3">
<span
class="rounded-full bg-emerald-100 px-2.5 py-0.5
text-xs font-medium text-emerald-800"
>
Paid
</span>
</td>
<td
class="px-4 py-3 text-right font-mono font-medium
text-slate-900 tabular-nums"
>
$19.00
</td>
</tr>
</tbody>
</table>
</div>
Right-aligning currency columns with text-right font-mono tabular-nums lines every decimal point straight down the column.
7. Avatar and badge stack
Overlapping avatar stacks use negative horizontal spacing (-space-x-2) paired with a crisp ring (ring-2 ring-white) so each circle cuts cleanly out from its neighbor.
<div
class="flex flex-wrap items-center justify-between gap-4
rounded-xl border border-slate-200 bg-white p-4"
>
<div class="flex items-center gap-3">
<div class="flex -space-x-2">
<span
class="flex size-9 items-center justify-center
rounded-full bg-blue-600 text-xs font-semibold
text-white ring-2 ring-white"
>
FC
</span>
<span
class="flex size-9 items-center justify-center
rounded-full bg-emerald-600 text-xs font-semibold
text-white ring-2 ring-white"
>
ER
</span>
<span
class="flex size-9 items-center justify-center
rounded-full bg-violet-600 text-xs font-semibold
text-white ring-2 ring-white"
>
MB
</span>
<span
class="flex size-9 items-center justify-center
rounded-full bg-slate-100 text-xs font-semibold
text-slate-700 ring-2 ring-white"
>
+4
</span>
</div>
<div>
<p class="text-sm font-semibold text-slate-900">
Core maintainers
</p>
<p class="text-xs text-slate-500">7 active reviewers</p>
</div>
</div>
<div class="flex flex-wrap items-center gap-1.5">
<span
class="inline-flex items-center gap-1.5 rounded-full
bg-emerald-50 px-2.5 py-0.5 text-xs font-medium
text-emerald-700 ring-1 ring-emerald-600/20"
>
<span class="size-1.5 rounded-full bg-emerald-500"></span>
Online
</span>
<span
class="rounded-full bg-slate-100 px-2.5 py-0.5 text-xs
font-medium text-slate-700"
>
v4.3
</span>
</div>
</div>
Swap the initials <span> circles for <img> tags with size-9 rounded-full object-cover ring-2 ring-white whenever you have real profile photos.
8. Clean multi-column footer
A dependable footer collapses neatly to two columns on phones and expands to four columns on desktop, separated from the copyright bar by a light divider.
<footer class="border-t border-slate-200 bg-slate-50">
<div class="mx-auto max-w-6xl px-6 py-12">
<div class="grid grid-cols-2 gap-8 md:grid-cols-4">
<div class="col-span-2 md:col-span-1">
<a href="/" class="font-bold text-slate-900">DevNotes</a>
<p class="mt-2 max-w-xs text-sm text-slate-600">
Short, practical programming handbooks written for
working web developers.
</p>
</div>
<div>
<h3 class="text-xs font-semibold text-slate-900 uppercase">
Library
</h3>
<ul class="mt-3 space-y-2 text-sm text-slate-600">
<li>
<a href="/html" class="hover:text-slate-900">HTML</a>
</li>
<li>
<a href="/css" class="hover:text-slate-900">CSS</a>
</li>
<li>
<a href="/tailwind" class="hover:text-slate-900">
Tailwind
</a>
</li>
</ul>
</div>
<div>
<h3 class="text-xs font-semibold text-slate-900 uppercase">
Resources
</h3>
<ul class="mt-3 space-y-2 text-sm text-slate-600">
<li>
<a href="/blog" class="hover:text-slate-900">Blog</a>
</li>
<li>
<a href="/cheatsheet" class="hover:text-slate-900">
Cheat sheet
</a>
</li>
<li>
<a href="/rss" class="hover:text-slate-900">RSS</a>
</li>
</ul>
</div>
<div>
<h3 class="text-xs font-semibold text-slate-900 uppercase">
Legal
</h3>
<ul class="mt-3 space-y-2 text-sm text-slate-600">
<li>
<a href="/privacy" class="hover:text-slate-900">
Privacy
</a>
</li>
<li>
<a href="/terms" class="hover:text-slate-900">Terms</a>
</li>
</ul>
</div>
</div>
<div
class="mt-10 flex flex-col items-center justify-between
gap-3 border-t border-slate-200 pt-6 text-xs
text-slate-500 sm:flex-row"
>
<p>Copyright 2026 DevNotes. All rights reserved.</p>
<p>Built with Tailwind CSS v4.</p>
</div>
</div>
</footer>
Putting col-span-2 md:col-span-1 on the brand summary lets it take the full top row on phones while keeping the three link columns compact below it.
Everyday Tailwind UI recipes and common beginner mistakes
Most web pages reuse the same handful of small UI pieces. Here are four compact recipes you can drop into any HTML, React, or Astro project and tweak right away.
Primary, secondary, and pill badge
<div class="flex flex-wrap items-center gap-3">
<button class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700">
Save changes
</button>
<button class="rounded-lg border border-zinc-300 px-4 py-2 text-sm font-medium text-zinc-800 hover:bg-zinc-100">
Cancel
</button>
<span class="rounded-full bg-emerald-100 px-2.5 py-0.5 text-xs font-medium text-emerald-800">
Published
</span>
</div>
User avatar and author row
<div class="flex items-center gap-3">
<img
src="/images/flavio.jpg"
alt="Flavio Copes"
class="size-10 rounded-full object-cover"
/>
<div>
<p class="text-sm font-semibold text-zinc-900">Flavio Copes</p>
<p class="text-xs text-zinc-500">Updated December 5, 2026</p>
</div>
</div>
Notice size-10, which sets both width: 2.5rem and height: 2.5rem in one class instead of writing w-10 h-10.
Alert callout box
<div class="rounded-lg border border-amber-300 bg-amber-50 p-4 text-sm text-amber-900">
<strong class="font-semibold">Heads up:</strong>
Restart your dev server after editing environment variables.
</div>
Five common beginner Tailwind mistakes
- Building class names with string concatenation. Writing
`bg-${color}-600`breaks in production because Tailwind scans your files as plain text and only generates complete class strings it can see. Map colors in a lookup object ({ red: 'bg-red-600', blue: 'bg-blue-600' }) instead. - Thinking
sm:means “only on mobile”.sm:means640pxand wider! Always style mobile with unprefixed classes first. - Wonder why
<h1>and<ul>have no size or bullets. Tailwind’s Preflight reset strips default browser heading sizes and list bullets so you style them intentionally (or addprosefrom@tailwindcss/typographyfor Markdown articles). - Forgetting
flex-wrapon button rows and navbars. Withoutflex-wrap, a row of buttons overflows narrow phone screens horizontally. - Hardcoding
bg-white text-blackwhen supporting dark mode. Pair them withdark:bg-zinc-900 dark:text-zinc-100(or use semantic CSS variables in@theme).
Everyday Tailwind layout patterns for beginners
Beyond individual buttons and badges, five small layout patterns cover almost every page structure you will ever build.
1. Centered page container with breathing room
<main class="mx-auto max-w-4xl px-4 py-10">
<h1 class="text-3xl font-bold">Welcome</h1>
</main>
mx-auto centers the container, max-w-4xl caps line width on wide monitors, and px-4 keeps text from touching phone edges.
2. Sticky footer on short pages
<body class="flex min-h-dvh flex-col">
<header class="border-b p-4">Header</header>
<main class="flex-1 p-4">Short page content</main>
<footer class="border-t p-4 text-sm text-zinc-500">Footer</footer>
</body>
Putting flex-1 on <main> makes it grow to fill any leftover height so the footer always rests cleanly at the bottom.
3. Equal-height cards with aligned bottom buttons
When three cards sit in a row and one has a longer description, put flex flex-col on each card and mt-auto on the button so every button lines up along the bottom edge:
<div class="grid gap-4 md:grid-cols-3">
<article class="flex flex-col rounded-xl border p-5">
<h2 class="font-semibold">Starter</h2>
<p class="mt-2 text-sm text-zinc-600">Great for personal blogs.</p>
<a href="/buy" class="mt-auto pt-4 text-sm font-medium text-blue-600">
Choose Starter
</a>
</article>
</div>
4. Responsive sidebar + main content
<div class="mx-auto grid max-w-6xl gap-8 px-4 lg:grid-cols-[240px_1fr]">
<aside class="rounded-lg border p-4">Sidebar</aside>
<main class="min-w-0">Main article content</main>
</div>
Notice min-w-0 on <main>. Without min-w-0, wide code blocks inside a CSS Grid column can stretch the column past the screen edge!
5. Horizontal scrollable pill bar on phones
<div class="flex gap-2 overflow-x-auto pb-2">
<a href="/html" class="shrink-0 rounded-full border px-3 py-1 text-sm">HTML</a>
<a href="/css" class="shrink-0 rounded-full border px-3 py-1 text-sm">CSS</a>
<a href="/js" class="shrink-0 rounded-full border px-3 py-1 text-sm">JS</a>
</div>
Adding shrink-0 stops the pills from squishing into two lines while letting phone users swipe smoothly sideways.
Hands-on exercises
Reading utility classes helps, and typing them into a real HTML file locks them into muscle memory. Work through these ten progressive exercises in order.
Try writing your own version first in a starter index.html running @tailwindcss/cli --watch, then compare your markup against each solution.
Exercise 1: Pill badge and primary action button
Build a compact row containing a green status pill (“Available”) next to a blue button (“Book a call”).
- Give the button clear
hover:and keyboardfocus-visible:states. - Wrap the row with
flex-wrapso it stays tidy on narrow screens.
Hint: Combine rounded-full on the badge with rounded-lg and focus-visible:outline-2 on the button.
Here is one clean solution:
<div class="flex flex-wrap items-center gap-3">
<span
class="inline-flex items-center gap-1.5 rounded-full
bg-emerald-50 px-3 py-1 text-xs font-medium
text-emerald-700 ring-1 ring-emerald-600/20"
>
<span class="size-1.5 rounded-full bg-emerald-500"></span>
Available
</span>
<button
type="button"
class="cursor-pointer rounded-lg bg-blue-600 px-4 py-2
text-sm font-medium text-white transition-colors
hover:bg-blue-700 focus-visible:outline-2
focus-visible:outline-offset-2
focus-visible:outline-blue-600"
>
Book a call
</button>
</div>
Exercise 2: Compact author profile card
Create a horizontal profile card capped at max-w-sm with a circular initials avatar, a name, a long bio line that truncates cleanly, and a “Follow” button on the right.
- Keep the avatar from squishing with
shrink-0. - Allow the bio text to truncate with
min-w-0 flex-1andtruncate.
Hint: Remember that truncate inside a flex row needs min-w-0 on its flex parent.
Here is one clean solution:
<article
class="flex max-w-sm items-center gap-3 rounded-xl border
border-slate-200 bg-white p-4 shadow-2xs"
>
<div
class="flex size-11 shrink-0 items-center justify-center
rounded-full bg-slate-900 text-sm font-bold
text-white"
>
FC
</div>
<div class="min-w-0 flex-1">
<h3 class="truncate text-sm font-semibold text-slate-900">
Flavio Copes
</h3>
<p class="truncate text-xs text-slate-500">
Writing practical web development handbooks every week
</p>
</div>
<button
type="button"
class="shrink-0 rounded-lg border border-slate-300 px-3
py-1.5 text-xs font-semibold text-slate-700
hover:bg-slate-50"
>
Follow
</button>
</article>
Exercise 3: Responsive 3-column feature grid
Create three feature cards that stack in one column on phones, switch to two columns at sm:, and expand to three columns at lg:.
- Make the third card span two columns at
sm:(sm:col-span-2) so there is no empty hole on tablets, then return to one column atlg:.
Hint: Put grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 on the container.
Here is one clean solution:
<section
class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3"
>
<article
class="rounded-xl border border-slate-200 bg-white p-5"
>
<h3 class="font-semibold text-slate-900">Fast builds</h3>
<p class="mt-1.5 text-sm text-slate-600">
Oxide compiles only the utilities your templates use.
</p>
</article>
<article
class="rounded-xl border border-slate-200 bg-white p-5"
>
<h3 class="font-semibold text-slate-900">OKLCH colors</h3>
<p class="mt-1.5 text-sm text-slate-600">
Balanced brightness across every hue from 50 to 950.
</p>
</article>
<article
class="rounded-xl border border-slate-200 bg-white p-5
sm:col-span-2 lg:col-span-1"
>
<h3 class="font-semibold text-slate-900">CSS-first theme</h3>
<p class="mt-1.5 text-sm text-slate-600">
Customize design tokens right inside your CSS file.
</p>
</article>
</section>
Exercise 4: Centered empty-state box
Build a min-h-72 dashed-border empty state that centers its icon circle, title, short description, and button both horizontally and vertically.
- Use
grid place-items-centeron the outer wrapper. - Keep the inner content readable with
max-w-xs text-center.
Hint: Place a single centered <div class="max-w-xs text-center"> inside the grid place-items-center wrapper.
Here is one clean solution:
<section
class="grid min-h-72 place-items-center rounded-2xl border
border-dashed border-slate-300 bg-slate-50 p-6"
>
<div class="max-w-xs text-center">
<div
class="mx-auto flex size-12 items-center justify-center
rounded-full bg-blue-100 text-lg font-bold
text-blue-700"
>
+
</div>
<h2 class="mt-4 text-base font-semibold text-slate-900">
No bookmarks saved
</h2>
<p class="mt-1 text-sm text-slate-600">
Save your favorite guides here so you can reopen them
fast.
</p>
<button
type="button"
class="mt-5 rounded-lg bg-blue-600 px-4 py-2 text-sm
font-medium text-white hover:bg-blue-700"
>
Add bookmark
</button>
</div>
</section>
Exercise 5: Hover-interactive article card with group-hover:
Create a clickable <a href="#"> card where hovering anywhere on the card turns the border blue, highlights the title, and shifts an arrow right.
- Respect Reduce Motion with
motion-reduce:transition-none. - Add a clean
focus-visible:outline for keyboard users.
Hint: Put group on the <a> tag and group-hover:translate-x-1 on the arrow <span>.
Here is one clean solution:
<a
href="/guides/grid"
class="group block max-w-sm rounded-2xl border
border-slate-200 bg-white p-6 transition-colors
duration-200 hover:border-blue-500
focus-visible:outline-2
focus-visible:outline-offset-2
focus-visible:outline-blue-600
motion-reduce:transition-none"
>
<p class="text-xs font-semibold text-slate-500 uppercase">
Tutorial
</p>
<h3
class="mt-2 text-lg font-semibold text-slate-900
transition-colors group-hover:text-blue-600"
>
CSS Grid in 10 minutes
</h3>
<p class="mt-1.5 text-sm text-slate-600">
Build responsive multi-column pages with clean templates.
</p>
<p
class="mt-4 inline-flex items-center gap-1 text-sm
font-medium text-blue-600"
>
<span>Read tutorial</span>
<span
class="transition-transform duration-200
group-hover:translate-x-1
motion-reduce:transition-none"
>
->
</span>
</p>
</a>
Exercise 6: Selectable shipping options with has-[:checked]:
Build two radio card labels (“Standard” at $0 and “Express” at $12) where selecting a radio button highlights the entire card border and background.
- Align prices cleanly with
tabular-nums. - Use zero JavaScript.
Hint: Add has-[:checked]:border-blue-600 has-[:checked]:bg-blue-50/60 on each <label>.
Here is one clean solution:
<fieldset class="max-w-md space-y-3">
<legend class="mb-2 text-sm font-semibold text-slate-900">
Shipping speed
</legend>
<label
class="flex cursor-pointer items-center justify-between
rounded-xl border border-slate-200 bg-white p-4
has-[:checked]:border-blue-600
has-[:checked]:bg-blue-50/60"
>
<span class="flex items-center gap-3">
<input
type="radio"
name="shipping"
value="standard"
checked
class="size-4 accent-blue-600"
/>
<span>
<span class="block text-sm font-medium text-slate-900">
Standard (3 to 5 days)
</span>
</span>
</span>
<span class="text-sm font-semibold text-slate-900 tabular-nums">
$0
</span>
</label>
<label
class="flex cursor-pointer items-center justify-between
rounded-xl border border-slate-200 bg-white p-4
has-[:checked]:border-blue-600
has-[:checked]:bg-blue-50/60"
>
<span class="flex items-center gap-3">
<input
type="radio"
name="shipping"
value="express"
class="size-4 accent-blue-600"
/>
<span>
<span class="block text-sm font-medium text-slate-900">
Express (next day)
</span>
</span>
</span>
<span class="text-sm font-semibold text-slate-900 tabular-nums">
$12
</span>
</label>
</fieldset>
Exercise 7: Email input with live peer-invalid: error message
Build a newsletter email field that stays neutral while empty, turns red when the user types an invalid email, and reveals a helper paragraph below it.
- Place the
<input class="peer">before the error<p>. - Use
placeholder-shown:oruser-invalid:so an untouched empty field looks calm.
Hint: Put peer on <input> and hidden peer-user-invalid:block on the error message.
Here is one clean solution:
<div class="max-w-sm">
<label
for="sub-email"
class="block text-sm font-medium text-slate-700"
>
Email address
</label>
<input
id="sub-email"
type="email"
required
placeholder="you@example.com"
class="peer mt-1.5 block w-full rounded-lg border
border-slate-300 bg-white px-3.5 py-2 text-sm
text-slate-900 focus:border-blue-600
focus:outline-hidden
user-invalid:border-red-500"
/>
<p
class="mt-1.5 hidden text-xs text-red-600
peer-user-invalid:block"
>
That does not look like a valid email address.
</p>
</div>
Exercise 8: Portable card using @container
Build a podcast episode card wrapped in @container that stacks vertically in narrow columns and switches to a horizontal row at @md:.
- Make the cover box full-width (
h-28 w-full) when stacked and square (@md:size-20) when horizontal.
Hint: Put @container on the parent <div>, then put flex-col @md:flex-row @md:items-center on the <article>.
Here is one clean solution:
<div class="@container">
<article
class="flex flex-col gap-4 rounded-xl border
border-slate-200 bg-white p-4
@md:flex-row @md:items-center"
>
<div
class="flex h-28 w-full shrink-0 items-center
justify-center rounded-lg bg-indigo-600 text-sm
font-bold text-white @md:size-20"
>
EP 12
</div>
<div class="min-w-0 flex-1">
<h3 class="truncate font-semibold text-slate-900">
Why Tailwind v4 moved to CSS
</h3>
<p class="mt-1 text-sm text-slate-600">
A 25-minute look at @theme, Oxide, and container queries.
</p>
</div>
<button
type="button"
class="shrink-0 rounded-lg bg-slate-900 px-3.5 py-2
text-sm font-medium text-white hover:bg-slate-800"
>
Play
</button>
</article>
</div>
Exercise 9: Dark-mode stats bar with custom @theme tokens
Define custom --color-brand and --color-surface tokens inside @theme, then build a 3-column metrics strip that supports both light and dark themes.
- Format all numbers with
tabular-nums. - Keep borders subtle in both themes (
border-slate-200 dark:border-slate-800).
Hint: Declare @theme tokens in src/app.css, then use bg-surface and text-brand in HTML.
Here is one clean solution:
@import "tailwindcss";
@theme {
--color-brand: #2563eb;
--color-surface: #ffffff;
}
<dl
class="grid max-w-2xl grid-cols-1 divide-y divide-slate-200
rounded-2xl border border-slate-200 bg-surface
sm:grid-cols-3 sm:divide-x sm:divide-y-0
dark:divide-slate-800 dark:border-slate-800
dark:bg-slate-900"
>
<div class="p-5">
<dt class="text-xs font-medium text-slate-500 uppercase">
Readers
</dt>
<dd
class="mt-1 text-2xl font-bold text-brand tabular-nums
dark:text-blue-400"
>
48,200
</dd>
</div>
<div class="p-5">
<dt class="text-xs font-medium text-slate-500 uppercase">
Handbooks
</dt>
<dd
class="mt-1 text-2xl font-bold text-slate-900
tabular-nums dark:text-slate-100"
>
46
</dd>
</div>
<div class="p-5">
<dt class="text-xs font-medium text-slate-500 uppercase">
Avg rating
</dt>
<dd
class="mt-1 text-2xl font-bold text-slate-900
tabular-nums dark:text-slate-100"
>
4.9/5
</dd>
</div>
</dl>
Exercise 10: Mini docs layout shell
Combine sticky positioning, CSS Grid, and responsive design into a mini documentation page:
- A sticky frosted top bar (
sticky top-0) with the app name and a button. - A two-column desktop layout (
lg:grid-cols-[200px_1fr]) with a sticky sidebar (lg:sticky lg:top-20 lg:self-start) and a fluid<main class="min-w-0">article card.
Hint: Never forget lg:self-start on the sticky <aside> inside a grid!
Here is one clean solution:
<div class="min-h-dvh bg-slate-50 text-slate-900">
<header
class="sticky top-0 z-20 border-b border-slate-200
bg-white/85 backdrop-blur-md"
>
<div
class="mx-auto flex h-14 max-w-5xl items-center
justify-between px-4 sm:px-6"
>
<span class="font-bold text-slate-900">MiniDocs</span>
<a
href="#top"
class="rounded-lg bg-blue-600 px-3 py-1.5 text-xs
font-semibold text-white hover:bg-blue-700"
>
v4.3
</a>
</div>
</header>
<div
class="mx-auto grid max-w-5xl grid-cols-1 gap-6 px-4 py-8
sm:px-6 lg:grid-cols-[200px_1fr]"
>
<aside class="lg:sticky lg:top-20 lg:self-start">
<ul class="space-y-1 text-sm">
<li>
<a
href="#setup"
class="block rounded-lg bg-blue-50 px-3 py-2
font-medium text-blue-700"
>
Setup
</a>
</li>
<li>
<a
href="#tokens"
class="block rounded-lg px-3 py-2 text-slate-600
hover:bg-slate-100"
>
Theme tokens
</a>
</li>
</ul>
</aside>
<main
class="min-w-0 rounded-2xl border border-slate-200
bg-white p-6"
>
<h1 id="setup" class="text-2xl font-bold text-slate-900">
Setup
</h1>
<p class="mt-2 text-sm/relaxed text-slate-600">
Install Tailwind CSS v4 and import it from your main
stylesheet to start building responsive pages.
</p>
</main>
</div>
</div>
CSS-to-Tailwind quick reference
Keep these lookup tables open whenever you know the CSS property you want and need its Tailwind v4 class name fast.
Spacing, sizing, and layout
| CSS property | Tailwind v4 utilities | Example |
|---|---|---|
padding | p-*, px-*, py-*, pt-*, pr-*, pb-*, pl-*, ps-*, pe-* | px-6 py-4 |
margin | m-*, mx-auto, my-*, mt-*, -mt-* | mx-auto mt-8 |
width / height | w-*, h-*, size-*, min-w-*, max-w-*, min-h-dvh | w-full max-w-2xl size-10 |
display | block, inline-block, flex, inline-flex, grid, hidden | hidden md:flex |
overflow | overflow-hidden, overflow-auto, overflow-x-auto | overflow-x-auto |
aspect-ratio | aspect-square, aspect-video, aspect-4/3 | aspect-video |
position | relative, absolute, fixed, sticky, inset-0, top-0, z-20 | sticky top-0 z-20 |
Typography and colors
| CSS property | Tailwind v4 utilities | Example |
|---|---|---|
font-family | font-sans, font-serif, font-mono | font-mono |
font-size / line-height | text-xs..text-6xl, text-base/7, leading-relaxed | text-3xl/tight |
font-weight | font-normal, font-medium, font-semibold, font-bold | font-semibold |
text-align / text-wrap | text-left, text-center, text-right, text-balance, text-pretty | text-balance |
font-variant-numeric | tabular-nums, lining-nums | tabular-nums |
color / background-color | text-*, bg-*, bg-blue-600/15, bg-[#faf9f6] | bg-slate-900 text-white |
background-image | bg-linear-to-r, bg-radial, from-*, via-*, to-* | bg-linear-to-r from-blue-600 to-indigo-600 |
Flexbox, Grid, borders, and effects
| CSS property | Tailwind v4 utilities | Example |
|---|---|---|
flex-direction / flex-wrap | flex-row, flex-col, flex-wrap | flex flex-col gap-4 |
align-items / justify-content | items-center, items-start, justify-between, justify-center | items-center justify-between |
flex / flex-shrink | flex-1, flex-none, shrink-0, grow | min-w-0 flex-1 |
grid-template-columns | grid-cols-1..12, grid-cols-[240px_1fr], grid-cols-subgrid | grid grid-cols-1 md:grid-cols-3 |
gap | gap-*, gap-x-*, gap-y-* | gap-6 |
border / border-radius | border, border-t, border-slate-200, divide-y, rounded-xl | rounded-xl border border-slate-200 |
box-shadow / backdrop-filter | shadow-xs, shadow-sm, shadow-md, backdrop-blur-md | shadow-sm backdrop-blur-md |
transition / transform | transition-colors, duration-200, hover:-translate-y-0.5, scale-95 | transition duration-200 |
Where to go from here
You now know every piece of Tailwind CSS v4 you need to build real web apps from a blank HTML file to a customized, dark-mode-ready design system.
My advice is to open a small side project today, install @tailwindcss/vite or @tailwindcss/cli, set up three @theme tokens, and build one full screen without writing a single custom CSS class. After two or three sessions, the utility names become muscle memory.
To sharpen your HTML and CSS foundations alongside Tailwind, read my free CSS Handbook, HTML Handbook, and HTML Forms Handbook, and try the interactive Tailwind cheat sheet and converter on the online Tailwind CSS guide.
CSS-to-Tailwind quick reference
Search and filter the most common Tailwind CSS v4 utilities right here in your browser:
Want me to talk about your product? You can sponsor this site.