How to use Sass and SCSS with React projects
· Category: HTML & CSS
Short answer
Sass extends CSS with variables, nesting, mixins, and imports. SCSS syntax is fully CSS-compatible and widely used in React projects.
Steps
- Install
sassas a dev dependency. - Write
.scssfiles and import them into components. - Use variables and mixins for reusable design tokens.
Example
// _variables.scss
$primary: #3498db;
// Button.scss
@import 'variables';
.button {
background: $primary;
&:hover {
background: darken($primary, 10%);
}
}
Tips
- Use CSS Modules alongside Sass for scoped component styles.
- Prefer
@useover@importin modern Sass for better encapsulation. - Avoid deep nesting; it increases specificity and output size.