Sub topic 8
Mixins, arguments and @content
This chapter assumes you can write plain CSS rules and media queries. It also assumes you have met SCSS variables and nesting from the earlier chapters.
What a mixin is
A mixin is a named block of declarations or rules that you define once and copy into other rules. You define it with @mixin and use it with @include. Sass pastes the mixin's output at the point of the @include.
Arguments
A mixin can take arguments, like a function. Give an argument a default value with a colon, and callers can then leave it out.
@mixin button($bg, $radius: 4px) {
padding: 0.5rem 1rem;
background: $bg;
border-radius: $radius;
}
.btn-save {
@include button(#3366cc);
}
.btn-pill {
@include button($bg: #cc3333, $radius: 999px);
}.btn-save uses the default radius. .btn-pill passes arguments by keyword, which makes calls with several values easier to read and lets you skip earlier optional arguments. The compiled CSS contains the full set of declarations in each rule:
.btn-save {
padding: 0.5rem 1rem;
background: #3366cc;
border-radius: 4px;
}
.btn-pill {
padding: 0.5rem 1rem;
background: #cc3333;
border-radius: 999px;
}Passing a block with @content
A mixin can accept a block of styles from its caller. Inside the mixin, @content marks where that block goes. This is the standard way to wrap rules in a media query.
@mixin respond($min) {
@media (min-width: $min) {
@content;
}
}
.nav {
display: block;
@include respond(48rem) {
display: flex;
}
}The caller writes the block after the @include, in braces. Sass compiles the result to:
.nav {
display: block;
}
@media (min-width: 48rem) {
.nav {
display: flex;
}
}Sass moves the media query out of .nav and keeps the selector inside it. That is why you can write the breakpoint next to the rule it changes.
Common errors
- Forgetting
@include. Writingbutton(#3366cc);inside a rule is not a mixin call. Sass treats it as invalid CSS or an unknown function. - Missing required arguments.
@include button;fails to compile because$bghas no default. Sass reports a missing argument. - Passing too many arguments. Extra positional arguments are an error. Check the parameter list.
- Using a mixin before it exists. Define the mixin in the same file, or load it with
@use, before you include it. - Expecting mixins to share output. Each
@includecopies the declarations again. A mixin used 40 times produces 40 copies in the CSS. When many selectors need identical, argument-free declarations, a placeholder with@extendcan produce smaller CSS. A later chapter covers that choice.
When to choose a mixin
Use a mixin when the output depends on arguments, or when you need to wrap caller-supplied styles with @content, as in media queries. For a plain value you reuse, use a variable. For a computed value, use a function.
Exercise
Write a mixin named dark-mode that wraps a caller's block in @media (prefers-color-scheme: dark). Then use it so that .page gets background: #111 in dark mode.
Show answer
@mixin dark-mode { @media (prefers-color-scheme: dark) { @content; } } .page { background: #fff; @include dark-mode { background: #111; } }