Skip to content

Commit 35cd609

Browse files
authored
docs: update nextjs documentation
1 parent 13518be commit 35cd609

File tree

101 files changed

+1607
-1754
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1607
-1754
lines changed

apps/docs/content/en/docs/01-app/01-getting-started/06-css.mdx

Lines changed: 172 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Next.js provides several ways to use CSS in your application, including:
2424

2525
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.
2626

27+
<AppOnly>
28+
2729
To start using CSS Modules, create a new file with the extension `.module.css` and import it into any component inside the `app` directory:
2830

2931
```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
3335
```
3436

3537
```tsx filename="app/blog/page.tsx" switcher
36-
import styles from './styles.module.css'
38+
import styles from './blog.module.css'
3739

38-
export default function Page({ children }: { children: React.ReactNode }) {
39-
return <main className={styles.blog}>{children}</main>
40+
export default function Page() {
41+
return <main className={styles.blog}></main>
4042
}
4143
```
4244

4345
```jsx filename="app/blog/page.js" switcher
44-
import styles from './styles.module.css'
46+
import styles from './blog.module.css'
47+
48+
export default function Layout() {
49+
return <main className={styles.blog}></main>
50+
}
51+
```
52+
53+
</AppOnly>
4554

46-
export default function Page({ children }) {
47-
return <main className={styles.blog}>{children}</main>
55+
<PagesOnly>
56+
57+
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;
4862
}
4963
```
5064

65+
```tsx filename="pages/blog/index.tsx" switcher
66+
import styles from './blog.module.css'
67+
68+
export default function Page() {
69+
return <main className={styles.blog}></main>
70+
}
71+
```
72+
73+
```jsx filename="pages/blog/index.js" switcher
74+
import styles from './blog.module.css'
75+
76+
export default function Page() {
77+
return <main className={styles.blog}></main>
78+
}
79+
```
80+
81+
</PagesOnly>
82+
5183
## Global CSS
5284

5385
You can use global CSS to apply styles across your application.
5486

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:
5690

5791
```css filename="app/global.css"
5892
body {
@@ -94,8 +128,28 @@ export default function RootLayout({ children }) {
94128

95129
> **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.
96130
131+
</AppOnly>
132+
133+
<PagesOnly>
134+
135+
Import the stylesheet in the `pages/_app.js` file to apply the styles to **every route** in your application:
136+
137+
```tsx filename="pages/_app.js"
138+
import '@/styles/global.css'
139+
140+
export default function MyApp({ Component, pageProps }) {
141+
return <Component {...pageProps} />
142+
}
143+
```
144+
145+
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+
97149
## External stylesheets
98150

151+
<AppOnly>
152+
99153
Stylesheets published by external packages can be imported anywhere in the `app` directory, including colocated components:
100154

101155
```tsx filename="app/layout.tsx" switcher
@@ -126,4 +180,114 @@ export default function RootLayout({ children }) {
126180
}
127181
```
128182

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:
197+
198+
```jsx filename="pages/_app.js"
199+
import 'bootstrap/dist/css/bootstrap.css'
200+
201+
export default function MyApp({ Component, pageProps }) {
202+
return <Component {...pageProps} />
203+
}
204+
```
205+
206+
To import CSS required by a third-party component, you can do so in your component. For example:
207+
208+
```jsx filename="components/example-dialog.js"
209+
import { useState } from 'react'
210+
import { Dialog } from '@reach/dialog'
211+
import VisuallyHidden from '@reach/visually-hidden'
212+
import '@reach/dialog/styles.css'
213+
214+
function ExampleDialog(props) {
215+
const [showDialog, setShowDialog] = useState(false)
216+
const open = () => setShowDialog(true)
217+
const close = () => setShowDialog(false)
218+
219+
return (
220+
<div>
221+
<button onClick={open}>Open Dialog</button>
222+
<Dialog isOpen={showDialog} onDismiss={close}>
223+
<button className="close-button" onClick={close}>
224+
<VisuallyHidden>Close</VisuallyHidden>
225+
<span aria-hidden>×</span>
226+
</button>
227+
<p>Hello there. I am a dialog</p>
228+
</Dialog>
229+
</div>
230+
)
231+
}
232+
```
233+
234+
</PagesOnly>
235+
236+
## Ordering and Merging
237+
238+
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+
import styles from './page.module.css'
245+
246+
export default function Page() {
247+
return <BaseButton className={styles.primary} />
248+
}
249+
```
250+
251+
```jsx filename="page.js" switcher
252+
import { BaseButton } from './base-button'
253+
import styles from './page.module.css'
254+
255+
export default function Page() {
256+
return <BaseButton className={styles.primary} />
257+
}
258+
```
259+
260+
```tsx filename="base-button.tsx" switcher
261+
import styles from './base-button.module.css'
262+
263+
export function BaseButton() {
264+
return <button className={styles.primary} />
265+
}
266+
```
267+
268+
```jsx filename="base-button.js" switcher
269+
import styles from './base-button.module.css'
270+
271+
export function BaseButton() {
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
281+
- Import global styles and Tailwind stylesheets in the root of your application.
282+
- 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

Comments
 (0)