Performance: Mixins with parameters
This affects CSS generation size and browser rendering speed by controlling how styles are reused and customized.
@mixin button-style($bg-color) { padding: 1rem 2rem; border-radius: 0.5rem; background-color: $bg-color; color: white; } .button-primary { @include button-style(blue); } .button-secondary { @include button-style(gray); }
@mixin button-style { padding: 1rem 2rem; border-radius: 0.5rem; background-color: blue; color: white; } .button-primary { @include button-style; } .button-secondary { @include button-style; background-color: gray; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Non-parameterized mixins with many variations | N/A | N/A | Higher due to larger CSS | [X] Bad |
| Parameterized mixins reusing styles efficiently | N/A | N/A | Lower due to smaller CSS | [OK] Good |
@mixin with parameters in Sass?@mixin does@mixin lets you write a block of styles once and reuse it multiple times.color?$color must be used with a dollar sign to refer to its value.@mixin box-shadow($x, $y, $blur, $color) {
box-shadow: $x $y $blur $color;
}
.button {
@include box-shadow(2px, 4px, 6px, rgba(0,0,0,0.3));
}.button class?@mixin border-radius($radius) {
border-radius: radius;
}
.box {
@include border-radius(10px);
}$radius must be referenced with a dollar sign inside the mixin body.radius without $, so it is treated as a literal string, causing wrong CSS.responsive-text that sets font size differently for mobile and desktop. It takes two parameters: $mobile-size and $desktop-size. Which Sass code correctly applies the font sizes using a media query inside the mixin?min-width media query.font-size: $mobile-size; outside media query, then inside @media (min-width: 768px) sets font-size: $desktop-size;.Pass the test and this pattern moves unknown