Mixins in SASS are reusable blocks of code that can accept arguments and include dynamic content. 1. Define a mixin using @mixin and include it with @include. 2. Pass arguments to make mixins flexible, with support for default values. 3. Use multiple or keyword arguments for complex configurations. 4. Utilize @content to inject dynamic CSS blocks, ideal for responsive design. 5. Follow best practices by using mixins for repeated style patterns, naming them clearly, and reserving functions for calculations, which keeps code DRY and maintainable.
Mixins in SASS are a powerful way to reuse styles across your CSS. They let you define reusable blocks of code, similar to functions, and include them wherever needed. Here's how to use them effectively.

What Is a Mixin?
A mixin is a reusable block of code that can take arguments (optional) and be included in other styles. Think of it like a function for CSS rules.
You define a mixin using the @mixin
directive:

@mixin border-radius { border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; }
Then include it using @include
:
.button { @include border-radius; background-color: #007bff; color: white; padding: 10px 20px; }
This outputs standard CSS with all the prefixed rules applied.

Passing Arguments to Mixins
You can make mixins more flexible by passing arguments.
@mixin border-radius($radius) { border-radius: $radius; -webkit-border-radius: $radius; -moz-border-radius: $radius; }
Now use it with different values:
.card { @include border-radius(10px); } .round-button { @include border-radius(50%); }
You can also set default values:
@mixin border-radius($radius: 5px) { border-radius: $radius; -webkit-border-radius: $radius; -moz-border-radius: $radius; }
Now if you call @include border-radius;
without an argument, it uses 5px
by default.
Advanced: Mixins with Multiple or Keyword Arguments
Mixins support multiple arguments and named (keyword) parameters.
@mixin box-shadow($h-offset, $v-offset, $blur: 4px, $color: #000) { box-shadow: $h-offset $v-offset $blur $color; -webkit-box-shadow: $h-offset $v-offset $blur $color; -moz-box-shadow: $h-offset $v-offset $blur $color; }
Usage:
.panel { @include box-shadow(2px, 4px, 6px, rgba(0,0,0,0.3)); } .tooltip { @include box-shadow(1px, 1px, $color: red); // uses keyword to skip $blur }
This flexibility makes mixins great for complex, configurable styles.
Using @content
for Dynamic Content
One advanced feature is @content
, which lets you pass blocks of CSS into a mixin.
@mixin responsive($breakpoint) { @media (max-width: $breakpoint) { @content; } }
Now you can inject styles:
.container { width: 100%; @include responsive(768px) { padding: 10px; font-size: 14px; } }
This is especially useful for responsive design patterns.
Best Practices
- Use mixins for repeated style patterns (e.g., shadows, flex layouts, resets).
- Avoid overusing them for single-property rules unless they add value (like prefixes).
- Name them clearly and consistently.
- Prefer functions for calculations and mixins for style blocks.
Mixins help keep your SASS DRY (Don’t Repeat Yourself) and maintainable.
Basically, if you find yourself writing the same CSS over and over, wrap it in a mixin. It’s not magic, but it saves time and reduces errors.
The above is the detailed content of How to use mixins in SASS?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The full name of SASS is "Software as a service", which means "software as a service"; it is a software deployment model in which third-party suppliers build applications on cloud infrastructure and provide these to customers through the Internet in the form of subscriptions. applications that do not require customers to build the underlying infrastructure upfront. This means that the software can be accessed on any device with an internet connection and a web browser, unlike traditional software that can only be installed on your local machine.

The sass used by Vue when creating a project is to strengthen the css auxiliary tool and is an extension of css; sass is a css preprocessing language written in the buby language. It has the same strict indentation style as html and is consistent with css writing specifications. Curly braces and semicolons are not used.

Vue is a popular JavaScript framework that allows developers to build high-performance, responsive web applications. In Vue, component properties and methods can be shared using Mixins. Mixins allow developers to reuse and maintain component code, improving code reusability and maintainability. In this article, we will learn how to share component properties and methods in Vue using Mixins. 1. What is MixinsMixins is a way to implement code re-implementation in Vue.

Solution to the Vue project compilation sass error: 1. Use the image source "cnpm install node-sass sass-loader --save-dev" to install sass; 2. Change the "sass-loader" version in "package.json" to " "sass-loader": "^7.3.1","; 3. Use it directly in the page or use @ instead of src.

The differences between Sass and less include syntax differences, definition methods of variables and mixers, import methods, operator support, extensibility, etc. Detailed introduction: 1. Syntax difference. Sass uses indentation to express nested rules, similar to Python syntax. Less uses CSS-like syntax and uses braces to express nested rules; 2. Variables and mixers. Definition method, in Sass, variables are defined using the `$` symbol, while mixers are defined using the `@mixin` keyword, in Less and so on.

How to use SASS styles in Angular projects? The following article will introduce to you how to use SASS styles in Angular. I hope it will be helpful to you!

Vue error: Mixins cannot be used correctly for code reuse, how to solve it? Introduction: In Vue development, we often encounter code reuse. Vue provides the mixins feature to solve this problem. However, sometimes we encounter situations where mixins cannot be used correctly. This article will detail the cause of this problem and provide corresponding solutions. Problem description: When we use mixins, we may encounter the following error message: "TypeError:Cannotr

How to implement customizable front-end styling with React and Sass Introduction: React is a popular JavaScript library for building user interfaces. It provides a component-based approach to developing complex front-end applications. Sass is a CSS preprocessor that makes it easier to manage and organize styles by decomposing CSS code into modules. React combined with Sass can achieve customizable front-end styles. This article will introduce how to use React and Sass together to achieve customizable styles in the project.
