bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

React Sass Styling

What is Sass?

Sass is a CSS pre-processor.

Sass files are executed on the server and sends CSS to the browser.

Sass adds extra features to CSS like variables, nesting, mixins, and more.

You can learn more about Sass in our Sass Tutorial .

Adding Sass to React

To add Sass to a React project, you need to install the Sass package:

Install the package using npm

npm install sass

Now you are ready to include Sass files in your project!

Create a Sass file

Create a Sass file the same way as you create CSS files, but Sass files have the file extension .scss :

Example

In the newly created .scss file, add some simple styling:

$myColor: red;
h1 {
 color: $myColor;
}

Import the Sass file

Import the Sass file in your React component:

Example

import { createRoot } from 'react-dom/client';
import './MyStyle.scss';
function MyHeader() {
 return ( <h1>My Header</h1> );
}
createRoot(document.getElementById('root')).render( <MyHeader /> );

You can learn more about Sass in our Sass Tutorial .

Sass Modules

Sass has many Built-in Modules that you can use to manipulate colors, math, strings, etc.

One example is the sass:color module.

It has a function to make a color darker or lighter, just by giving it a percentage:

Example

@use 'sass:color';
$myColor: red;
h1 {
 color: $myColor;
}
h2 {
 color: color.adjust($myColor, $lightness: -20%);
}
h3 {
 color: color.adjust($myColor, $lightness: 20%);
}

Let us add the headers to our component:

Example

import { createRoot } from 'react-dom/client';
import './MyStyle.scss';
function MyHeader() {
 return ( <div> <h1>My Header 1</h1> <h2>My Header 2</h2> <h3>My Header 3</h3> </div> );
}
createRoot(document.getElementById('root')).render( <MyHeader /> );

You can learn more about Sass in our Sass Tutorial .

Note

Sass files are compiled to CSS at build time.

Previous

React Higher Order Components

Next chapter

Hooks and State Management

Start with React Hooks

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

What is Sass?Adding Sass to ReactCreate a Sass fileImport the Sass fileSass Modules