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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to control file extensions with an .htaccess file?MiscellaneousEverything you need to know about ANY, SOME and ALL Operators in PostgresPostgresHow to Sort (Shuffle) an Array in Random Order in JavaScript?JavaScript@forward rule in SCSS (Sass)SCSSHow to progressively load images and add a blurry placeholder?CSSHow to disable scrolling on html body on menu click using JavaScript?JavaScript