Lesson Three: Managing Styles in Next.js

Design plays a huge role in engaging users. In this lesson, we will explore different ways to manage styles in Next.js.

Next js tutorial

February 24, 2025

Lesson Three: Managing Styles in Next.js

Lesson Three: Managing Styles in Next.js

Introduction

In modern applications, design plays a major role in attracting users. In this lesson, we’ll explore different ways to manage styles in Next.js, including:

 
  • Adding global CSS files .
  • Using CSS Modules for local styles.
  • Adding Styling Libraries like Tailwind CSS or Material-UI.
 

1. Adding a Global CSS File

Next.js allows you to add a global CSS file that applies to your entire application. This file can contain basic styles that are applied across all pages.

 

Steps:

1.1 Creating a Global CSS File
  • Open the styles/ folder in your project.
  • Create a new file named global.css .
 
1.2 Adding Styles to the File

Add some simple styles to global.css :

 
1.3 Linking the File to the Application

To ensure these styles are applied to all pages, link the file in the _app.js component.

 
  • Open the _app.js file located in the pages/ folder.
  • Import the global.css file:
 

2. Using CSS Modules for Local Styles

CSS Modules is a tool that allows you to write styles specific to each component separately, preventing naming conflicts between styles.

 

Steps:

2.1 Creating a CSS Module
  • Open the about.js file located in the pages/ folder.
  • Create a new file named about.module.css next to the about.js file.
 
2.2 Adding Styles to the File

Add the following styles to about.module.css :

 
2.3 Importing and Using the Styles

Modify the about.js file to use the styles:

 

3. Adding a Styling Library (Example: Tailwind CSS)

Tailwind CSS is a popular styling framework that provides ready-to-use CSS classes you can apply directly in your HTML.

 

Steps:

3.1 Installing Tailwind CSS

In the Terminal, install Tailwind CSS using the following command:

 
3.2 Creating Tailwind Configuration Files

Create configuration files using the following command:

 
3.3 Modifying the tailwind.config.js File

Ensure the tailwind.config.js file contains the following settings:

 
3.4 Adding Tailwind to the CSS File

Open the global.css file and add the following code:

 
3.5 Using Tailwind in a Page

Modify the index.js file to use Tailwind:

 

4. Testing the Styles

After completing the above steps, run the project using:

npm run dev
 

Open your browser and go to http://localhost:3000 . You will notice that the styles have been successfully applied.

 

5. Comparison of Different Approaches

Approach
Advantages
Disadvantages
Global CSS
- Easy to use.<br>- Suitable for shared styles.
- Potential naming conflicts.
CSS Modules
- Prevents naming conflicts.<br>- Suitable for local styles.
- Requires separate CSS files per component.
Tailwind CSS
- No need to write CSS files.<br>- Fast and flexible.<br>- Provides modern designs.
- May be challenging for beginners.

Conclusion of Lesson Three

In this lesson, you learned how to:

  • Add a global CSS file to your application.
  • Use CSS Modules to manage local styles.
  • Add a styling library like Tailwind CSS.
 

In the next lesson, we’ll discuss managing data using getStaticProps and getServerSideProps .

 

See you in Lesson Four! 🚀

Comments

No Comments

There are no comments

Please login to leave a review

  • Web development tutorial A-0

    Web development tutorial A-0

    From Zero to Hero: A Complete Web Development Journey

    View article
  • Differences Between Android and iOS

    Differences Between Android and iOS

    Mobile Application Development: Differences Between Android and iOS and Best Practices

    View article
  • Game Development with Unity

    Game Development with Unity

    Game Development with Unity: From Concept to Final Product

    View article
  • Mastering Programming with Python

    Mastering Programming with Python

    Mastering Programming with Python: Practical Projects and Tips

    View article
  • What are the benefits of websites

    What are the benefits of websites

    Benefits of websites for companies

    View article
  • Web development tutorial A-1

    Web development tutorial A-1

    Lesson 1: Learn HTML from Scratch - A Comprehensive Introduction with Tools Setup

    View article
  • Web development tutorial A-2

    Web development tutorial A-2

    Lesson Two: Learning HTML from Scratch - Advanced Tags and Tables

    View article
  • Artificial Intelligence Qwen AI

    Artificial Intelligence Qwen AI

    Qwen AI is a multi-functional AI model that supports languages and handles creative and interactive tasks

    View article