You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/01-getting-started/06-css.mdx
+172-8Lines changed: 172 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,8 @@ Next.js provides several ways to use CSS in your application, including:
24
24
25
25
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.
26
26
27
+
<AppOnly>
28
+
27
29
To start using CSS Modules, create a new file with the extension `.module.css` and import it into any component inside the `app` directory:
28
30
29
31
```css filename="app/blog/styles.module.css"
@@ -33,26 +35,58 @@ To start using CSS Modules, create a new file with the extension `.module.css` a
33
35
```
34
36
35
37
```tsx filename="app/blog/page.tsx" switcher
36
-
importstylesfrom'./styles.module.css'
38
+
importstylesfrom'./blog.module.css'
37
39
38
-
exportdefaultfunction Page({ children }: { children:React.ReactNode }) {
To start using CSS Modules, create a new file with the extension `.module.css` and import it into any component inside the `pages` directory:
58
+
59
+
```css filename="/styles/blog.module.css"
60
+
.blog {
61
+
padding: 24px;
48
62
}
49
63
```
50
64
65
+
```tsx filename="pages/blog/index.tsx" switcher
66
+
importstylesfrom'./blog.module.css'
67
+
68
+
exportdefaultfunction Page() {
69
+
return <mainclassName={styles.blog}></main>
70
+
}
71
+
```
72
+
73
+
```jsx filename="pages/blog/index.js" switcher
74
+
importstylesfrom'./blog.module.css'
75
+
76
+
exportdefaultfunctionPage() {
77
+
return<main className={styles.blog}></main>
78
+
}
79
+
```
80
+
81
+
</PagesOnly>
82
+
51
83
## Global CSS
52
84
53
85
You can use global CSS to apply styles across your application.
54
86
55
-
To use global styles, create a `app/global.css` file and import it in the root layout to apply the styles to **every route** in your application:
87
+
<AppOnly>
88
+
89
+
Create a `app/global.css` file and import it in the root layout to apply the styles to **every route** in your application:
56
90
57
91
```css filename="app/global.css"
58
92
body {
@@ -94,8 +128,28 @@ export default function RootLayout({ children }) {
94
128
95
129
> **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.
Due to the global nature of stylesheets, and to avoid conflicts, you should import them inside [`pages/_app.js`](/docs/pages/building-your-application/routing/custom-app).
146
+
147
+
</PagesOnly>
148
+
97
149
## External stylesheets
98
150
151
+
<AppOnly>
152
+
99
153
Stylesheets published by external packages can be imported anywhere in the `app` directory, including colocated components:
100
154
101
155
```tsx filename="app/layout.tsx" switcher
@@ -126,4 +180,114 @@ export default function RootLayout({ children }) {
126
180
}
127
181
```
128
182
129
-
External stylesheets must be directly imported from an npm package or downloaded and colocated with your codebase. You cannot use `<link rel="stylesheet" />`.
183
+
> **Good to know:** In React 19, `<link rel="stylesheet" href="..." />` can also be used. See the [React `link` documentation](https://react.dev/reference/react-dom/components/link) for more information.
184
+
185
+
</AppOnly>
186
+
187
+
<PagesOnly>
188
+
189
+
Next.js allows you to import CSS files from a JavaScript file.
190
+
This is possible because Next.js extends the concept of [`import`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import) beyond JavaScript.
191
+
192
+
### Import styles from `node_modules`
193
+
194
+
Since Next.js **9.5.4**, importing a CSS file from `node_modules` is permitted anywhere in your application.
195
+
196
+
For global stylesheets, like `bootstrap` or `nprogress`, you should import the file inside `pages/_app.js`. For example:
Next.js optimizes CSS during production builds by automatically chunking (merging) stylesheets. The **order of your CSS** depends on the **order you import styles in your code**.
239
+
240
+
For example, `base-button.module.css` will be ordered before `page.module.css` since `<BaseButton>` is imported before `page.module.css`:
241
+
242
+
```tsx filename="page.ts" switcher
243
+
import { BaseButton } from'./base-button'
244
+
importstylesfrom'./page.module.css'
245
+
246
+
exportdefaultfunction Page() {
247
+
return <BaseButtonclassName={styles.primary} />
248
+
}
249
+
```
250
+
251
+
```jsx filename="page.js" switcher
252
+
import { BaseButton } from'./base-button'
253
+
importstylesfrom'./page.module.css'
254
+
255
+
exportdefaultfunctionPage() {
256
+
return<BaseButton className={styles.primary} />
257
+
}
258
+
```
259
+
260
+
```tsx filename="base-button.tsx" switcher
261
+
importstylesfrom'./base-button.module.css'
262
+
263
+
exportfunction BaseButton() {
264
+
return <buttonclassName={styles.primary} />
265
+
}
266
+
```
267
+
268
+
```jsx filename="base-button.js" switcher
269
+
importstylesfrom'./base-button.module.css'
270
+
271
+
exportfunctionBaseButton() {
272
+
return<button className={styles.primary} />
273
+
}
274
+
```
275
+
276
+
### Recommendations
277
+
278
+
To keep CSS ordering predictable:
279
+
280
+
- Try to contain CSS imports to a single JavaScript or TypeScript entry file
- Use CSS Modules instead of global styles for nested components.
283
+
- Use a consistent naming convention for your CSS modules. For example, using `<name>.module.css` over `<name>.tsx`.
284
+
- Extract shared styles into shared components to avoid duplicate imports.
285
+
- Turn off linters or formatters that auto-sort imports like ESLint’s [`sort-imports`](https://eslint.org/docs/latest/rules/sort-imports).
286
+
- You can use the [`cssChunking`](/docs/app/api-reference/config/next-config-js/cssChunking) option in `next.config.js` to control how CSS is chunked.
287
+
288
+
## Development vs Production
289
+
290
+
- In development (`next dev`), CSS updates apply instantly with [Fast Refresh](/docs/architecture/fast-refresh).
291
+
- In production (`next build`), all CSS files are automatically concatenated into **many minified and code-split**`.css` files, ensuring the minimal amount of CSS is loaded for a route.
292
+
- CSS still loads with JavaScript disabled in production, but JavaScript is required in development for Fast Refresh.
293
+
- CSS ordering can behave differently in development, always ensure to check the build (`next build`) to verify the final CSS order.
0 commit comments