Development
-
December 20, 2023

How to Use Module SCSS with Tailwind in Next.js

Web development is a rapidly evolving field, with new frameworks and libraries consistently setting higher standards. Among these game-changers are Next.js, Tailwind CSS, and SCSS (Sass). These technologies offer unique advantages: Next.js provides server-side rendering and static site generation in the React ecosystem; Tailwind CSS simplifies utility-first styling, and SCSS offers advanced features like nesting, variables, and functions for CSS.

So, why not get the best of all three worlds? By integrating these technologies, you can achieve rapid development speeds, write cleaner and more maintainable code, and create highly scalable and efficient web applications. In this guide, we'll walk you through every step required to configure these tools and set up a robust development environment.

Prerequisites

Before diving into the integration of SCSS modules with Tailwind CSS in a Next.js application, it's important to make sure you're equipped with some foundational knowledge and tools. The following prerequisites will set the stage for a smooth learning experience.

Knowledge Prerequisites:

Basic Understanding of React: Next.js is built on top of React, so a foundational understanding of React is beneficial for following this tutorial.

Familiarity with CSS and Preprocessors: A basic understanding of CSS is essential, and some familiarity with CSS preprocessors like SASS will make it easier to understand the SCSS-specific aspects of this tutorial.

Software Prerequisites:

Node.js and npm: Ensure that Node.js and npm are installed on your system. You can confirm their installation by running node -v and npm -v in your terminal. If they are not installed, you can download them from the official Node.js website.

Step 1: Setting Up a New Next.js Project

Creating a new Next.js project is a straightforward process, thanks to the Create Next App command-line utility. Not only does this tool set up a new Next.js project with sensible defaults, but it also provides prompts that allow you to install additional packages like Tailwind CSS and SCSS, which we will be using in this tutorial.

Create a New Project

Open your terminal and navigate to the directory where you'd like to create your new project.

Run the following command to initiate the project setup:

CODE: https://gist.github.com/joaquincollado/f27118b3ffc1b9a9aff7dda894eef1de.js?file=newproject.sh

During the setup process, you may be prompted to install additional packages. Be sure to select Tailwind CSS when prompted, as we'll be integrating these into our Next.js project.

Navigate to Your Project Directory

After the setup is complete, navigate into your project's directory:

CODE: https://gist.github.com/joaquincollado/c97a4eedb7e43759c3767ff8a616cd7a.js?file=navigate.sh

Verify Your Development Environment

Before we dive into integrating Tailwind CSS and SCSS, let's make sure your basic Next.js application is up and running:

Start the development server:

CODE: https://gist.github.com/joaquincollado/4e32caca8835a3525a94b8259c3dd462.js?file=startserver.sh

Open your web browser and navigate to http://localhost:3000. You should see the default Next.js starter page.

By doing this, you're confirming that the Next.js environment is set up correctly on your machine, paving the way for the subsequent steps in this tutorial where we will integrate Tailwind CSS and SCSS.

Step 2: Installing SCSS

SCSS, also known as Sass, provides a rich set of features such as variables, nesting, and mixins that are not available in plain CSS. Adding SCSS support to your Next.js project is straightforward and enhances your styling capabilities.

Install the SCSS Package

First, ensure that you're in the root directory of your Next.js project. Open your terminal and run the following command to install the Sass package:

CODE: https://gist.github.com/joaquincollado/1be876072d039940f4afd607f1e42646.js?file=installscss.sh

This command installs the necessary package, allowing Next.js to compile your SCSS files into standard CSS.

Creating an SCSS Module

In this tutorial, our focus will be on SCSS modules, which encapsulate styles at the component level. To start, create a new SCSS file with a .module.scss extension. For instance, let's create a Button.module.scss file:

CODE: https://gist.github.com/joaquincollado/6631b6502748db6c4a6cf8e1aeeb60e2.js?file=Button.module.scss

Update Your Component to Use SCSS

Now, you can import this SCSS module into a Next.js component and apply the classes. Here's an example:

CODE: https://gist.github.com/joaquincollado/33577422609324ac37b0fd13647c6e04.js?file=Button.tsx

