Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 39 ยท Mixins and Functions

Sass Mixins with @mixin and @include

Learn Sass mixins: define reusable style blocks with @mixin, apply them with @include, nest rules and the parent selector inside them, and organize mixin files.

Variables store single values. A mixin stores an entire block of styles: several declarations, or even nested rules, that you want to reuse. You define it once with @mixin, then paste it wherever you need it with @include. If you ever copied the same five lines of CSS into a dozen selectors, mixins are the cure.

Defining and including a mixin

SCSS
@mixin center-flex {
  display: flex;
  align-items: center;
  justify-content: center;
}

.hero {
  @include center-flex;
  min-height: 60vh;
}

.modal-backdrop {
  @include center-flex;
  position: fixed;
  inset: 0;
}

Both .hero and .modal-backdrop receive the three flex declarations at the position of the @include. Declarations that come after the include can override the mixin's values, so the order inside the rule matters.

A mixin is just a reusable chunk. Whatever you could write directly inside the including rule, you can put into the mixin.

Your first mixin

Mixins can contain nested rules

A mixin is not limited to declarations. It can hold selectors, pseudo-classes and media queries. The parent selector & inside a mixin refers to the rule that included it:

SCSS
@mixin interactive {
  cursor: pointer;
  transition: transform .15s ease;

  &:hover { transform: translateY(-2px); }
  &:active { transform: translateY(0); }
  &:focus-visible { outline: 3px solid #0ea5e9; outline-offset: 2px; }
}

.card { @include interactive; }
.button { @include interactive; }

This creates .card, .card:hover, .card:active, .card:focus-visible and the same four for .button. A well-designed mixin captures a whole behavior, not just a list of properties.

Real-world mixin examples

Visually hidden text (for screen readers only), a classic accessibility helper:

SCSS
@mixin visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

Text truncation with an ellipsis:

SCSS
@mixin truncate {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

Clearfix, historically used for floats (flex and grid have mostly replaced the need):

SCSS
@mixin clearfix {
  &::after { content: ""; display: table; clear: both; }
}

Mixins used to be the way to write vendor prefixes (-webkit-, -moz-). Tools such as Autoprefixer, PostCSS and Lightning CSS now do that for you automatically from a browser list, so you should not hand-write prefix mixins in a new project.

Behavior mixins: interactive and truncate

Mixins can include other mixins

Build bigger behaviors out of smaller ones:

SCSS
@mixin card-base { padding: 1rem; border-radius: 8px; background: white; }
@mixin card-hover { &:hover { box-shadow: 0 6px 20px rgb(0 0 0 / .12); } }

@mixin card {
  @include card-base;
  @include card-hover;
}

Keep this shallow. A mixin that includes a mixin that includes a mixin is hard to trace, and the compiled CSS tells you nothing about which layer produced a declaration.

Top-level mixins

A mixin may emit styles with no property at all, as long as it only contains rules. When it is included at the top level of a file, the rules are output on their own:

SCSS
@mixin base-reset {
  *, *::before, *::after { box-sizing: border-box; }
  body { margin: 0; }
}

@include base-reset;   // outside any selector

Mixins from modules

Mixins in a partial are used through the module's namespace, like variables:

SCSS
// _mixins.scss
@mixin center-flex { display: flex; align-items: center; justify-content: center; }
SCSS
@use "mixins";

.hero { @include mixins.center-flex; }

A common layout is a abstracts/ or base/ folder holding _variables.scss, _functions.scss and _mixins.scss, forwarded from one index file. See Project architecture.

Mixins versus extend and functions

  • Mixin โ†’ output declarations or rules, may take arguments, may contain nested rules.
  • @extend โ†’ share one rule between selectors, without arguments.
  • Function โ†’ compute and return a single value; cannot emit declarations.

If you find yourself writing @include just to get one calculated value, you probably want a function instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you apply a mixin named center-flex?

    @mixin defines it and @include applies it.
  2. 2. What does & mean inside a mixin?

    The mixin's rules are expanded at the include site, so & refers to the including rule's selector.
  3. 3. Which is the best modern approach to vendor prefixes?

    Build tools add prefixes automatically based on your browser support list, so prefix mixins are unnecessary.
  4. 4. How do you include a mixin from a file loaded with @use "mixins";?

    Members of a module are accessed with the namespace and a dot.

Summary

  • @mixin name { ... } defines reusable styles; @include name; applies them where it is written.
  • Mixins can contain nested rules, pseudo-classes and &, so one mixin can capture a full behavior.
  • Mixins can include other mixins, and can be included at the top level to emit rules.
  • Use @use and module.mixin-name for mixins stored in partials.
  • Prefer functions for computing values and tools like Autoprefixer for vendor prefixes.

Next: Mixin arguments and content blocks