Sass mixins

By Amr

Learn how to use SASS mixins for modular CSS in Bootstrap 5, enhancing your front-end development with reusable styles.

Estimated reading time: 3 minutes

Edit on Github

SASS mixins are powerful tools that allow you to reuse blocks of styling across your CSS, making your front-end code more modular and maintainable. In the context of Bootstrap 5, mixins are extensively used to manage responsive design, typography, spacing, and other styling features.

Here’s a step-by-step guide on how to use and modify SASS mixins, with a focus on Bootstrap 5:


1. What Are SASS Mixins?

A SASS mixin is like a reusable function for CSS rules. You define it once and include it wherever needed. You can also pass parameters to mixins for more flexibility.

Example:

@mixin rounded-corners($radius) {
  border-radius: $radius;
}

// Usage
.box {
  @include rounded-corners(10px);
}

2. Using Bootstrap 5 Mixins

Bootstrap 5 includes a variety of built-in mixins for breakpoints, spacing, typography, and more. These mixins are located in the Bootstrap SASS source files.

Example 1: Responsive Design with Breakpoint Mixins

Bootstrap provides breakpoint mixins for handling responsive styling:

.my-container {
  @include media-breakpoint-up(md) {
    background-color: blue;
  }
  @include media-breakpoint-down(sm) {
    background-color: red;
  }
}
  • media-breakpoint-up($breakpoint) applies styles from the specified breakpoint upwards.
  • media-breakpoint-down($breakpoint) applies styles from the specified breakpoint downwards.

Example 2: Spacing Utilities

You can use Bootstrap’s spacing mixins to control padding and margin:

.card {
  @include spacer($size: 3); // Adds padding or margin of size 3 (default scale in Bootstrap)
}

Example 3: Typography Mixins

Customize text styles with typography mixins:

.heading {
  @include text-emphasis(primary); // Uses Bootstrap's primary text color
  @include text-truncate;          // Adds text truncation (ellipsis)
}

3. Modifying or Extending Bootstrap Mixins

If you want to customize or extend Bootstrap mixins, follow these steps:

  1. Install Bootstrap via npm or download the source files:
    npm install bootstrap
    
  2. Import Bootstrap’s SCSS in your custom SASS file:
    @import "bootstrap/scss/bootstrap";
    
  3. Override or Add Your Mixins: You can redefine mixins by creating your own custom styles or modifying existing ones.

    Example:

    // Override Bootstrap's default spacer values
    @mixin spacer($size) {
      margin: $size * 0.5 !important;
      padding: $size * 0.5 !important;
    }
    
    .custom-card {
      @include spacer(20px); // Uses modified spacing logic
    }
    

4. Custom SASS Mixins in Bootstrap Projects

If you want to create your own mixins for use alongside Bootstrap, here’s how:

Create a Custom Mixin:

@mixin custom-button($bg-color, $text-color, $padding: 1rem) {
  background-color: $bg-color;
  color: $text-color;
  padding: $padding;
  border-radius: 5px;
  &:hover {
    background-color: darken($bg-color, 10%);
  }
}

Use the Mixin:

.btn-custom {
  @include custom-button(#007bff, #ffffff);
}

5. Bootstrap 5-Specific Tips

  • Breakpoints: Use media-breakpoint-up and media-breakpoint-down for responsive designs.
  • Theming: Bootstrap’s SASS files are designed for easy theming. Use SASS variables (e.g., $primary, $secondary) in your mixins for consistent theming.
  • Utility-First Design: Bootstrap 5 embraces utility classes, but mixins allow for deeper customization when utilities fall short.

6. Compiling SASS into CSS

Make sure your SASS files are compiled into CSS using tools like Webpack, Gulp, or the command line:

sass input.scss output.css

By using SASS mixins effectively, you can create modular, customizable styles that seamlessly integrate with Bootstrap 5. Let me know if you’d like examples for specific use cases!