Yogesh Chauhan's Blog

@mixin and @include in SCSS (Sass)

in SCSS on April 23, 2021

@mixin is a wonderful rule SassScript has. It allows you to write style rules that can be used throughout your stylesheet — basically it helps to create reusable styles.

@mixin helps to create reusable styles and to avoid using non-semantic classes.

Non-semantic classes are the classes that don’t give you an idea of what the particular element represents. They might give an impression of how the element looks like. Some of the examples of non-semantic classes are .green, .float-left, .grid etc.

@mixin also helps in distributing collections of styles in libraries which is another advantage of using it.

How to define @mixin rules?

Using @mixin at-rule we can define @mixin rules.

It basically like JavaScript named or anonymous function declaration.

You can either use @mixin { … } or @mixin name() { … }.

For the @mixin name, you can use Sass identifier. It can contain any kind of statements except the top-level statements.

@mixin can also be used to encapsulate styles which then can be dropped into a single style rule. You can add styles inside @mixin and even nest the style inside other @mixin rules. You can also use @mixin to modify the declared variables.

That’s where @include comes into play. To include the @mixin rules.

@mixin and @include Example

In the following example, we are using @include to include @mixin rules twice and one of them is nested as well.

@mixin rule names treat underscore and hyphens as same. So, reset-list and reset_list both refer to the same @mixin rule.

Source: Sass Docs


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
HTML canvas methodsHTMLIs there a difference between SCSS and Sass?SCSSSlider animation using CSS transform propertyCSSHow to add AppRoutingModule in Angular application using command line?AngularHow to use PHPMailer to send an Email via Gmail SMTP Server?PHPLearn to Implement Estimated Reading Time using PHP Part 1: The BasicsPHP