Tailwind CSS
Tailwind CSS is a utility-first CSS framework that works exceptionally well with Next.js.
Installing Tailwind
Install the Tailwind CSS packages and run the init command to generate both the tailwind.config.js and postcss.config.js files:
1npm install -D tailwindcss postcss autoprefixer2npx tailwindcss init -p
Configuring Tailwind
Inside tailwind.config.js, add paths to the files that will use Tailwind CSS class names:
1/** @type {import('tailwindcss').Config} */2module.exports = {3content: [4'./app/**/*.{js,ts,jsx,tsx,mdx}', // Note the addition of the `app` directory.5'./pages/**/*.{js,ts,jsx,tsx,mdx}',6'./components/**/*.{js,ts,jsx,tsx,mdx}',78// Or if using `src` directory:9'./src/**/*.{js,ts,jsx,tsx,mdx}',10],11theme: {12extend: {},13},14plugins: [],15}
You do not need to modify postcss.config.js.
Importing Styles
Add the Tailwind CSS directives that Tailwind will use to inject its generated styles to a Global Stylesheet in your application, for example:
1@tailwind base;2@tailwind components;3@tailwind utilities;
Inside the root layout (app/layout.tsx), import the globals.css stylesheet to apply the styles to every route in your application.
1import type { Metadata } from 'next'23// These styles apply to every route in the application4import './globals.css'56export const metadata: Metadata = {7title: 'Create Next App',8description: 'Generated by create next app',9}1011export default function RootLayout({ children }: { children: React.ReactNode }) {12return (13<html lang="en">14<body>{children}</body>15</html>16)17}
Using Classes
After installing Tailwind CSS and adding the global styles, you can use Tailwindโs utility classes in your application.
1export default function Page() {2return <h1 className="text-3xl font-bold underline">Hello, Next.js!</h1>3}
Usage with Turbopack
As of Next.js 13.1, Tailwind CSS and PostCSS are supported with Turbopack.