Yogesh Chauhan's Blog

Default Values in SCSS (Sass)

in SCSS on May 9, 2021

When you define a variable in SCSS (Sass) and change the value of it, the value gets overwritten.

If you want to set the variables value dynamically using an user input, there is a way.

You can use !default flag to do so.

The !default flag only assigns a value to a variable if the variable is not defined or the value for the variable is null.

It is basically like adding an if-else statement but in a really short and quick way.

You can configure variables with !default flag when you load a module using @use rule.

To do so, write @use with (: , : ). The new values in configuration will override the default values.

You need to write those variables with !default tag at the top of the stylesheet. Only those variables will be configured.

Example


// _alert.scss
$border-radius: 10px !default;

.alert {
  border-radius: $border-radius;
}


// style.scss
@use 'alert' with (
  $border-radius: 15px
);

Compiled CSS


.alert {
  border-radius: 15px;
}


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
How to create a cross-browser smooth scrolling with jQuery?jQueryHow to Skip or Exclude a Specific URL or File From .htaccess Rewrite Rule?PHPSorting Arrays in JavaScriptJavaScriptHow to get front page or home page ID in WordPress?WordPressA Step by Step Guide to Make RSS in XML For Any Website or Blog For FreeMiscellaneousHow to show widgets on the Appearance tab in WordPress?WordPressAmpersand (Parent Selector) in SCSS (Sass)SCSSSolution for Xcode 11 Command PhaseScriptExecution failed with a nonzero exit code errorMiscellaneousHow to create Glowing Texts using CSS?CSSOOP, Class and Objects Strategies For Beginners (PHP)PHPWordPress: How to print ACF array field values?WordPressIf statement shorthand examples in JavaScriptJavaScript