Next.js provides several ways to use CSS in your application, including:
...
...
@@ -15,12 +15,10 @@ Next.js provides several ways to use CSS in your application, including:
- [CSS Modules](#css-modules)
- [Global CSS](#global-css)
- [Tailwind CSS](#tailwind-css)
- [Sass](#sass)
- [CSS-in-JS](#css-in-js)
- [Sass](/docs/app/guides/sass)
- [CSS-in-JS](/docs/app/guides/css-in-js)
- [External Stylesheets](#external-stylesheets)
This page will guide you through how to use each of these approaches.
## CSS Modules
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.
...
...
@@ -185,292 +183,6 @@ export default function Page() {
}
```
## Sass
Next.js integrates with [Sass](https://sass-lang.com/) using both the [`.scss`](https://sass-lang.com/documentation/syntax/#scss) and [`.sass`](https://sass-lang.com/documentation/syntax#the-indented-syntax) extensions and syntax.
You can also use component-level Sass via [CSS Modules](#css-modules) and the `.module.scss`or `.module.sass` extension.
### Installing Sass
To start using Sass, install the `sass` package:
```bash filename="Terminal"
npm install --save-dev sass
```
### Customizing Sass options
If you want to configure your Sass options, use the [`sassOptions`](/docs/app/api-reference/config/next-config-js/sassOptions) option in `next.config.js`.
```ts filename="next.config.ts" switcher
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
sassOptions: {
additionalData: `$var: red;`,
},
}
export default nextConfig
```
```js filename="next.config.mjs" switcher
/** @type {import('next').NextConfig} */
const nextConfig = {
sassOptions: {
additionalData: `$var: red;`,
},
}
export default nextConfig
```
## CSS-in-JS
> **Warning:** CSS-in-JS libraries which require runtime JavaScript are not currently supported in React Server Components. Using CSS-in-JS with newer React features like Server Components and Streaming requires library authors to support the latest version of React.
The following libraries are supported in **Client Components** in the `app` directory (alphabetical):
If you want to style Server Components, we recommend using [CSS Modules](#css-modules) or other solutions that output CSS files, like [Tailwind CSS](#tailwind-css).
### Configuring CSS-in-JS
To configure CSS-in-JS, you need to:
1. Create a **style registry** to collect all CSS rules in a render.
2. Use the `useServerInsertedHTML` hook to inject rules before any content that might use them.
3. Create a Client Component that wraps your app with the style registry during initial server-side rendering.
#### `styled-jsx`
To configure `styled-jsx` for your application, create a new registry:
```tsx filename="app/registry.tsx" switcher
'use client'
import React, { useState } from 'react'
import { useServerInsertedHTML } from 'next/navigation'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
export default function StyledJsxRegistry({
children,
}: {
children: React.ReactNode
}) {
// Only create stylesheet once with lazy initial state
Then, wrap your [root layout](/docs/app/api-reference/file-conventions/layout#root-layouts) with the registry:
```tsx filename="app/layout.tsx" switcher
import StyledJsxRegistry from './registry'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html>
<body>
<StyledJsxRegistry>{children}</StyledJsxRegistry>
</body>
</html>
)
}
```
```jsx filename="app/layout.js" switcher
import StyledJsxRegistry from './registry'
export default function RootLayout({ children }) {
return (
<html>
<body>
<StyledJsxRegistry>{children}</StyledJsxRegistry>
</body>
</html>
)
}
```
#### `styled-components`
To use `styled-components`, enable it in `next.config.js`:
```ts filename="next.config.ts" switcher
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
compiler: {
styledComponents: true,
},
}
export default nextConfig
```
```js filename="next.config.mjs" switcher
/** @type {import('next').NextConfig} */
const nextConfig = {
compiler: {
styledComponents: true,
},
}
export default nextConfig
```
Then, use the `styled-components` API to create a global registry component to collect all CSS style rules generated during a render, and a function to return those rules. Then use the `useServerInsertedHTML` hook to inject the styles collected in the registry into the `<head>` HTML tag in the root layout.
```tsx filename="lib/registry.tsx" switcher
'use client'
import React, { useState } from 'react'
import { useServerInsertedHTML } from 'next/navigation'
import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
export default function StyledComponentsRegistry({
children,
}: {
children: React.ReactNode
}) {
// Only create stylesheet once with lazy initial state
@@ -247,7 +247,7 @@ Styling the Image component is similar to styling a normal `<img>` element, but
- Use `className` or `style`, not `styled-jsx`.
- In most cases, we recommend using the `className` prop. This can be an imported [CSS Module](/docs/app/building-your-application/styling/css), a [global stylesheet](/docs/app/building-your-application/styling/css#global-styles), etc.
- You can also use the `style` prop to assign inline styles.
- You cannot use [styled-jsx](/docs/app/building-your-application/styling/css-in-js) because it's scoped to the current component (unless you mark the style as `global`).
- You cannot use [styled-jsx](/docs/app/guides/css-in-js) because it's scoped to the current component (unless you mark the style as `global`).
- When using `fill`, the parent element must have `position: relative`
- This is necessary for the proper rendering of the image element in that layout mode.
- When using `fill`, the parent element must have `display: block`
{/* DO NOT EDIT. The content of this doc is generated from the source above. To edit the content of this page, navigate to the source page in your editor. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
{/* DO NOT EDIT. The content of this doc is generated from the source above. To edit the content of this page, navigate to the source page in your editor. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
1. Ensure your code is following [our official examples](https://github.com/vercel/next.js/tree/canary/examples)
7. Incorrectly configured Edge/CDN that attempts to modify the html response, such as Cloudflare [Auto Minify](https://developers.cloudflare.com/speed/optimization/content/troubleshooting/disable-auto-minify/)