Mixins and functions
🎯 Introduction
By the end of this article, you will be able to use SCSS mixins and functions to make your CSS more reusable and configurable. From button colors and sizes to theme adjustments, these tools keep code concise and make styles easier to manage consistently, saving development and maintenance time.
✨ Basic usage of @mixin / @include
A mixin is a reusable template for styles. Define a block of frequently used declarations, then include it wherever it is needed. This reduces duplication and keeps styles clear.
Here is a simple example:
@mixin center-flex {
display: flex;
justify-content: center;
align-items: center;
}
.box {
@include center-flex;
width: 100px;
height: 100px;
background-color: skyblue;
}
.box {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 100px;
background-color: skyblue;
}
@mixin center-flex defines a block of styles for centering content with flexbox. .box reuses it with @include center-flex.
🎨 Mixins with parameters (button sizes, colors, and more)
Reusable styles become more useful when you can customize them. Mixins accept arguments so you can adjust values such as button sizes and colors.
Consider this button example:
@mixin button-style($bg-color, $font-size: 16px) {
background-color: $bg-color;
color: white;
padding: 10px 20px;
font-size: $font-size;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.btn-primary {
@include button-style(#3498db);
}
.btn-large {
@include button-style(#e74c3c, 20px);
}
.btn-primary {
background-color: #3498db;
color: white;
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.btn-large {
background-color: #e74c3c;
color: white;
padding: 10px 20px;
font-size: 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
The button-style mixin accepts a background color and font size. Pass different values to generate different button styles.
🛠️ Custom functions with @function
Alongside mixins, Sass provides @function for computing values. Use a function to calculate dimensions from a number or determine a color's opacity.
For example, here is a function that calculates spacing from an input value:
@function spacing($size) {
@return $size * 8px;
}
.container {
margin: spacing(2);
padding: spacing(1);
}
.container {
margin: 16px;
padding: 8px;
}
The spacing function accepts a number and returns that number multiplied by 8px. This lets you control spacing with a numeric scale and keeps the code readable.
🌈 Built-in functions: map.get()
map.get($map, $key) retrieves a value from a map, helping you manage complex theme colors or variable collections.
Here is an example combining these features:
@use 'sass:map';
$themes: (
primary: #3498db,
secondary: #2ecc71,
danger: #e74c3c,
);
@mixin themed-button($type) {
$color: map.get($themes, $type);
background-color: $color;
color: white;
padding: 10px 16px;
border-radius: 3px;
border: none;
cursor: pointer;
}
.btn-primary {
@include themed-button(primary);
}
.btn-danger {
@include themed-button(danger);
}
.btn-primary {
background-color: #3498db;
color: white;
padding: 10px 16px;
border-radius: 3px;
border: none;
cursor: pointer;
}
.btn-danger {
background-color: #e74c3c;
color: white;
padding: 10px 16px;
border-radius: 3px;
border: none;
cursor: pointer;
}
map.get retrieves the color value, keeping the code simple and flexible.
💡 Reuse and configurability
Mixins and functions provide reuse by reducing duplicated code. Parameters make styles configurable, so you can create variations without writing many nearly identical rules.
This is particularly useful in large projects, where it saves time and simplifies later changes and maintenance.
❓ Questions and answers
Q1: What is the difference between a mixin and a function?
A mixin reuses a block of style declarations, while a function returns a computed value.
Q2: Can mixin parameters have default values?
Yes. Assign defaults when defining the parameters. Those defaults are used when the corresponding arguments are omitted.
Q3: What is map.get() used for?
It retrieves values from a map, making complex collections of variables easier to manage.
🧪 Exercises
Exercise 1: Write a mixin for a card with a shadow and rounded corners. Accept the shadow color and corner radius as parameters.
@mixin card($shadow-color, $border-radius) {
// Add your code here
}
.card {
@include card(rgba(0, 0, 0, 0.15), 8px);
}
Exercise 2: Write a function that calculates font size from an input number, using font-size = base-size + n * step, where base-size is 14px and step is 2px.
@function dynamic-font-size($n) {
// Add your code here
}
.text {
font-size: dynamic-font-size(3);
}
🎉 You now know how to use mixins, mixin parameters, custom functions, and built-in Sass functions! Apply them in your own projects to make styles more efficient, flexible, and maintainable!
