How to use default values ​​for variables in SASS

How to use default values ​​for variables in SASS

Variables defined in SASS, the value set later will overwrite the old value.

$color: red;
$color: blue;

.btn {
    color: $color;
}

After compilation:

.btn {
  color: blue; }

If you write a UI library that provides SASS files, you may provide some parameters for users to customize when using it. Inside the SASS component, we need to apply these values ​​set by the user. But if the user does not customize the value of the variable, then these variables should have their own default values.

This cannot be achieved using the coverage mechanism mentioned above. Because no matter you set it before or after @importing the UI library, you cannot affect the value in this imported file. If you set the value before importing, the variables in the UI library will be overwritten and will not work. If your setting is after importing, it will be even more ineffective.

Assume this is the style file in the UI:

_lib.scss

$color: red;
.btn {
    color: $color;
}

Use in another file and try to customize the value of the variable:

page.scss

@import 'lib';
$color: blue;

or:

page.scss

$color: blue;
@import 'lib';

The compilation results of both are:

.btn {
  color: red; }

!default

For this situation, SASS provides the !default flag. Applying this identifier after a variable value means that if the variable is not defined elsewhere or even if it is defined but the value is null, then the default value set here will take effect, otherwise the value set elsewhere will be used.

Transform the above _lib.scss:

_lib.scss

- $color: red;
+ $color: red!default;
.btn {
    color: $color;
}

use:

$color: blue;

@import "lib";

Note: The custom value needs to be placed before @import, otherwise it will not take effect.

At this point the compilation result will be what you want, with the external custom variable values ​​applied.

.btn {
  color: blue; }

Related resources

  • SASS Documentation - Default Values
  • A Sass !default use case

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Solve the problem of docker pull image error

>>:  What the website needs most is to improve the experience of the target user group

Recommend

How to modify the group to which a user belongs in Linux

Modify the group to which a user belongs in Linux...

Use of Linux sed command

1. Function Introduction sed (Stream EDitor) is a...

CSS3 uses animation attributes to achieve cool effects (recommended)

animation-name animation name, can have multiple ...

Detailed explanation of mysql backup and recovery

Preface: The previous articles introduced the usa...

What kinds of MYSQL connection queries do you know?

Preface If the query information comes from multi...

Use of MySQL trigger

Table of contents 1. Trigger Introduction 1. What...

Detailed analysis of the MySQL slow log opening method and storage format

In development projects, we can monitor SQL with ...

In-depth understanding of uid and gid in docker containers

By default, processes in the container run with r...

How to select all child elements and add styles to them in CSS

method: Take less in the actual project as an exa...

Analysis of the usage of Xmeter API interface testing tool

XMeter API provides a one-stop online interface t...

How to make spaces have the same width in IE and FF?

body{font-size:12px; font-family:"宋体";}...

Problems and experiences encountered in web development

<br />The following are the problems I encou...