Functions and mixins help you reuse code in Sass. Functions return values, while mixins add styles.
Functions vs mixins comparison in SASS
@function function-name($params) { @return value; } @mixin mixin-name($params) { property: value; // more styles }
Functions must return a value using @return.
Mixins insert CSS rules directly where they are included using @include.
@function double($number) { @return $number * 2; } @mixin box-shadow($x, $y, $blur, $color) { box-shadow: $x $y $blur $color; }
$width: double(10px); .container { width: $width; @include box-shadow(2px, 2px, 5px, rgba(0,0,0,0.3)); }
This Sass code defines a function to convert pixels to rem units. The mixin creates button styles using that function. The button class uses the mixin and function to style a button with consistent spacing and font size.
@function calculate-rem($px) { @return $px / 16 * 1rem; } @mixin button-style($bg-color, $text-color) { background-color: $bg-color; color: $text-color; padding: calculate-rem(12px) calculate-rem(20px); border: none; border-radius: calculate-rem(4px); cursor: pointer; } .button { @include button-style(#3498db, #fff); font-size: calculate-rem(16px); }
Functions only return values and cannot output CSS rules directly.
Mixins can include other mixins and functions for flexible styling.
Use functions for calculations and mixins for reusable style blocks.
Functions return values like numbers, colors, or strings.
Mixins add CSS rules where included.
Use both to write cleaner, reusable Sass code.
Practice
function and a mixin in Sass?Solution
Step 1: Understand what functions do in Sass
Functions calculate and return values like colors, numbers, or strings.Step 2: Understand what mixins do in Sass
Mixins insert blocks of CSS rules where they are included.Final Answer:
Functions return values, while mixins output CSS rules. -> Option BQuick Check:
Functions = return values, Mixins = CSS rules [OK]
- Thinking mixins return values like functions
- Confusing output of CSS rules with returned values
- Assuming both do the same thing
double that returns twice the input number?Solution
Step 1: Identify function syntax in Sass
Functions use@functionkeyword and must include a@returnstatement.Step 2: Check each option
@function double($n) { @return $n * 2; } correctly defines a function with@return $n * 2;. Choices using@mixinare incorrect for functions. @function double($n) { $n * 2; } lacks@return.Final Answer:
@function double($n) { @return $n * 2; } -> Option AQuick Check:
Functions need @return [OK]
- Using @mixin instead of @function for functions
- Forgetting the @return statement
- Confusing mixin syntax with function syntax
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}
.button {
@include border-radius(10px);
}Solution
Step 1: Understand what the mixin does
The mixin adds three CSS properties for border-radius with vendor prefixes.Step 2: Check how the mixin is included
The mixin is included inside the.buttonselector with argument10px, so all three properties will appear with that value.Final Answer:
.button { -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; } -> Option AQuick Check:
Mixins output CSS rules where included [OK]
- Expecting only the last property without prefixes
- Thinking mixins return values instead of CSS
- Confusing @include with outputting @include literally
@function add($a, $b) {
$sum: $a + $b;
}
.result {
width: add(10px, 5px);
}Solution
Step 1: Check function definition
The function calculates $sum but does not return it with@return.Step 2: Understand function usage
Functions must return a value to be used inside property values likewidth. Without@return, it returns nothing.Final Answer:
The function does not have a @return statement. -> Option DQuick Check:
Functions must return values to be used [OK]
- Forgetting @return in functions
- Confusing mixins with functions
- Using mixins inside property values
Solution
Step 1: Understand the goal
You want a reusable way to get a color with 50% opacity that can be used anywhere, like in variables or properties.Step 2: Choose between function and mixin
Functions return values, so they can be used inside any property or variable. Mixins output CSS rules directly and cannot return values.Step 3: Evaluate choices
The choice suggesting a mixin that returns a value is incorrect because mixins do not return values. The choice using a mixin to output CSS rules for the color property is less flexible. The choice using a function to output CSS rules has invalid syntax. The choice using a function to return the color value with opacity is best for flexibility.Final Answer:
Use a function to return the color with 50% opacity for flexible use. -> Option CQuick Check:
Functions return values for reuse; mixins output CSS [OK]
- Using mixins when a value return is needed
- Expecting mixins to return values
- Trying to output CSS from functions
Ready to prove it?
Pass the test and this pattern moves unknown
