Yogesh Chauhan's Blog

Check if mixin is being called in a style rule or not in SCSS

in SCSS on April 26, 2021

We can use the parent selector to do that.

The parent selector (&) will return null if we use it outside of any style rules. The null is nothing but a false or a falsey. We can check that using SCSS if statement and check if mixin is being called in a style rule or not.

Here’s how to do that:

Let’s understand what happens in the example above.

We know how @mixin and @include with parameters work.

@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.

Everything work the same way — passing an argument and applying the style. The only difference is that the if statement checks if there is a parent selector or not. It returns one of the two classes based on that check.

The SCSS code above compiled into this CSS code:

.app-background {
  background-color: #036;
  color: rgba(255, 255, 255, 0.75);

.sidebar.app-background {
  background-color: #c6538c;
  color: rgba(255, 255, 255, 0.75);


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
How to convert an object from API to JSON array in Angular 9?AngularNumber Properties in JavaScriptJavaScriptIntroduction to components and templates Part 1: Component MetadataAngularWhat Is a Graph Database?MiscellaneousWP_Query Class in WordPressWordPresstransform-origin Property in CSSCSSHow to get Current Hour, Minute and Second in JavaScript?JavaScriptSimple Page Hit Counter in PHPPHPHow to display and animate image on scroll using JavaScript?JavaScriptHow to create a flip effect with CSS?CSSHow to change HTML content on click using JavaScript?JavaScriptHow to list all PHP variables to debug the script?PHP