HTML and CSS course Β· Module 7: SASS

Mixins in Sass

4 min read
In this lesson4

Mixins in Sass allow you to define styles that can be reused throughout the stylesheet without repeating code. Mixins are especially useful when you need to use the same CSS declarations in multiple places.

Defining a Mixin

To define a mixin, use the @mixin directive followed by the mixin name. For example:

1@mixin reset-list {
2  margin: 0;
3  padding: 0;
4  list-style: none;
5}

Using a Mixin

To use a mixin, use the @include directive followed by the mixin name. For example:

1ul, ol {
2  @include reset-list;
3}

After compiling the above code to CSS, we get:

1ul, ol {
2  margin: 0;
3  padding: 0;
4  list-style: none;
5}

Mixins with Arguments

Mixins in Sass can also accept arguments, making them even more flexible. For example, we can create a mixin that generates styles for buttons of different colors:

1@mixin button-color($color) {
2  background-color: $color;
3  color: white;
4  &:hover {
5    background-color: darken($color, 10%);
6  }
7}
8
9.button-primary {
10  @include button-color(blue);
11}
12
13.button-danger {
14  @include button-color(red);
15}

After compiling to CSS, we get:

1.button-primary {
2  background-color: blue;
3  color: white;
4}
5.button-primary:hover {
6  background-color: darkblue;
7}
8
9.button-danger {
10  background-color: red;
11  color: white;
12}
13.button-danger:hover {
14  background-color: darkred;
15}

As you can see, mixins are a very powerful tool in Sass that allows you to significantly simplify your code and increase its reusability. However, remember to use them in moderation to avoid unnecessarily complicating your code.

Exercise: Creating and Using Mixins in a Sass Project

Exercise Goals

  1. Create mixins for resetting lists, creating buttons, and setting up layouts.
  2. Apply the created mixins to various page elements.
  3. Refactor existing CSS code using mixins.

Step 1: File Structure

Create the following file structure for the project:

1project/
2|-- scss/
3|   |-- _mixins.scss
4|   |-- main.scss
5|-- css/
6|   |-- main.css
7|-- index.html

Step 2: Defining Mixins

1. Resetting Lists

In the _mixins.scss file, define a reset-list mixin that resets list styles:

1// _mixins.scss
2@mixin reset-list {
3  margin: 0;
4  padding: 0;
5  list-style: none;
6}

2. Creating Buttons

Add a button-style mixin that allows creating buttons of different colors:

1// _mixins.scss
2@mixin button-style($color) {
3  background-color: $color;
4  color: white;
5  padding: 10px 20px;
6  border: none;
7  border-radius: 5px;
8  cursor: pointer;
9  &:hover {
10    background-color: darken($color, 10%);
11  }
12}

3. Layout

Add a center-flex mixin that centers elements using flexbox:

1// _mixins.scss
2@mixin center-flex {
3  display: flex;
4  justify-content: center;
5  align-items: center;
6}

Step 3: Using Mixins

In the main.scss file, import the mixins and use them to style elements:

1// main.scss
2@use 'mixins';
3
4ul, ol {
5  @include mixins.reset-list;
6}
7
8.button-primary {
9  @include mixins.button-style(blue);
10}
11
12.button-danger {
13  @include mixins.button-style(red);
14}
15
16.container {
17  @include mixins.center-flex;
18  height: 100vh;
19}

Step 4: HTML

Create an index.html file and add the following structure:

1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="UTF-8">
5  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6  <title>Sass Project</title>
7  <link rel="stylesheet" href="css/main.css">
8</head>
9<body>
10  <div class="container">
11    <ul>
12      <li>Element 1</li>
13      <li>Element 2</li>
14      <li>Element 3</li>
15    </ul>
16    <button class="button-primary">Primary Button</button>
17    <button class="button-danger">Danger Button</button>
18  </div>
19</body>
20</html>

Step 5: Compiling Sass

Use a Sass compilation tool (e.g., node-sass, Dart Sass, or your editor's preprocessor) to generate the CSS file (main.css) from the Sass file (main.scss).

Exercise

  1. Adding New Mixins: Create a box-shadow mixin that adds a shadow to an element, and apply it to the buttons.
  2. Using a Mixin with Arguments: Create a mixin that sets margins with arguments for top, right, bottom, and left margins, and apply it to the list.
  3. Expanding the Layout: Create a layout-grid mixin that arranges elements in a grid layout, and apply it to the container.
  4. Code Optimization: Analyze the code and use mixins to make it more modular and easier to maintain.

This exercise will help you understand how to define and use mixins in Sass, leading to more organized and efficient CSS code.

Code for this lesson: index.html
1<!DOCTYPE html>
2<html lang="en">
3<head><title>Text Fields</title></head>
4<body style="background:#d4a574;font-family:serif;padding:20px;color:#3d2914">
5<h1>Text Fields</h1>
6<p>Input type=text - like papyrus for writing.</p>
7</body>
8</html>

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How do you define a mixin in SASS?

  2. 2. How do you use a mixin in SASS?

Hands-on tasks in the game

  • Code editor

    Create @mixin flex-center { display: flex; justify-content: center; align-items: center }. Use it: .card { @include flex-center; }, .hero { @include flex-center; height: 100vh }. Add @mixin shadow($size) with a parameter!

  • Click in order

    Build a SASS mixin declaration with a parameter:

  • Code editor

    Create @mixin button-style($bg, $color) { background: $bg; color: $color; padding: 10px 20px; border: none; border-radius: 5px; }. Use it: .btn-primary { @include button-style(#2C3E50, white); }, .btn-secondary { @include button-style(#D4AF37, black); }. Add a $radius parameter with a default value!

  • Vertical ordering

    Arrange the steps for creating a mixin:

Useful articles