by Sai gowtham

How to use sass stylesheet in react app

In this tutorial, we are going to learn about how to add and use sass(syntactically awesome style sheets) to your create react app with the help of an example.

What is Sass?

  • Sass is a css preprocessor, which allows us to use features that don’t exist in CSS like nesting, mixins, inheritance, etc.
  • Sass compiler complies your sass files into css files.

Adding sass to react

To use sass in react we need to install a new package called node-sass from the npm.

This tutorial assumes that you already created a new react project using create-react-app cli.

Run the below command to install node-sass.

npm i node-sass

Now, we are ready to use sass in our react app without ejecting.

Using sass

Now, rename your .css files into .scss and also change your css imports to scss.

Example:

index.scss
$font-stack: Helvetica, sans-serif;
$primary-color: #333;

body {
  font: 100% $font-stack;
  color: $primary-color;
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.scss';import App from './App';


ReactDOM.render(<App />, document.getElementById('root'));

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course