SASS/SCSS / Mixins and Functions

Passing Arguments to Mixins

In this tutorial, you'll learn how to pass arguments to mixins in SASS/SCSS. This enables more dynamic and customizable styles in your CSS files.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Teaches how to create reusable code using mixins and functions in SASS/SCSS.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

Timestamp Converter

Convert timestamps to human-readable dates.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help