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: docs/01-app/01-getting-started/06-css.mdx
+171-8Lines changed: 171 additions & 8 deletions
Display the source diff
Display the rich diff
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 }) {
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,113 @@ 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**.
238
+
239
+
For example, `base-button.module.css` will be ordered before `page.module.css` since `<BaseButton>` is imported before `page.module.css`:
240
+
241
+
```tsx filename="page.ts" switcher
242
+
import { BaseButton } from'./base-button'
243
+
importstylesfrom'./page.module.css'
244
+
245
+
exportdefaultfunction Page() {
246
+
return <BaseButtonclassName={styles.primary} />
247
+
}
248
+
```
249
+
250
+
```jsx filename="page.js" switcher
251
+
import { BaseButton } from'./base-button'
252
+
importstylesfrom'./page.module.css'
253
+
254
+
exportdefaultfunctionPage() {
255
+
return<BaseButton className={styles.primary} />
256
+
}
257
+
```
258
+
259
+
```tsx filename="base-button.tsx" switcher
260
+
importstylesfrom'./base-button.module.css'
261
+
262
+
exportfunction BaseButton() {
263
+
return <buttonclassName={styles.primary} />
264
+
}
265
+
```
266
+
267
+
```jsx filename="base-button.js" switcher
268
+
importstylesfrom'./base-button.module.css'
269
+
270
+
exportfunctionBaseButton() {
271
+
return<button className={styles.primary} />
272
+
}
273
+
```
274
+
275
+
### Recommendations
276
+
277
+
To keep CSS ordering predictable:
278
+
279
+
- Try to contain CSS imports to a single JavaScript or TypeScript entry file
- Use CSS Modules instead of global styles for nested components.
282
+
- Use a consistent naming convention for your CSS modules. For example, using `<name>.module.css` over `<name>.tsx`.
283
+
- Extract shared styles into shared components to avoid duplicate imports.
284
+
- Turn off linters or formatters that auto-sort imports like ESLint’s [`sort-imports`](https://eslint.org/docs/latest/rules/sort-imports).
285
+
- 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.
286
+
287
+
## Development vs Production
288
+
289
+
- In development (`next dev`), CSS updates apply instantly with [Fast Refresh](/docs/architecture/fast-refresh).
290
+
- 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.
291
+
- CSS still loads with JavaScript disabled in production, but JavaScript is required in development for Fast Refresh.
292
+
- CSS ordering can behave differently in development, always ensure to check the build (`next build`) to verify the final CSS order.
Copy file name to clipboardExpand all lines: docs/01-app/02-guides/css-in-js.mdx
-2Lines changed: 0 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,8 +32,6 @@ The following are currently working on support:
32
32
33
33
> **Good to know**: We're testing out different CSS-in-JS libraries and we'll be adding more examples for libraries that support React 18 features and/or the `app` directory.
34
34
35
-
If you want to style Server Components, we recommend using [CSS Modules](/docs/app/building-your-application/styling/css) or other solutions that output CSS files, like PostCSS or [Tailwind CSS](/docs/app/guides/tailwind-css).
36
-
37
35
## Configuring CSS-in-JS in `app`
38
36
39
37
Configuring CSS-in-JS is a three-step opt-in process that involves:
Copy file name to clipboardExpand all lines: docs/01-app/02-guides/migrating/app-router-migration.mdx
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -886,11 +886,11 @@ If you are also migrating to Next.js from a Single-Page Application (SPA) at the
886
886
887
887
In the `pages` directory, global stylesheets are restricted to only `pages/_app.js`. With the `app` directory, this restriction has been lifted. Global styles can be added to any layout, page, or component.
Copy file name to clipboardExpand all lines: docs/01-app/02-guides/migrating/from-create-react-app.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -273,7 +273,7 @@ With the above changes, you shifted from declaring everything in your `index.htm
273
273
274
274
### Step 5: Styles
275
275
276
-
Like CRA, Next.js supports [CSS Modules](/docs/app/building-your-application/styling/css#css-modules) out of the box. It also supports [global CSS imports](/docs/app/building-your-application/styling/css#global-styles).
276
+
Like CRA, Next.js supports [CSS Modules](/docs/app/getting-started/css#css-modules) out of the box. It also supports [global CSS imports](/docs/app/getting-started/css#global-css).
277
277
278
278
If you have a global CSS file, import it into your `app/layout.tsx`:
Copy file name to clipboardExpand all lines: docs/01-app/02-guides/tailwind-css.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ There is also an [upgrade CLI](https://tailwindcss.com/docs/upgrade-guide) and [
39
39
40
40
## Importing Styles
41
41
42
-
Add the [Tailwind CSS directives](https://tailwindcss.com/docs/functions-and-directives#directives) that Tailwind will use to inject its generated styles to a [Global Stylesheet](/docs/app/building-your-application/styling/css#global-styles) in your application, for example:
42
+
Add the [Tailwind CSS directives](https://tailwindcss.com/docs/functions-and-directives#directives) that Tailwind will use to inject its generated styles to a [Global Stylesheet](/docs/app/getting-started/css#global-css) in your application, for example:
43
43
44
44
```css filename="app/globals.css"
45
45
@import'tailwindcss';
@@ -111,7 +111,7 @@ export default function Page() {
111
111
112
112
## Importing Styles
113
113
114
-
Add the [Tailwind CSS directives](https://tailwindcss.com/docs/functions-and-directives#directives) that Tailwind will use to inject its generated styles to a [Global Stylesheet](/docs/pages/building-your-application/styling/css#global-styles) in your application, for example:
114
+
Add the [Tailwind CSS directives](https://tailwindcss.com/docs/functions-and-directives#directives) that Tailwind will use to inject its generated styles to a [Global Stylesheet](/docs/app/getting-started/css#global-css) in your application, for example:
0 commit comments