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 style development process through the use of variables, nesting, mixins, and other features. It allows for the creation of clean and organized code, greatly improving work wit...

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 style development process through the use of variables, nesting, mixins, and other features. It allows for the creation of clean and organized code, greatly improving work with large projects. In this article, we will explore practical examples of using mixins and extends in SCSS.

Mixins: Creating Reusable Code Fragments

Mixins in SCSS allow for the creation of reusable code fragments that can be used in different parts of the project. This is convenient for ensuring style consistency 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;
}

This example uses the placeholder %button-core, allowing the inheritance of basic button styles. @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 for maximizing the benefits 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 for separating base styles and themes, making the code more structured and easier to maintain.

Advantages of Using SCSS

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

  • Reduction of Code Duplication: the use of these tools helps maintain project cleanliness 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 web application development. Knowledge and skills in using mixins and extends make the code not only effective but also easy to maintain and scale.

🔥 More posts

All posts
Feb 17, '25 02:00

What is SASS?

SASS is a preprocessor for CSS that allows you to write styled code faster and more efficiently. SASS ...