Add Component to Main File

Once you've created your Button component with SCSS styles, the next step is to include it in your main application file to see it in action. If you are using the default Next.js setup, this would typically be pages/index.tsx. or app/page.tsx if you’re using app router.

Open pages/index.tsx (or app/page.tsx) and import your Button component:

CODE: https://gist.github.com/joaquincollado/0a370d3c9bbe4c40885169ffebabd25e.js?file=index.tsx

Navigate to http://localhost:3000 in your web browser. You should now see your Button component displayed along with its SCSS styles.

Step 3: Using Tailwind CSS with SCSS Modules

Tailwind CSS is a utility-first CSS framework that allows for rapid styling of web applications. Since Tailwind CSS has already been installed and configured during your Next.js project setup, integrating it with your SCSS modules is the next logical step.

Using Tailwind with SCSS Modules

The power of this setup lies in the ability to use Tailwind's utility classes in tandem with SCSS modules. For example, you can extend the Button.module.scss file you created earlier like so:

CODE: https://gist.github.com/joaquincollado/dcd1f81f0fa4e57ea91c25c99d7751ac.js?file=Button.module.scss

Here, the @apply directive enables you to incorporate Tailwind classes within your SCSS files. This approach allows for a seamless blend of Tailwind's utility-first design philosophy with the advanced features of SCSS.

Navigate to http://localhost:3000 in your web browser. Your Button component should now display the styles from both your SCSS module and Tailwind's utility classes.

Congratulations! You've successfully integrated Tailwind CSS with SCSS modules in a Next.js project. This setup allows you to leverage the rapid development capabilities of Tailwind CSS while also taking advantage of the power and flexibility that SCSS offers.

Final Thoughts: Leveraging Tailwind and SCSS Effectively

While Tailwind CSS offers a utility-first approach to styling, SCSS provides advanced features like variables, mixins, and functions. In this step, we'll explore how you can find a balanced approach to using both, tailored to your project's needs.

Understanding Your Options

Tailwind CSS excels at utility-based, atomic design. SCSS shines when it comes to creating reusable logic and variables. Knowing when to use which tool is crucial for maintaining a clean and efficient codebase.

Using SCSS Variables with Tailwind's @apply

If your project leans heavily on themes or needs extensive customization, SCSS variables can be very helpful:

CODE: https://gist.github.com/joaquincollado/f9e502ccd01906c171099b898e5a2149.js?file=variables.scss

Leveraging SCSS Mixins and Functions

SCSS's mixins and functions can help encapsulate complex styles or calculations:

CODE: https://gist.github.com/joaquincollado/45becaa939cd8d3c53710ccc27ef9cbb.js?file=mixins.scss

Choosing the Right Tool for the Job

When in doubt, ask yourself: "Am I looking for a one-off utility or creating a pattern that will be reused?" Tailwind is often quicker for the former, while SCSS can be more efficient for the latter.

By recognizing the strengths and weaknesses of both Tailwind CSS and SCSS, you can more effectively decide which to use in different scenarios, thus creating a more balanced and maintainable codebase.

Summary

We've covered the essentials of setting up a Next.js project to work with both SCSS and Tailwind CSS. Through practical examples, we've explored how you can leverage the individual strengths of each tool to write cleaner, more maintainable code. Whether you're developing a small personal project or a complex enterprise application, the combination of SCSS and Tailwind CSS in a Next.js environment can significantly empower your web development process. The choice between utility-first and traditional SCSS styling will ultimately depend on your project's unique requirements, but knowing how to effectively combine these tools sets the stage for a flexible, scalable, and enjoyable development experience.

Thank you for reading, and happy coding!

Additional Resources

If you're looking to explore further, here are some valuable resources that can deepen your understanding of SCSS, Tailwind CSS, and Next.js:

For SCSS:

- Sass Official Documentation

- Introducing Sass Modules


For Tailwind CSS:

- Tailwind CSS Documentation
- Tailwind CSS: From Zero to Production


For Next.js:

- Next.js Official Documentation
- Next.js and Tailwind CSS Setup Guide