Blog>
Snippets

Leveraging Sass/SCSS for Nested Styles in Next.js

Illustrate the use of Sass/SCSS for nested styles and how to set it up in a Next.js project.
// Install Sass package
npm install sass
The first step is to install the Sass package in your Next.js project using npm or yarn. This command demonstrates installation via npm.
// Create a Sass/SCSS file
// styles/Home.module.scss
.home {
  background-color: #f5f5f5;
  .content {
    padding: 20px;
    h1 {
      color: #333;
    }
  }
}
Create a Sass file with nested styles. This example demonstrates how to nest styles within a `.home` class, targeting a `.content` class and an `h1` element within that content.
import styles from '../styles/Home.module.scss';
function Home() {
  return (
    <div className={styles.home}>
      <div className={styles.content}>
        <h1>Welcome to Next.js!</h1>
      </div>
    </div>
  );
}
export default Home;
In your JavaScript or JSX file, import the SCSS module and use the defined classes. This code snippet demonstrates how to import and use the `.home` and `.content` classes in a Next.js component.