Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Next.js CSS Styling

Next.js CSS Styling

In Next.js, styling is very flexible; you can choose various ways to add styles to your application.


Global Styles

Global styles are CSS styles that affect the entire application; they are typically used to set basic page styles such as fonts, colors, and layout.

Next.js imports global CSS files via app/layout.tsx.

By default, the app directory already has a global styles file, globals.css; if it does not exist, you can also create a new CSS file.

Create the globals.css file in the styles directory, then import it in app/layout.tsx.

app/globals.css file code:

/* app/globals.css */

body

{

 

padding

:

20px

20px

60px

;

 

max-width

:

680px

;

 

font-size

:

64px

;

 

margin

:

0

auto

;

 

background-color

:

#F5F5FB

;

 

color

:

#333

;

}

Then, import this file in the root layout (app/layout.tsx) so the styles apply to every route in the application:

app/layout.tsx file code:

// app/layout.tsx

// These styles will be applied to every route in the application

import

'./globals.css'

export

default

function

RootLayout

(

{

  children

,

}

:

{

  children

:

React.

ReactNode

}

)

{

 

return

(

   

<html>
<body{children}body html)}>

**Note:** Global styles can be imported into any layout, page, or component in the app directory. However, because Next.js uses React's built-in stylesheet support to integrate with Suspense, the current built-in support does not remove stylesheets when you navigate between routes. Therefore, we recommend using Global Styles for truly global CSS and CSS Modules for scoped CSS.

app/page.tsx code is as follows:

Example

export

default

function

Home

(

)

{

 

return

<

div

Hello

,

Runoob

!

div

;

}

Visit http://localhost:3000/; the test display effect is as follows:


Sass

Next.js also supports Sass, a more feature-rich CSS preprocessor.

If you prefer to use Sass for styling, you can enable Sass by installing the corresponding dependency. For more, see Sass Tutorial.

Installation:



npm install sass

Next.js integrates with Sass and supports the .scss and .sass extensions and syntax.

You can also use component-level Sass via CSS Modules, with the extensions .module.scss or .module.sass.

Custom Sass Configuration

If you want to configure Sass options, you can use the sassOptions option in next.config.js.

Example

// next.config.ts

import

type

{

NextConfig

}

from

'next'

const

nextConfig

:

NextConfig

=

{

  sassOptions

:

{

    additionalData

:

`$var

:

red

;

`

,

// Automatically inject these styles into every Sass file

 

}

,

}

export

default

nextConfig

Through this approach, you can configure some default Sass data or make other custom settings.

Create a SCSS file, such as styles/Button.module.scss:

Example

/* styles/Button.module.scss */

.button

{

 

padding

:

10px

20px

;

 

background-color

:

#0070f3

;

 

color

:

white

;

 

border

:

none

;

 

border-radius

:

5px

;

 

cursor

:

pointer

;

}

Use the SCSS module in a component:

Example

// app/components/Button.tsx

import

styles from

'../styles/Button.module.scss'

;

export

default

function

Button

(

)

{

 

return

<

button className

=

{

styles.

button

}

Click Me

button

;

}