Register to get access to free programming courses with interactive exercises

Mixins SASS: Fundamentals of Operations

An important tool in the SASS preprocessor's toolkit is mixins. These are repeating portions of code that we can include in various selectors. It sounds like template selectors, right? It's just like that in its basic form, but mixins have many more options.

First, let's deal with reusing styles. Let's create a simple style reset mixin.

To create a mixin, we specify the keyword @mixin and give it a unique name. Inside this mixin, we write down all the necessary rules:

@mixin reset {
  margin: 0;
  padding: 0;
}

To include a mixin in the selector, we use the @include keyword and then give the mixin's name we want to include. For example, let's reset the indents in the bulleted list:

@mixin reset {
  margin: 0;
  padding: 0;
}

ul {
  @include reset;
}

After compiling, we get the following CSS code:

ul {
  margin: 0;
  padding: 0;
}

It's also possible to nest mixins into each other, which allows you to make a new mixin based on an existing one:

@mixin flex-between {
  display: flex;
  justify-content: space-between;
}

@mixin reset {
  margin: 0;
  padding: 0;
}

@mixin horizontal-menu {
  @include reset;
  @include flex-between;

  list-style: none;

  .menu-item {
    margin: 0 10px;
  }
}

.menu {
  &.horizontal-menu {
    @include horizontal-menu;
  }
}

Note that inside the horizontal-menu mixin, we connected the two above mixins and then used it to style our menu. All styles from the flex-between and reset mixins will be inside the horizontal-menu mixin. The final CSS code will look like this:

.menu.horizontal-menu {
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: space-between;
  list-style: none;
}

.menu.horizontal-menu .menu-item {
  margin: 0 10px;
}

The preprocessor runs sequentially, so the order in which we include the mixins may make a difference in some cases. Don't forget to keep an eye on it. Also, don't forget to supervise the nesting level of the mixins. Although SASS allows you to nest mixins within each other, I don't recommend going too deep. The more we nest mixins, the less control we have over the styles. The creators of mixins designed them primarily to make it easier to write repeating CSS code. If you feel like you're going too deep with nesting, stop, breathe, and think. Maybe you find that it is better to use some styles without mixins.

Often, developers use mixins for properties that need support in older browsers and specify vendor prefixes for these properties. If we use the box-shadow property and need support for older browsers like Firefox 3.5, Chrome 9, and Safari 4.3, we should use the vendor prefixes -moz- and -webkit-. This is the kind of code we can use mixins for:

@mixin box-shadow-prefix {
  -webkit-box-shadow: 10px 10px 5px 0px rgba(0,0,0,0.75);
  -moz-box-shadow: 10px 10px 5px 0px rgba(0,0,0,0.75);
  box-shadow: 10px 10px 5px 0px rgba(0,0,0,0.75);
}

.box-shadow {
  @include box-shadow-prefix;
}

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY
Suggested learning programs
profession
Layout with the latest CSS standards
5 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.