Default parameter values let you set a fallback value for a function or mixin parameter. This means if you don't give a value, it uses the default.
Default parameter values in SASS
@mixin name($param: default-value) { // styles } // or for functions @function name($param: default-value) { @return something; }
Use a colon : to assign the default value after the parameter name.
You can use any valid Sass value as a default, like colors, numbers, strings, or lists.
@mixin button($color: blue) { background-color: $color; color: white; padding: 0.5rem 1rem; border-radius: 0.25rem; }
@mixin box($size: 100px) { width: $size; height: $size; background: gray; }
@function double($number: 2) { @return $number * 2; }
This example shows a card mixin with two default parameters: background color and padding. The .card-default uses the default values. The .card-custom overrides both defaults.
@mixin card($bg-color: #f0f0f0, $padding: 1rem) { background-color: $bg-color; padding: $padding; border-radius: 0.5rem; box-shadow: 0 0 5px rgba(0,0,0,0.1); } .card-default { @include card(); } .card-custom { @include card(#cce5ff, 2rem); }
Default values make your mixins and functions easier to reuse.
You can override defaults by passing your own values when calling the mixin or function.
Always choose sensible defaults that fit most cases to save time.
Default parameter values provide fallback values for mixins and functions.
They help avoid repeating common values and make code flexible.
You can override defaults by passing new values when using the mixin or function.