Difference Between a Mixin and a Function in Sass

Sass (Syntactically Awesome Style Sheets) provides two powerful features: mixins and functions. While both are used to enhance the capabilities of your stylesheets, they serve different purposes and have distinct characteristics. Below, we will explore the differences between mixins and functions in Sass, along with examples for each.

1. Definition

A mixin is a block of styles that can be reused throughout your stylesheet. Mixins can include CSS properties and can accept parameters to create dynamic styles. They are primarily used to apply styles to selectors.

A function, on the other hand, is a piece of code that performs a calculation or operation and returns a value. Functions are typically used to manipulate values, such as colors or sizes, and can be called within other styles or mixins.

2. Syntax

Mixin Syntax


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

Function Syntax


@function calculate-rem($pixels) {
@return $pixels / 16 * 1rem;
}

3. Usage

Mixins are included in selectors using the @include directive, while functions are called using the @include directive or directly within property values.

Example of Using a Mixin


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

.button {
@include border-radius(5px); /* Using the mixin */
background-color: #3498db;
color: white;
}

Example of Using a Function


@function calculate-rem($pixels) {
@return $pixels / 16 * 1rem;
}

body {
font-size: calculate-rem(16px); /* Using the function */
}

4. Return Values

Mixins do not return values; they simply output styles when included. Functions, however, always return a value that can be used in your stylesheets.

Mixin Example (No Return Value)


@mixin box-shadow($x, $y, $blur, $color) {
-webkit-box-shadow: $x $y $blur $color;
-moz-box-shadow: $x $y $blur $color;
box-shadow: $x $y $blur $color;
}

.card {
@include box-shadow(0, 2px, 5px, rgba(0, 0, 0, 0.3)); /* Outputs styles */
}

Function Example (Returns a Value)


@function double($number) {
@return $number * 2;
}

p {
font-size: double(16px); /* Returns 32px */
}

5. Use Cases

Mixins are best used for applying reusable styles, such as vendor prefixes or complex styles that need to be reused across multiple selectors. Functions are ideal for calculations, such as converting units or manipulating colors.

Mixin Use Case


@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}

.container {
@include flex-center; /* Applying flex styles */
}

Function Use Case


@function lighten($color, $amount) {
@return lighten($color, $amount);
}

.button {
background-color: lighten(#3498db, 10%); /* Lightens the color */
}

Conclusion

In summary, mixins and functions in Sass serve different purposes. Mixins are used to create reusable blocks of styles that can be included in selectors, while functions are used to perform calculations and return values that can be utilized within styles. Understanding the differences between these two features allows developers to write more efficient and organized Sass code, ultimately leading to better maintainability and scalability of stylesheets.