Sass @mixin & @include
A mixin is a named, reusable group of declarations (and even whole rules). You define it once with @mixin and paste it anywhere with @include. Mixins can take arguments, so one mixin can produce many variations.
Defining and including a mixin
@mixin flex-center {
display: flex;
align-items: center;
justify-content: center;
}
.hero {
@include flex-center;
height: 120px;
background: #fbe9f2;
}
.avatar {
@include flex-center;
width: 48px;
height: 48px;
border-radius: 50%;
background: #cc6699;
color: white;
}.hero {
display: flex;
align-items: center;
justify-content: center;
height: 120px;
background: #fbe9f2;
}
.avatar {
display: flex;
align-items: center;
justify-content: center;
width: 48px;
height: 48px;
border-radius: 50%;
background: #cc6699;
color: white;
}
The mixin's declarations were copied into each rule that included it.
Arguments
Mixins accept arguments just like functions in other languages. Arguments can have default values:
@mixin button($bg, $fg: white, $radius: 6px) {
background: $bg;
color: $fg;
border: none;
border-radius: $radius;
padding: 8px 16px;
}
.btn-primary { @include button(#cc6699); }
.btn-light { @include button(#f1f1f1, #333); }
.btn-pill { @include button(#2d2a32, $radius: 999px); } // keyword argument.btn-primary {
background: #cc6699;
color: white;
border: none;
border-radius: 6px;
padding: 8px 16px;
}
.btn-light {
background: #f1f1f1;
color: #333;
border: none;
border-radius: 6px;
padding: 8px 16px;
}
.btn-pill {
background: #2d2a32;
color: white;
border: none;
border-radius: 999px;
padding: 8px 16px;
}
| Form | Example |
|---|---|
| Positional | @include button(#cc6699, white) |
| Keyword (any order) | @include button($radius: 0, $bg: red) |
| Default value | @mixin button($fg: white) |
| Variable arguments | @mixin shadows($layers...) |
Variable arguments with ...
A final argument ending in ... collects any number of values into a list:
@mixin transition($props...) {
transition-property: $props;
transition-duration: 200ms;
}
.card {
@include transition(transform, box-shadow, opacity);
}.card {
transition-property: transform, box-shadow, opacity;
transition-duration: 200ms;
}
Passing a block with @content
A mixin can receive a whole block of styles and decide where to put it, using @content. This is the basis of breakpoint and state mixins:
@mixin hover-and-focus {
&:hover,
&:focus-visible {
@content;
}
}
@mixin dark {
@media (prefers-color-scheme: dark) {
@content;
}
}
.link {
color: #cc6699;
@include hover-and-focus {
text-decoration: underline;
}
@include dark {
color: #f3a6cb;
}
}.link {
color: #cc6699;
}
.link:hover, .link:focus-visible {
text-decoration: underline;
}
@media (prefers-color-scheme: dark) {
.link {
color: #f3a6cb;
}
}
Tip: Mixins can live in a partial (for example
_mixins.scss) and be loaded with@use "mixins";. You then include them with the namespace:@include mixins.flex-center;.
Mixin vs @extend vs function
| Tool | Produces | Use it for |
|---|---|---|
@mixin |
Copies declarations into each rule | Reusable patterns with arguments |
@extend |
Groups selectors into one rule | Sharing identical styles between semantic classes |
@function |
A single value | Calculations (rem(24px)) |
Mixins duplicate their output each time you include them. That's fine — gzip compresses repetition very well — but it's another reason to keep mixins small.
Test yourself
Which rule inserts a mixin's styles?
- @mixin
- @use
- @include
- @extend
What does @content inside a mixin do?
- Outputs the mixin's name
- Inserts the block of styles passed to
@include - Includes another file
- Prints debug info
Given @mixin box($w, $h: $w), what does @include box(10px) set $h to?
- null
- 0
- 10px
- An error