YogeshChauhan . com

@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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

What does it mean by Continuous Integration, Continuous Delivery and Continuous Deployment?MiscIntroduction to Angular modules Part 1: NgModule metadataAngularIN Operator in PostgreSQLPostgres4 ways to create Date Objects in JavaScriptJavaScriptFull and Partial ROLLUP in Postgresql with ExamplesPostgresThe 7 Security Objectives of Any Organization for IT and Network SecurityMisc