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 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
A list of wp-cli commands to use via SSHWordPressHow to uninstall Cocoapods from the Mac OS?Miscellaneous$this Keyword in PHPPHPfirst-of-type and last-of-type selectors in CSSCSSHow to change HTML content on click using JavaScript?JavaScriptHow to add a Bar Chart in Angular App?Angular