Using Sass with Vite in React

Posted by


Sass is a popular preprocessor scripting language that is used to write more organized and structured CSS code. It adds features like variables, nesting, and mixins to CSS, making it easier to code and maintain stylesheets. In this tutorial, we will learn how to use Sass in a React project with Vite, a build tool that provides a fast development environment for modern web projects.

Step 1: Create a new React project with Vite

To get started, you need to have Node.js installed on your machine. If you don’t have it installed, you can download and install it from the official Node.js website.

Next, open your terminal and run the following command to install Vite globally:

npm install -g create-vite

Now, create a new React project with Vite by running the following command:

create-vite my-react-app --template react

Replace "my-react-app" with the name of your project. This command will create a new React project with Vite as the build tool.

Step 2: Install Sass

Next, navigate to the root directory of your project and install Sass by running the following command:

npm install sass

This will install the Sass package in your project.

Step 3: Configure Sass in your project

To configure Sass in your project, you need to create a new file named vite.config.js in the root directory of your project. Open this file and add the following code:

import { defineConfig } from 'vite'

export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@import "./src/styles/variables.scss";`
      },
    },
  },
})

This code configures Vite to use Sass as the preprocessor for CSS files. It also imports a file named variables.scss from the src/styles directory, which we will create in the next step.

Step 4: Create a Sass file

Now, create a new directory named styles inside the src directory of your project. Inside the styles directory, create a new file named variables.scss and add some Sass code to it, such as variables or mixins. For example:

$primary-color: #3498db;
$secondary-color: #2c3e50;

Step 5: Import Sass styles in your React components

To use Sass styles in your React components, you need to import the variables.scss file in your component’s CSS or Sass file. For example, if you have a styles.scss file in the src directory, you can import the variables.scss file like this:

@import "./styles/variables.scss";

body {
  background-color: $secondary-color;
  color: $primary-color;
}

You can now write your styles using Sass features like variables and mixins in your React components.

Step 6: Start the development server

To start the development server, run the following command in your terminal:

npm run dev

This will start the Vite development server and open your project in the browser. You can now start writing your React components and styling them with Sass.

In this tutorial, we have learned how to use Sass in a React project with Vite. Sass makes it easier to write and maintain CSS code by adding features like variables, nesting, and mixins. By following the steps in this tutorial, you can set up Sass in your React project and start using it to style your components.

0 0 votes
Article Rating

Leave a Reply

20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@kevinzhu9171
21 days ago

Thank u. It's really helpful.

@fadhilhasan-x2e
21 days ago

how about bootstrap sass ? can you make tutorial for it? i need to override bootstrap with scss

@anoop.chauhan
21 days ago

Hi, how can we use an SCSS module file in a Vite React project?

@rozakcenter
21 days ago

Thank you so much 🎉

@amaurybarbieri9834
21 days ago

Just short question: how can we compile in CSS file every components scss files ?

@sidneythomas643
21 days ago

No need of Live sass compiler?

@אילוןמעיין
21 days ago

thank u 🙂

@naverandno
21 days ago

your theme name please

@jrd1sso
21 days ago

Very helpful, thanks!👏

@edberaga
21 days ago

Thanks!!

@seeker3794
21 days ago

Very good explanation… Straight to the point 👍

@MeezanTheFairBloke
21 days ago

very nice bud… sweet and short!

@hazeify7798
21 days ago

thanks for the easy tutorial

@oscarfrischmann4341
21 days ago

Thank you! I don't know if this is important… buit… Where is the CSS compiling???

@robertfrank2345
21 days ago

Hard to believe that it's simply "npm add -D sass" and then you can begin using scss files, pretty amazing. Thanks

@Deepakrai767622
21 days ago

love from india

@dananjayachathuranga7113
21 days ago

thank u

@44valennn
21 days ago

Gracias me re salvaste 😁

@random2059
21 days ago

🙏🙏🙏🙏🙏🙏
Difference bt MongoDb driver and MOngoose Driver ?? please reply

why not npm i MongoDb , you used nom i Mongoose , please reply

@skrewed
21 days ago

You CRUD API with MongoDB, Express and NodeJS was fire! Can you expand on that? Maybe show a bit more about authentication/X-CSRF-Token and such, it would be awesome!

20
0
Would love your thoughts, please comment.x
()
x