Install Tailwind CSS
Configure Tailwind CSS for your Next.js project.
Tailwind CSS v4 is the latest version with a simpler configuration.
Install packages
Install the necessary packages including the new CLI and PostCSS plugin.
npm install tailwindcss @tailwindcss/postcss @tailwindcss/cliConfigure PostCSS
Add a postcss.config.mjs (or .js) file to your project root.
const config = {
plugins: {
"@tailwindcss/postcss": {},
},
};
export default config;Import Tailwind CSS
Add the import to your main CSS file (e.g., app/globals.css).
@import "tailwindcss";
@theme {
/* Customize your theme here */
--font-sans: "Inter", sans-serif;
--color-primary: oklch(0.6 0.2 240);
}Run your project
npm run devIf you are using an older version, follow these steps.
Install packages
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -pConfigure paths
Update your tailwind.config.ts to include all file paths that use Tailwind classes.
import type { Config } from "tailwindcss";
const config: Config = {
content: [
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
"./app/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {},
},
plugins: [],
};
export default config;Add directives
Add the Tailwind directives to your app/globals.css.
@tailwind base;
@tailwind components;
@tailwind utilities;Key Differences
If you are migrating from v3 to v4:
- CSS-First: Configuration is handled in CSS using
@themeinstead of JS. - Imports: Use
@import "tailwindcss"instead of@tailwinddirectives. - Performance: v4 is significantly faster and has a smaller footprint.