|
1 | 1 | ---
|
2 | 2 | title: CSS
|
3 |
| -description: Learn about the different ways to add CSS to your application, including CSS Modules, Global CSS, Tailwind CSS, and more. |
| 3 | +description: Learn about the different ways to add CSS to your application, including Tailwind CSS, CSS Modules, Global CSS, and more. |
4 | 4 | related:
|
5 | 5 | title: Next Steps
|
6 | 6 | description: Learn more about the alternatives ways you can use CSS in your application.
|
7 | 7 | links:
|
8 |
| - - app/guides/tailwind-css |
| 8 | + - app/guides/tailwind-v3-css |
9 | 9 | - app/guides/sass
|
10 | 10 | - app/guides/css-in-js
|
11 | 11 | ---
|
12 | 12 |
|
13 | 13 | Next.js provides several ways to style your application using CSS, including:
|
14 | 14 |
|
| 15 | +- [Tailwind CSS](#tailwind-css) |
15 | 16 | - [CSS Modules](#css-modules)
|
16 | 17 | - [Global CSS](#global-css)
|
17 | 18 | - [External Stylesheets](#external-stylesheets)
|
18 |
| -- [Tailwind CSS](/docs/app/guides/tailwind-css) |
19 | 19 | - [Sass](/docs/app/guides/sass)
|
20 | 20 | - [CSS-in-JS](/docs/app/guides/css-in-js)
|
21 | 21 |
|
| 22 | +## Tailwind CSS |
| 23 | + |
| 24 | +[Tailwind CSS](https://tailwindcss.com/) is a utility-first CSS framework that provides low-level utility classes to build custom designs. |
| 25 | + |
| 26 | +<AppOnly> |
| 27 | + |
| 28 | +Install Tailwind CSS: |
| 29 | + |
| 30 | +```bash package="pnpm" |
| 31 | +pnpm add -D tailwindcss @tailwindcss/postcss |
| 32 | +``` |
| 33 | + |
| 34 | +```bash package="npm" |
| 35 | +npm install -D tailwindcss @tailwindcss/postcss |
| 36 | +``` |
| 37 | + |
| 38 | +```bash package="yarn" |
| 39 | +yarn add -D tailwindcss @tailwindcss/postcss |
| 40 | +``` |
| 41 | + |
| 42 | +```bash package="bun" |
| 43 | +bun add -D tailwindcss @tailwindcss/postcss |
| 44 | +``` |
| 45 | + |
| 46 | +Add the PostCSS plugin to your `postcss.config.mjs` file: |
| 47 | + |
| 48 | +```js filename="postcss.config.mjs" |
| 49 | +export default { |
| 50 | + plugins: { |
| 51 | + '@tailwindcss/postcss': {}, |
| 52 | + }, |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +Import Tailwind in your global CSS file: |
| 57 | + |
| 58 | +```css filename="app/globals.css" |
| 59 | +@import 'tailwindcss'; |
| 60 | +``` |
| 61 | + |
| 62 | +Import the CSS file in your root layout: |
| 63 | + |
| 64 | +```tsx filename="app/layout.tsx" switcher |
| 65 | +import './globals.css' |
| 66 | + |
| 67 | +export default function RootLayout({ |
| 68 | + children, |
| 69 | +}: { |
| 70 | + children: React.ReactNode |
| 71 | +}) { |
| 72 | + return ( |
| 73 | + <html lang="en"> |
| 74 | + <body>{children}</body> |
| 75 | + </html> |
| 76 | + ) |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +```jsx filename="app/layout.js" switcher |
| 81 | +import './globals.css' |
| 82 | + |
| 83 | +export default function RootLayout({ children }) { |
| 84 | + return ( |
| 85 | + <html lang="en"> |
| 86 | + <body>{children}</body> |
| 87 | + </html> |
| 88 | + ) |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +Now you can start using Tailwind's utility classes in your application: |
| 93 | + |
| 94 | +```tsx filename="app/page.tsx" switcher |
| 95 | +export default function Page() { |
| 96 | + return ( |
| 97 | + <main className="flex min-h-screen flex-col items-center justify-between p-24"> |
| 98 | + <h1 className="text-4xl font-bold">Welcome to Next.js!</h1> |
| 99 | + </main> |
| 100 | + ) |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +```jsx filename="app/page.js" switcher |
| 105 | +export default function Page() { |
| 106 | + return ( |
| 107 | + <main className="flex min-h-screen flex-col items-center justify-between p-24"> |
| 108 | + <h1 className="text-4xl font-bold">Welcome to Next.js!</h1> |
| 109 | + </main> |
| 110 | + ) |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +</AppOnly> |
| 115 | + |
| 116 | +<PagesOnly> |
| 117 | + |
| 118 | +Install Tailwind CSS: |
| 119 | + |
| 120 | +```bash package="pnpm" |
| 121 | +pnpm add -D tailwindcss @tailwindcss/postcss |
| 122 | +``` |
| 123 | + |
| 124 | +```bash package="npm" |
| 125 | +npm install -D tailwindcss @tailwindcss/postcss |
| 126 | +``` |
| 127 | + |
| 128 | +```bash package="yarn" |
| 129 | +yarn add -D tailwindcss @tailwindcss/postcss |
| 130 | +``` |
| 131 | + |
| 132 | +```bash package="bun" |
| 133 | +bun add -D tailwindcss @tailwindcss/postcss |
| 134 | +``` |
| 135 | + |
| 136 | +Add the PostCSS plugin to your `postcss.config.mjs` file: |
| 137 | + |
| 138 | +```js filename="postcss.config.mjs" |
| 139 | +export default { |
| 140 | + plugins: { |
| 141 | + '@tailwindcss/postcss': {}, |
| 142 | + }, |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +Import Tailwind in your global CSS file: |
| 147 | + |
| 148 | +```css filename="styles/globals.css" |
| 149 | +@import 'tailwindcss'; |
| 150 | +``` |
| 151 | + |
| 152 | +Import the CSS file in your `pages/_app.js` file: |
| 153 | + |
| 154 | +```jsx filename="pages/_app.js" |
| 155 | +import '@/styles/globals.css' |
| 156 | + |
| 157 | +export default function MyApp({ Component, pageProps }) { |
| 158 | + return <Component {...pageProps} /> |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +Now you can start using Tailwind's utility classes in your application: |
| 163 | + |
| 164 | +```tsx filename="pages/index.tsx" switcher |
| 165 | +export default function Home() { |
| 166 | + return ( |
| 167 | + <main className="flex min-h-screen flex-col items-center justify-between p-24"> |
| 168 | + <h1 className="text-4xl font-bold">Welcome to Next.js!</h1> |
| 169 | + </main> |
| 170 | + ) |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +```jsx filename="pages/index.js" switcher |
| 175 | +export default function Home() { |
| 176 | + return ( |
| 177 | + <main className="flex min-h-screen flex-col items-center justify-between p-24"> |
| 178 | + <h1 className="text-4xl font-bold">Welcome to Next.js!</h1> |
| 179 | + </main> |
| 180 | + ) |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +</PagesOnly> |
| 185 | + |
| 186 | +> **Good to know:** If you need broader browser support for very old browsers, see the [Tailwind CSS v3 setup instructions](/docs/app/guides/tailwind-v3-css). |
| 187 | +
|
22 | 188 | ## CSS Modules
|
23 | 189 |
|
24 | 190 | CSS Modules locally scope CSS by generating unique class names. This allows you to use the same class in different files without worrying about naming collisions.
|
@@ -125,7 +291,7 @@ export default function RootLayout({ children }) {
|
125 | 291 | }
|
126 | 292 | ```
|
127 | 293 |
|
128 |
| -> **Good to know:** Global styles can be imported into any layout, page, or component inside the `app` directory. However, since Next.js uses React's built-in support for stylesheets to integrate with Suspense, this currently does not remove stylesheets as you navigate between routes which can lead to conflicts. We recommend using global styles for _truly_ global CSS, and [CSS Modules](#css-modules) for scoped CSS. |
| 294 | +> **Good to know:** Global styles can be imported into any layout, page, or component inside the `app` directory. However, since Next.js uses React's built-in support for stylesheets to integrate with Suspense, this currently does not remove stylesheets as you navigate between routes which can lead to conflicts. We recommend using global styles for _truly_ global CSS (like Tailwind's base styles), [Tailwind CSS](#tailwind-css) for component styling, and [CSS Modules](#css-modules) for custom scoped CSS when needed. |
129 | 295 |
|
130 | 296 | </AppOnly>
|
131 | 297 |
|
@@ -277,7 +443,8 @@ To keep CSS ordering predictable:
|
277 | 443 |
|
278 | 444 | - Try to contain CSS imports to a single JavaScript or TypeScript entry file
|
279 | 445 | - Import global styles and Tailwind stylesheets in the root of your application.
|
280 |
| -- Use CSS Modules instead of global styles for nested components. |
| 446 | +- **Use Tailwind CSS** for most styling needs as it covers common design patterns with utility classes. |
| 447 | +- Use CSS Modules for component-specific styles when Tailwind utilities aren't sufficient. |
281 | 448 | - Use a consistent naming convention for your CSS modules. For example, using `<name>.module.css` over `<name>.tsx`.
|
282 | 449 | - Extract shared styles into shared components to avoid duplicate imports.
|
283 | 450 | - Turn off linters or formatters that auto-sort imports like ESLint’s [`sort-imports`](https://eslint.org/docs/latest/rules/sort-imports).
|
|
0 commit comments