Introduction
Mixins with parameters let you reuse styles but change details each time. This saves time and keeps your code neat.
Mixins with parameters let you reuse styles but change details each time. This saves time and keeps your code neat.
@mixin name($param1, $param2) { property: $param1; property: $param2; } .selector { @include name(value1, value2); }
@mixin to define reusable style blocks with parameters.@include to apply the mixin and pass values.@mixin button($bg-color) { background-color: $bg-color; padding: 1rem 2rem; border-radius: 0.5rem; } .btn-primary { @include button(blue); } .btn-danger { @include button(red); }
@mixin box-shadow($x, $y, $blur, $color) { box-shadow: $x $y $blur $color; } .card { @include box-shadow(0.5rem, 0.5rem, 1rem, rgba(0,0,0,0.3)); }
This example shows a mixin that sets text color and size. We use it for a title and a subtitle with different colors and sizes.
@charset "UTF-8"; @mixin colored-text($color, $size) { color: $color; font-size: $size; font-weight: bold; } .title { @include colored-text(#2a9d8f, 2rem); } .subtitle { @include colored-text(#e76f51, 1.5rem); }
Parameters make mixins flexible and powerful.
You can set default values for parameters like $color: black to make them optional.
Mixins help avoid repeating the same code with small changes.
Mixins with parameters let you reuse styles but customize them.
Use @mixin to create and @include to apply with values.
This keeps your styles clean, consistent, and easy to update.
@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