Why do I prefer SCSS over plain CSS?

Why do I prefer SCSS over plain CSS?

Whenever I get into a web project, this is something that I always look for, and makes me genuinely relieved as well if it's being used. For personal projects, this has been my choice for a long time now. So, to understand the topic let us first see what SCSS/SASS is. To put it simply, it's CSS with superpowers. It has such a vast variety of features that developers or even beginners can use for their projects for a better developer experience. I might use the acronyms SASS and SCSS interchangeably. Although there are just syntax differences (brackets & indentation) between them, I prefer the SCSS syntax as it resonates with plain CSS. It is moreover considered an easier option to adopt rather than SASS (Syntactically Awesome Style Sheets).

CSS (Cascading Style Sheets) is the cornerstone of web styling, but as web projects grow in complexity, maintaining clean and organized styles becomes challenging. This is where SCSS (Sassy CSS) comes into play as a powerful preprocessor that extends the capabilities of CSS, offering a range of features that enhance maintainability and efficiency. In this article, we'll dive into the reasons why SCSS is often preferred over plain CSS. Let's start by explaining its amazing features:

1. Variables for Consistency and Reusability

One of the fundamental features of SCSS is the introduction of variables. Variables allow developers to declare values that can be reused throughout the stylesheet. This not only promotes consistency in design but also makes it significantly easier to update styles globally. For instance:

$primary-color: #3498db;

.button {
  background-color: $primary-color;
}

In this example, if the primary color needs to be changed, you can simply update the variable, and the change will be reflected across all instances where it is used.

2. Nesting Selectors for Hierarchy

SCSS supports the nesting of selectors, mirroring the HTML structure, and enhancing code organization. This feature simplifies the readability of styles, especially when dealing with complex structures like navigation menus. Consider the following example:

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;

    li {
      display: inline-block;
    }
  }
}

It makes this easier and less redundant by allowing property declarations to be nested. The outer property names are added to the inner, separated by a hyphen.

3. Mixins for Reusable Style Blocks

Mixins are reusable blocks of styles in SCSS, allowing developers to define a set of styles and apply them to multiple selectors. This promotes a DRY (Don't Repeat Yourself) approach, reducing redundancy in the code. Here's an example of a border-radius mixin:

@mixin border-radius($radius) {
  -webkit-border-radius: $radius;
  -moz-border-radius: $radius;
  border-radius: $radius;
}

.box {
  @include border-radius(10px);
}

By using mixins, you can encapsulate styles in a modular way, making your stylesheets more maintainable and flexible.

4. Functions for Dynamic Styles

SCSS introduces functions, allowing developers to perform calculations and manipulations directly within stylesheets. This feature enables the creation of dynamic styles based on variable values. Consider the following example:

$base-font-size: 16px;

body {
  font-size: $base-font-size * 1.2;
}

Functions enhance the expressiveness of stylesheets, making them more adaptable to different screen sizes and devices.

5. Importing for Modularization

SCSS supports the @import directive, enabling the modularization of styles. This feature is particularly beneficial for large projects where breaking down styles into smaller, focused files enhances organization and collaboration. For instance:

// _variables.scss
$primary-color: #3498db;

// main.scss
@import 'variables';

.button {
  background-color: $primary-color;
}

With importing, you can structure your stylesheets in a way that makes sense for your project, promoting maintainability and collaboration.

6. Logical Operators for Conditions

SCSS includes logical operators, allowing developers to introduce conditional statements within stylesheets. This capability is especially useful when styling elements based on certain conditions. Here's an example:

$theme: light;

body {
  background-color: if($theme == light, #fff, #333);
}

Logical operators enhance the versatility of styles, making it easier to create themes and adapt styles based on dynamic factors.

7. Extends for Selector Inheritance

The @extend directive in SCSS enables selector inheritance, allowing styles defined for one selector to be inherited by another. This promotes code reuse and avoids duplication of styles. Consider the following example:

.error {
  border: 1px solid #ff0000;
  color: #ff0000;
}

.serious-error {
  @extend .error;
  font-weight: bold;
}

There are more @ rules that can be used which are resourceful in their own way.

In summary, SCSS/SASS provides an array of features that enhance the development experience. The benefits of variables, nesting, mixins, functions, importing, logical operators, and extends makes this a valuable language for scalable and maintainable styles in modern web development.

Conclusion

While SCSS offers these advantages, it's important to note that the choice between SCSS and CSS depends on the specific project requirements and the preferences of the development team. SCSS needs to be compiled into CSS before being served to the browser, so a build step is required. Some developers may prefer the simplicity of writing plain CSS, especially for smaller projects or when working in an environment where a preprocessor is not practical.

Also, you can read more about this language here: Documentation
Feel free to comment your thoughts or reach out to me via my socials.

Ending 2023 with this one, will take off with more blogs in 2024.