Sass·Sass from variables to a design system

Mixins

Reuse declarations with mixins and @include.

Intermediate 13 min read Lesson 5 of 300 Challenge included
What you will be able to do
  • Declare with @mixin name($args).
  • Include with @include name(args).
  • Use defaults for optional arguments.
  • Keep mixins focused on one pattern.

In plain English

A mixin is a reusable block of declarations with optional arguments. Use it for patterns such as focus rings, truncation or responsive breakpoints. Do not turn every rule into a mixin.

mixins.scssSCSS
@mixin focus-ring($color: #2f74c0) {
  outline: 2px solid $color;
  outline-offset: 2px;
}

.button {
  &:focus-visible { @include focus-ring; }
}
Your turn

Declare a mixin

Medium

Declare a mixin named truncate that hides overflow and adds an ellipsis.

SCSS · starter
@mixin
Check your understanding before moving on.
Knowledge checkQuestion 1

Which directive applies a mixin?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Functions and operators
Lesson 5 of 300 · 0% of course