Overview
Tailwind CSS builds designs from single-purpose utility classes (flex, p-4, text-sm) written directly in HTML or JSX. The compiler scans your source files, generates only the classes it finds, and reads design tokens (colors, fonts, radii, breakpoints) from CSS variables declared in @theme. Since v4 there is no tailwind.config.js by default and no content list: configuration lives in your CSS file and class detection is automatic.
Instructions
Install (v4)
# Vite (recommended for Vite, SvelteKit, Astro, etc.)
npm install tailwindcss @tailwindcss/vite
# PostCSS (Next.js and others)
npm install tailwindcss @tailwindcss/postcss
# Standalone build with no bundler
npm install tailwindcss @tailwindcss/cli
// vite.config.ts
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({ plugins: [tailwindcss()] });
// postcss.config.mjs — PostCSS users (no postcss-import or autoprefixer needed)
export default { plugins: { "@tailwindcss/postcss": {} } };
/* src/app.css — replaces the three v3 @tailwind directives */
@import "tailwindcss";
For the CLI: npx @tailwindcss/cli -i src/app.css -o dist/app.css --watch. To move a v3 project, run npx @tailwindcss/upgrade on a new branch and review the diff.
Layout and Responsive
// Responsive card grid with flexbox/grid
function ProductGrid({ products }: { products: Product[] }) {
return (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6 p-4">
{products.map((product) => (
<div key={product.id}
className="group bg-white rounded-2xl shadow-xs border border-gray-100
hover:shadow-lg hover:border-gray-200 transition-all duration-300
overflow-hidden">
{/* Image with aspect ratio */}
<div className="aspect-[4/3] overflow-hidden">
<img src={product.image} alt={product.name}
className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500" />
</div>
{/* Content */}
<div className="p-4 space-y-2">
<div className="flex items-center justify-between">
<h3 className="font-semibold text-gray-900 truncate">{product.name}</h3>
<span className="text-lg font-bold text-emerald-600">${product.price}</span>
</div>
<p className="text-sm text-gray-500 line-clamp-2">{product.description}</p>
<div className="flex gap-2 pt-2">
{product.tags.map((tag) => (
<span key={tag} className="px-2 py-0.5 text-xs bg-gray-100 text-gray-600 rounded-full">
{tag}
</span>
))}
</div>
</div>
</div>
))}
</div>
);
}
Dark Mode and Custom Theme
/* globals.css — Tailwind v4 */
@import "tailwindcss";
@theme {
--color-brand-50: #eff6ff;
--color-brand-500: #3b82f6;
--color-brand-600: #2563eb;
--color-brand-700: #1d4ed8;
--font-sans: "Inter", system-ui, sans-serif;
--font-mono: "JetBrains Mono", monospace;
--radius-lg: 0.75rem;
--radius-xl: 1rem;
}
Theme variable names decide which utilities exist: --color-brand-500 gives bg-brand-500 and text-brand-500, --font-sans sets font-sans, --breakpoint-3xl adds a 3xl: variant. The v3 names --font-family-* are not recognized.
By default dark: follows prefers-color-scheme. To switch with a class (for a theme toggle), override the variant once in your CSS:
@custom-variant dark (&:where(.dark, .dark *));
// Dark mode — add the dark: prefix
function DashboardCard({ title, value, trend }: CardProps) {
return (
<div className="bg-white dark:bg-gray-800 rounded-xl p-6 border border-gray-200 dark:border-gray-700">
<p className="text-sm text-gray-500 dark:text-gray-400">{title}</p>
<p className="text-3xl font-bold text-gray-900 dark:text-white mt-1">{value}</p>
<p className={`text-sm mt-2 ${trend > 0 ? "text-emerald-600" : "text-red-500"}`}>
{trend > 0 ? "↑" : "↓"} {Math.abs(trend)}%
</p>
</div>
);
}
Animations
// 4 built-in animations
<div className="animate-spin">...</div> // loading spinner
<div className="animate-ping">...</div> // notification dot
<div className="animate-pulse">...</div> // skeleton placeholder
<div className="animate-bounce">...</div> // scroll indicator
// Staggered bounce loader
function LoadingPulse() {
return (
<div className="flex gap-2">
<div className="w-3 h-3 bg-blue-500 rounded-full animate-bounce [animation-delay:-0.3s]" />
<div className="w-3 h-3 bg-blue-500 rounded-full animate-bounce [animation-delay:-0.15s]" />
<div className="w-3 h-3 bg-blue-500 rounded-full animate-bounce" />
</div>
);
}
Enter/exit animations: tailwindcss-animate is a v3 JavaScript plugin. For v4 use its CSS-only replacement tw-animate-css (the one current shadcn/ui setups use):
npm install -D tw-animate-css
/* src/app.css */
@import "tailwindcss";
@import "tw-animate-css";
// Entrance animations
<div className="animate-in fade-in slide-in-from-bottom-4 duration-500">
Content fades and slides up
</div>
// Exit animations
<div className="animate-out fade-out slide-out-to-top-4 duration-300">
Content fades and slides away
</div>
// With delay — fill-mode-backwards prevents flash of final state
<div className="animate-in fade-in delay-200 fill-mode-backwards">
Appears after 200ms delay
</div>
// Staggered children
{items.map((item, i) => (
<div
key={item.id}
className="animate-in fade-in slide-in-from-bottom-2 duration-300 fill-mode-backwards"
style={{ animationDelay: `${i * 100}ms` }}
>
{item.name}
</div>
))}
Custom keyframes (Tailwind v4 uses @theme, v3 uses tailwind.config.js):
/* globals.css — Tailwind v4 custom animation */
@theme {
--animate-shimmer: shimmer 2s infinite linear;
}
@keyframes shimmer {
0% { background-position: -200% 0; }
100% { background-position: 200% 0; }
}
// Skeleton loading with shimmer
<div className="h-4 w-48 rounded bg-linear-to-r from-gray-200 via-gray-100 to-gray-200 bg-[length:200%_100%] animate-shimmer" />
Examples
Example 1: Set up Tailwind v4 in a Vite React app
User prompt: "Add Tailwind to my Vite React project and give me a brand blue and Inter font."
npm install tailwindcss @tailwindcss/vite
Add tailwindcss() to plugins in vite.config.ts, then put this in src/index.css (imported from main.tsx):
@import "tailwindcss";
@theme {
--color-brand-500: #3b82f6;
--font-sans: "Inter", system-ui, sans-serif;
}
<button className="bg-brand-500 font-sans text-white px-4 py-2 rounded-lg">Save</button> now renders blue with Inter. No config file and no content globs are needed.
Example 2: Fix a v3 project that looks different after upgrading
User prompt: "After moving to Tailwind 4 my shadows are bigger, focus rings are thin and borders turned black."
After npx @tailwindcss/upgrade most renames are applied; check the rest by hand:
shadow-sm->shadow-xs,shadow->shadow-sm, same forrounded,blur,drop-shadowringis now 1px andcurrentColor: usering-3 ring-blue-500for the old lookoutline-none->outline-hidden;bg-gradient-to-r->bg-linear-to-r- default border color is
currentColor: writeborder border-gray-200 flex-shrink-0->shrink-0;bg-opacity-50->bg-black/50
Guidelines
- Utility-first — Build designs with utilities; extract components (React/Vue) not CSS classes
- Responsive prefixes —
sm:,md:,lg:,xl:,2xl:— mobile-first breakpoints - Dark mode —
dark:prefix for dark variants; toggle via class or system preference - Group/peer —
group-hover:for parent-triggered styles;peer-invalid:for sibling-based - Arbitrary values —
w-[137px],text-[#1a2b3c],grid-cols-[1fr_2fr]for one-off values - @theme — Define design tokens in CSS; v4 reads them directly. Use
@config "./tailwind.config.js"only to load a legacy JS config - Class detection — Tailwind scans source files for complete class names, so only used classes ship. Never build names dynamically (
`bg-${color}-500`); write the full string or map values to complete class names - cn() helper — Use
clsx+tailwind-mergefor conditional classes:cn("base", condition && "extra") - Animations — Use CSS animations for simple effects (fade, slide, spin). Prefer
duration-300for most transitions. Usemotion-reduce:animate-nonefor reduced motion. - tw-animate-css — Import it for entrance/exit animations (
animate-in,animate-out). Usefill-mode-backwardsto prevent a flash during delays. - Custom utilities — v4 replaces
@layer utilities { .x {} }with@utility x { ... }; plain@layerclasses no longer get variants likehover:. - Important modifier — put
!at the end:bg-red-500!. - Old browsers — v4 needs Safari 16.4+, Chrome 111+, Firefox 128+; stay on v3.4 if you must support older ones.