Mixins let you define a reusable group of CSS declarations once and apply it anywhere with a single line. This lesson explains how to define a mixin with @mixin, how it differs from a function or a placeholder selector, and common real-world use cases.
What Is a Sass Mixin?
A mixin is a named, reusable block of styles defined with @mixin and applied with @include. Unlike a function, which returns a single value, a mixin can output any number of CSS declarations, nested rules, or even other at-rules.
Mixins are ideal for patterns you repeat across many components: flexbox centering, truncating text, visually hidden utility styles, or applying a consistent box-shadow.
@include flex-center; inserts the mixin's three declarations directly into .hero, exactly as if you had typed them by hand.
Mixin Definition Syntax
@mixin name {
// declarations
}
@include name;
@mixin name { ... } defines a reusable block of declarations.
@include name; applies that block wherever it is called.
A mixin can contain plain declarations, nested selectors, media queries, or even other @include calls.
Mixins can optionally accept parameters, covered in the dedicated Parameters lesson.
Sass Mixins Cheatsheet
Common mixin patterns you will reuse across almost every project.
Mixin
Purpose
Applied With
flex-center
Centers content with flexbox
@include flex-center;
truncate-text
Single-line ellipsis truncation
@include truncate-text;
visually-hidden
Hides visually, keeps for screen readers
@include visually-hidden;
clearfix
Legacy float-clearing fix
@include clearfix;
respond-to($bp)
Parameterized media query
@include respond-to(md) { ... }
box-shadow-md
Consistent elevation shadow
@include box-shadow-md;
Practical Mixin Examples
Some of the most commonly reused mixins across real projects solve small, repetitive layout and text problems that would otherwise require copy-pasting the same three or four declarations everywhere.
A mixin can accept an entire block of styles from the caller using @content, which is especially useful for wrapping arbitrary rules in a media query or selector without repeating that wrapper everywhere.
Whatever styles you pass inside the { } block become the @content inside the mixin definition.
Mixins vs Functions vs Placeholders
Mixins, functions, and placeholder selectors (covered later) all promote reuse, but solve different problems. Choosing the right tool keeps your Sass code intention-revealing.
Tool
Returns
Best For
@mixin
Any number of declarations
Reusable groups of styles, optionally parameterized
@function
A single value
Computing a value (like converting px to rem)
%placeholder + @extend
Shared declarations via inheritance
Sharing styles without duplicating output CSS per call
Common Mistakes
Reaching for a mixin when a function would be simpler, for example computing a single spacing value.
Defining a mixin with no parameters purely to avoid typing a few repeated lines, when a placeholder selector with @extend would produce smaller compiled CSS.
Forgetting @content when a mixin needs to wrap caller-provided styles inside a media query or selector.
Writing overly generic, do-everything mixins that are harder to understand than just writing the CSS directly.
Key Takeaways
A mixin is a reusable, named block of declarations defined with @mixin and applied with @include.
Mixins can output any number of declarations, nested rules, or even other at-rules like media queries.
@content lets a mixin accept and wrap an arbitrary block of styles from the caller.
Choose mixins for reusable groups of styles, functions for computing single values, and placeholders for pure inheritance without parameters.
Pro Tip
Before writing a new mixin, ask whether a function or a placeholder selector fits better, mixins are powerful, but overusing them for simple value calculations makes a codebase harder to read than necessary.
You now understand how to define and use Sass mixins. Next, take a closer look at the @include rule and the different ways to apply a mixin.