Apr 24, '25 03:00

Deep Dive into SCSS: Practical Examples of Using Mixins and Extends

SCSS (Sassy CSS) is a powerful extension of CSS that significantly simplifies the process of developing styles through variables, nesting, mixins, and other features. It allows for the creation of clean and organized code, greatly improving work on large pr...

Read post
Share
🔥 More posts
This content has been automatically translated from Ukrainian.

SCSS (Sassy CSS) is a powerful extension of CSS that significantly simplifies the process of developing styles through variables, nesting, mixins, and other features. It allows for the creation of clean and organized code, greatly improving work on large projects. In this article, we will explore practical examples of using mixins and extends in SCSS.

Mixins: Creating Reusable Code Snippets

Mixins in SCSS allow you to create reusable code snippets that can be used in different parts of the project. This is convenient for ensuring consistency in styles and reducing code duplication.

Example of Using Mixins

@mixin button-styles($bg-color, $text-color) {
  background-color: $bg-color;
  color: $text-color;
  padding: 10px 20px;
  border-radius: 5px;
  text-align: center;
  text-decoration: none;
}

.button-primary {
  @include button-styles(#3498db, #ffffff);
}

.button-secondary {
  @include button-styles(#95a5a6, #ffffff);
}

In this example, the mixin button-styles takes parameters for background and text colors, allowing for the creation of different button styles without code repetition.

Using extends for Inheriting Styles

One of the key aspects of SCSS is the ability to inherit styles using @extend. This helps avoid code duplication by merging common styles.

How to Use extends

%button-core {
  padding: 10px 20px;
  border-radius: 5px;
  text-align: center;
  text-decoration: none;
}

.button-primary {
  @extend %button-core;
  background-color: #3498db;
  color: #ffffff;
}

.button-secondary {
  @extend %button-core;
  background-color: #95a5a6;
  color: #ffffff;
}

In this example, the placeholder %button-core is used to inherit the base styles for buttons. @extend is an ideal solution for cases where multiple elements share the same styles.

Interaction of Mixins and extends

Mixins and extends can be combined to create a more flexible and efficient style structure. This allows you to maximize the advantages of both approaches.

Combined Approach

@mixin button-theme($bg-color, $text-color) {
  background-color: $bg-color;
  color: $text-color;
}

%button-core {
  padding: 10px 20px;
  border-radius: 5px;
  text-align: center;
  text-decoration: none;
}

.button-primary {
  @extend %button-core;
  @include button-theme(#3498db, #ffffff);
}

.button-secondary {
  @extend %button-core;
  @include button-theme(#95a5a6, #ffffff);
}

This approach allows you to separate base styles and themes, making the code more structured and easier to maintain.

Benefits of Using SCSS

The use of mixins and extends in SCSS provides a number of advantages, including:

  • Reduction of Code Duplication: using these tools helps maintain the cleanliness of the project and avoid repetition.
  • Ease of Maintenance: style changes can be made in one place, simplifying project maintenance.
  • Flexibility: the ability to create parameterized styles that adapt to different conditions.

SCSS is a powerful tool for developing web applications. Knowing how to use mixins and extends makes the code not only efficient but also easy to maintain and scale.

🔥 More posts

All posts