Parametric mixins accept arguments, just like a function, making them far more reusable than a fixed block of declarations. This lesson covers defining parameters, default values, and the separator rules that trip up beginners.
What Are Parametric Mixins?
A parametric mixin declares one or more parameters inside its parentheses, each prefixed with @. When called, you supply values for those parameters, and every use of the parameter name inside the mixin's body is replaced with the supplied value.
Parameters can have default values, making them optional at the call site. If a caller omits an argument that has a default, the default value is used instead.
Default values are written after a colon inside the parameter list.
Arguments are matched positionally, in the order they're passed, unless called by name.
Named arguments (@param: value) can be passed in any order at the call site.
Use a semicolon separator instead of commas when an argument itself contains a comma-separated list.
Parametric Mixins Cheatsheet
Common parameter patterns for defining and calling mixins.
Pattern
Example
Notes
Required param
.box(@size) { }
No default, must be supplied
Default value
.box(@size: 10px) { }
Optional at the call site
Multiple params
.box(@w, @h: @w) { }
Later defaults can reference earlier params
Named argument call
.box(@color: red);
Bypasses positional order
Semicolon separator
.font(@family; @size; @weight);
Needed when an argument is a comma list
Variable arguments
.mixin(@rest...) { }
Collects remaining args into a list
Defaults That Reference Other Parameters
A parameter's default value can reference an earlier parameter in the same list, which is useful for square or proportional defaults where one dimension should match another unless explicitly overridden.
By default, arguments are separated with commas. But if any single argument is itself a comma-separated list, like a box-shadow value, you must switch the mixin's overall argument separator to semicolons to avoid ambiguity.
Semicolons make each parameter's boundary unambiguous, even though @color's value contains commas.
Variable Argument Lists
A mixin parameter followed by ... collects any remaining arguments into a single list variable, similar to rest parameters in JavaScript. This is useful for mixins that forward an arbitrary number of values to a CSS property like transition or grid-template-columns.
Mixing commas inside an argument value while the mixin's own separator is also a comma, causing LESS to misread argument boundaries.
Forgetting that positional arguments must be supplied in order, unless called by name.
Assuming a required parameter (no default) can be silently omitted at the call site; LESS will error instead.
Overcomplicating a mixin with too many optional parameters instead of splitting it into smaller, focused mixins.
Key Takeaways
Parametric mixins accept arguments in parentheses, each prefixed with @.
Default values make parameters optional; omitted arguments fall back to their default.
Use semicolons instead of commas to separate arguments whenever one argument contains a comma-separated list.
@rest... collects any remaining arguments into a single list variable.
Pro Tip
Whenever a mixin argument might be a comma-separated value, like a shadow or gradient, switch that mixin's parameter separator to semicolons from the start. It avoids a confusing class of bugs later.
You now understand parametric mixins with defaults. Next, learn how mixin guards add conditional logic.