๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Front
NextJs
Tailwind CSS

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:

Terminal
1
npm install -D tailwindcss postcss autoprefixer
2
npx tailwindcss init -p

Configuring Tailwind

Inside tailwind.config.js, add paths to the files that will use Tailwind CSS class names:

tailwind.config.js
1
/** @type {import('tailwindcss').Config} */
2
module.exports = {
3
content: [
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}',
7
8
// Or if using `src` directory:
9
'./src/**/*.{js,ts,jsx,tsx,mdx}',
10
],
11
theme: {
12
extend: {},
13
},
14
plugins: [],
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:

app/globals.css
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.

app/layout.tsx
1
import type { Metadata } from 'next'
2
3
// These styles apply to every route in the application
4
import './globals.css'
5
6
export const metadata: Metadata = {
7
title: 'Create Next App',
8
description: 'Generated by create next app',
9
}
10
11
export default function RootLayout({ children }: { children: React.ReactNode }) {
12
return (
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.

pages/index.tsx
1
export default function Page() {
2
return <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.