Skip to content
tezvyn:
Menu

Sass Mixins: Reusable Style Functions

Source: sass-lang.comMediumHow cards are made

Think of a Sass mixin as a function for CSS. It bundles style declarations you can reuse, avoiding repetitive code. Use it for common patterns like button styles or clearing floats. The footgun: mixins copy code everywhere, which can bloat your final CSS.

Why it exists

CSS lacks a native way to reuse groups of properties without creating utility classes like .text-center. This leads to either repetitive code in your stylesheets or non-semantic classes in your HTML. Mixins solve this by letting you define and reuse style blocks within the Sass preprocessor itself.

The mental model

A mixin is like a function for your styles. You define a block of CSS once using @mixin, give it a name, and then call it anywhere you need those styles using @include. Just like a function, it can accept arguments to dynamically change its output, making it incredibly flexible for creating variations of a style.

How it works

You declare a mixin with @mixin my-mixin { ... }. The block can contain any CSS properties and even other Sass rules. To use it, you write @include my-mixin; inside a selector. The Sass compiler then copies the styles from the mixin directly into that selector in the final CSS output. You can also pass arguments, like @mixin button-style(bg-color) { background-color: bg-color; }, and then call it with @include button-style(blue);.

When to use it

Use mixins for style patterns that are repeated across unrelated CSS selectors, especially when you need to pass parameters to create variations. Examples include: creating themed button variants, managing vendor prefixes for experimental CSS properties, or defining complex layout patterns like a flexbox container setup.

When not to use it

Avoid using mixins for styles that should be grouped under a single, shared class rule. If multiple elements share the exact same block of styles without variation, using Sass's @extend with a placeholder selector is more efficient. @extend creates a comma-separated list of selectors for one rule block, whereas @include duplicates the entire rule block for each selector, which can bloat the CSS output.

One canonical example

A common use case is creating a mixin to reset the default styling of a list, which can then be included by other rules. @mixin reset-list { margin: 0; padding: 0; list-style: none; }

nav ul { @include reset-list; }

This compiles to CSS where the reset styles are copied directly into the nav ul rule, keeping your Sass organized and your HTML clean: nav ul { margin: 0; padding: 0; list-style: none; }

Interview question

When is Sass's @extend generally more efficient than a @mixin?

  • a.When defining complex layout patterns that require dynamic values.
  • b.When creating themed component variations based on specific arguments.
  • c.When multiple selectors require identical styles without any variation.Correct
  • d.When applying vendor prefixes to experimental CSS properties.
Why?

The card states that if multiple elements share the exact same block of styles without variation, @extend is more efficient because it creates a single rule block with multiple selectors, unlike @include which duplicates the entire rule block. Options A, B, and D describe scenarios where mixins are typically used, especially when dynamic values or variations are needed.

Just read this? Test yourself on what you have been reading.

Read the original → sass-lang.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles