Passing Arguments to Mixins

Tutorial 2 of 5

Tutorial: Passing Arguments to Mixins in SASS/SCSS

1. Introduction

In this tutorial, we are going to learn how to pass arguments to mixins in SASS/SCSS. Mixins allow us to make our CSS more dynamic and reusable. By passing arguments to mixins, we can make our styles even more customizable.

By the end of this tutorial, you'll understand:

  • What mixins are and how they work in SASS/SCSS
  • How to pass arguments to mixins
  • How to use default values for mixin arguments

Prerequisites:

  • A basic understanding of HTML and CSS
  • Familiarity with SASS/SCSS syntax is beneficial but not mandatory

2. Step-by-Step Guide

Mixins in SASS/SCSS

Mixins in SASS/SCSS are a way of reusing a block of CSS declarations. We define a mixin once and can then reuse it throughout our stylesheet.

Passing Arguments to Mixins

Arguments can be passed to mixins to make them more dynamic. This allows the same mixin to create different outputs depending on the argument values.

Default Values

We can give our arguments default values. If we don't pass a value to the mixin when we call it, the default value will be used.

3. Code Examples

Example 1: Basic Mixin with Arguments

Here's an example of a basic mixin that uses arguments:

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

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

In this example, we've created a mixin called border-radius that takes one argument - $radius. We then include this mixin in the .box class and pass 10px as the argument.

Example 2: Mixin with Default Argument Values

Now, let's create a mixin with default argument values:

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

.box {
  @include border-radius; // will use default value of 5px
}

.round-box {
  @include border-radius(15px); // overrides default value
}

In this example, the border-radius mixin has a default value of 5px. When we include this mixin in the .box class without passing an argument, it uses the default value. However, in the .round-box class, we override the default value by passing 15px as the argument.

4. Summary

In this tutorial, we’ve learned how to pass arguments to mixins in SASS/SCSS. This allows us to create more dynamic and reusable styles in our CSS. We also learned how to set default values for mixin arguments.

To further your understanding, consider exploring how to pass multiple arguments to mixins and how to use keyword arguments.

5. Practice Exercises

Exercise 1:

Create a mixin that takes two arguments: $width and $height. This mixin should set the width and height of an element. Use this mixin in a class of your choice.

Solution:

@mixin dimensions($width, $height) {
  width: $width;
  height: $height;
}

.box {
  @include dimensions(100px, 200px);
}

Exercise 2:

Modify the dimensions mixin from Exercise 1 to include default values for $width and $height. Then, use this mixin in two different classes - one with argument values and one without.

Solution:

@mixin dimensions($width: 50px, $height: 50px) {
  width: $width;
  height: $height;
}

.box {
  @include dimensions(100px, 200px);
}

.small-box {
  @include dimensions; // will use default values
}

Remember, practice is key in mastering any programming concept. Happy coding!