DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Mastering SASS/SCSS in React: A Comprehensive Guide

Using SASS/SCSS in React: A Complete Guide

SASS (Syntactically Awesome Style Sheets) is a preprocessor scripting language that extends CSS and provides features like variables, nested rules, mixins, and functions, making it a powerful tool for writing scalable and maintainable styles. SCSS (Sassy CSS) is a syntax of SASS that is fully compatible with regular CSS, allowing you to write CSS-like code and still take advantage of the power of SASS features.

Using SASS/SCSS with React allows you to write more dynamic and maintainable styles by leveraging these features while keeping your component-based architecture intact.

Why Use SASS/SCSS in React?

  1. Modularity: SASS allows you to break your CSS into smaller, reusable pieces like variables, mixins, and partials, which is great for larger React applications.
  2. Variables and Functions: You can define variables for colors, fonts, or any other property, and use functions to generate styles dynamically.
  3. Nesting: SCSS allows you to nest your CSS selectors in a hierarchical manner, reflecting the structure of your HTML and improving readability.
  4. Maintainability: SASS offers better maintainability and scalability with features like mixins and inheritance, making it easier to manage large CSS codebases.
  5. Cleaner Code: SCSS allows you to write cleaner, more efficient code by reducing redundancy and improving the organization of styles.

Setting Up SASS/SCSS in React

To start using SASS/SCSS in your React project, follow these steps:

  1. Install SASS:

First, you need to install the sass package to enable SCSS in your project:

npm install sass
Enter fullscreen mode Exit fullscreen mode
  1. Create SCSS Files:

Once SASS is installed, you can create .scss files and start writing SCSS code.

For example:

src/
  styles/
    App.scss
  components/
    App.js
Enter fullscreen mode Exit fullscreen mode
  1. Import SCSS Files into React Components:

After creating the SCSS file, you can import it into your React component just like a normal CSS file. SCSS will be compiled into regular CSS by Webpack.

Example:

// App.js
import React from 'react';
import './styles/App.scss';

const App = () => {
  return (
    <div className="app">
      <h1>Welcome to React with SASS/SCSS!</h1>
    </div>
  );
};

export default App;
Enter fullscreen mode Exit fullscreen mode
  1. Write SCSS in App.scss:

Here’s how to write SCSS code that will style your components:

// App.scss
$app-bg-color: #282c34;
$primary-color: #61dafb;

.app {
  background-color: $app-bg-color;
  color: $primary-color;
  font-family: Arial, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;

  h1 {
    font-size: 2.5rem;
    text-align: center;
  }
}
Enter fullscreen mode Exit fullscreen mode

SCSS Features and Usage in React

1. Variables

SASS allows you to define reusable variables for colors, fonts, or any other CSS property.

// variables.scss
$primary-color: #61dafb;
$secondary-color: #282c34;

// App.scss
@import './variables';

.app {
  background-color: $secondary-color;
  color: $primary-color;
}
Enter fullscreen mode Exit fullscreen mode

2. Nesting

SASS/SCSS allows you to nest CSS selectors in a way that reflects the HTML structure, improving the organization and readability of your styles.

// App.scss
.app {
  background-color: #282c34;
  color: #61dafb;

  .header {
    font-size: 2rem;
    font-weight: bold;

    &:hover {
      color: #fff;
    }
  }

  .footer {
    font-size: 1rem;
    color: #888;
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Partials and Imports

SASS allows you to break your styles into smaller, more manageable pieces using partials. You can import multiple SCSS files into one main SCSS file.

For example:

// _variables.scss
$primary-color: #61dafb;
$secondary-color: #282c34;

// _button.scss
.button {
  padding: 10px;
  background-color: $primary-color;
  border-radius: 5px;
  &:hover {
    background-color: darken($primary-color, 10%);
  }
}

// App.scss
@import './variables';
@import './button';

.app {
  background-color: $secondary-color;
  color: $primary-color;
}
Enter fullscreen mode Exit fullscreen mode

4. Mixins

Mixins in SCSS allow you to create reusable chunks of CSS that can be included in different parts of your stylesheet.

// _mixins.scss
@mixin button-style($bg-color) {
  padding: 10px;
  background-color: $bg-color;
  border-radius: 5px;
  color: white;
}

.button {
  @include button-style($primary-color);
}

// App.scss
@import './mixins';

.button {
  @include button-style($secondary-color);
}
Enter fullscreen mode Exit fullscreen mode

5. Functions

SCSS functions allow you to perform calculations or generate values dynamically.

// _functions.scss
@function calculate-spacing($multiplier) {
  @return $multiplier * 8px;
}

// App.scss
@import './functions';

.app {
  margin: calculate-spacing(2); // 16px
}
Enter fullscreen mode Exit fullscreen mode

Best Practices for Using SASS/SCSS in React

  1. Modularize Your SCSS: Split your SCSS into smaller, reusable partials to keep the codebase clean and maintainable.

  2. Use BEM Naming Convention: While this is not a feature of SASS, it's a great practice to follow BEM (Block, Element, Modifier) for naming classes to avoid conflicts and ensure clarity.

  3. Use Mixins and Functions: To reduce code repetition and make your styles more flexible, use mixins and functions for reusable patterns and logic.

  4. Leverage SCSS Variables: Use variables for colors, spacing, and typography to make your styles more consistent and easier to maintain.

  5. Keep SCSS Modular: Each component should ideally have its own SCSS file, which is imported only where necessary, reducing global styles.

  6. Use the @import Directive Sparingly: Instead of importing everything into one main file, try to import only what is necessary to reduce the number of HTTP requests and keep your CSS file smaller.

Example of SCSS with React for a Responsive Layout

Here’s an example of using SCSS in React to create a responsive layout:

// _variables.scss
$primary-color: #61dafb;
$secondary-color: #282c34;
$font-size-base: 1rem;
$font-size-lg: 1.25rem;

// _responsive.scss
$breakpoint-md: 768px;

// _layout.scss
@import './variables';
@import './responsive';

.container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;

  @media (min-width: $breakpoint-md) {
    grid-template-columns: 1fr 1fr;
  }
}

.card {
  background-color: $secondary-color;
  padding: 20px;
  color: $primary-color;
  font-size: $font-size-base;

  @media (min-width: $breakpoint-md) {
    font-size: $font-size-lg;
  }
}
Enter fullscreen mode Exit fullscreen mode
// App.js
import React from 'react';
import './styles/layout.scss';

const App = () => {
  return (
    <div className="container">
      <div className="card">Card 1</div>
      <div className="card">Card 2</div>
    </div>
  );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

Conclusion

Integrating SASS/SCSS into React allows you to write maintainable, scalable, and modular styles. SCSS's powerful features like variables, mixins, and nesting enhance the development experience, especially in large applications. By using SCSS, you can keep your styles organized, reusable, and easy to manage as your project grows.

Top comments (0)