Performance: Functions with parameters
This affects the CSS generation time and the final CSS file size, impacting page load speed.
@function shade-color($color, $percent) { @return mix(black, $color, $percent / 100); } $primary-color: #3498db; .button { background-color: $primary-color; } .alert { background-color: shade-color($primary-color, 20); }
$primary-color: #3498db; .button { background-color: #3498db; } .alert { background-color: #2980b9; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Hardcoded repeated colors | No impact | No impact | No impact | [X] Bad |
| Parameterized functions for colors | No impact | No impact | No impact | [OK] Good |
@function with parameters in Sass?@function does@function defines reusable code that can take inputs and return values.double that takes one parameter $n?@function followed by the function name and parameters in parentheses.@return to send back the result.@function add($a, $b) { @return $a + $b; }
.box {
width: add(10px, 5px);
}add returns the sum of two values, here 10px + 5px = 15px.@function multiply($x, $y) {
$result: $x * $y;
}@return to send back a value; otherwise, they produce no output.responsive-size that takes a pixel value $px and returns a rem value assuming 16px base font size. Which function definition correctly achieves this?Pass the test and this pattern moves unknown