Detailed explanation of using CSS3's var() to change scss variable values ​​at runtime

Detailed explanation of using CSS3's var() to change scss variable values ​​at runtime

var() Introduction and Usage

Details (MDN) IE is invalid, other mainstream browsers are valid

var() Usage

Can only be declared in {}, the scope is determined by the selector of {}

<!-- Disclaimer-->
body{
  --name:value; //Valid in body}


<!-- Usage -->
.test{
  attr: var(--name,defaultValue) //When --name does not exist, use defaultValue
  
  var(--name):#369;//Wrong usage}

The native variable definition syntax in CSS is: –*, and the variable usage syntax is: var(–*), where * represents our variable name.

However, it cannot contain characters such as $, [, ^, (, %, etc. Ordinary characters are limited to the combination of "numbers [0-9]", "letters [a-zA-Z]", "underscore _", and "hyphen -", but it can be Chinese, Japanese, or Korean.

Changing scss variable values ​​at runtime

This method does not directly change the value of the scsss variable, but it can achieve the same effect. It is more effective and concise for a variable to control multiple attributes. It is unnecessary to use a single variable to control a single attribute. This method is just to modify the style attribute. One-to-one is the same as writing the attribute in the style.

Principle(English)

Simply put, the scss variables are controlled by css variables

$colors: (
  primary: #FFBB00,
  secondary: #0969A2
);

Selector1{
  @each $name, $color in $colors {
    --color-#{$name}: $color;
  }
}

<!-- Generation effect of Selector1-->
:root {
  --color-primary: #FFBB00;
  --color-secondary: #0969A2;
}


<!-- Usage method 1: directly use css variables -->
Selector{
  color:var(--color-primary);
}

<!-- Usage 2 uses scss functions to comply with scss syntax recommendations -->  
@function color($color-name) {
  @return var(--color-#{$color-name});
}

body { 
  color: color(primary); //Use}

<!-- body generation effect -->
body { 
  color: var(--color-primary); //This can be set by js}

js sets css variables, that is, sets and runs scss variables

domObject.style.setProperty(name,value); //name is the CSS variable name eg: --color-primary

So far, the runtime change of variable values ​​in scss has been completed. I am not sure about the specific application scenario, but I encountered it.

My application scenario:

Custom components need to expose some style properties for users to adjust freely, similar to themes, but I don’t want to use string concatenation to complete it, which is too wasteful. Every time a value is changed, the entire style must be rewritten, and this involves frequent modifications to the DOM, which is not in line with the idea of ​​Vue, and it is too cumbersome to write directly in CSS.

So I use scss to write styles. SCSS nesting is really easy to use. LESS does not support nested attributes, which is very uncomfortable to use and not as concise as scss.
Since scss is precompiled, the variable value cannot be changed at runtime, and I need to change it, so I went to Google and got a satisfactory solution-> Principle (English)

Special attention

If you use scoped in a single file component (.vue), the effects of selectors such as :root and :body will not be as you expect.

[data-v-1374924e]:root {
  --test:100px;
}

In this case, the variable -test cannot be found at all because there is no root. The scoped feature of the Vue component is only valid in this component, but the component does not have a complete document, that is, there is no root inside the component.

So in the vue file, carefully consider the selector scope of the CSS variable declaration to avoid invalid variables

This concludes this article on how to use CSS3's var() to change scss variable values ​​at runtime. For more information about how to change scss variable values ​​with CSS3, please search 123WORDPRESS.COM's previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  How to quickly return to the top from the bottom when there is too much content on the web page

>>:  How to use CURRENT_TIMESTAMP in MySQL

Recommend

MySQL character set viewing and modification tutorial

1. Check the character set 1. Check the MYSQL dat...

Detailed explanation of JavaScript state container Redux

Table of contents 1. Why Redux 2. Redux Data flow...

How to view nginx configuration file path and resource file path

View the nginx configuration file path Through ng...

Detailed basic operations on data tables in MySQL database

Table of contents 1. View the tables in the curre...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

Detailed explanation of Vue's calculated properties

1. What is a calculated attribute? In plain words...

A quick guide to Docker

Docker provides a way to automatically deploy sof...

Differences between ES6 inheritance and ES5 inheritance in js

Table of contents Inheritance ES5 prototype inher...

Ubuntu terminal multi-window split screen Terminator

1. Installation The biggest feature of Terminator...

Interpretation of syslogd and syslog.conf files under Linux

1: Introduction to syslog.conf For different type...

Detailed explanation of mandatory and implicit conversion of types in JavaScript

Table of contents 1. Implicit conversion Conversi...

Realize three-level linkage of year, month and day based on JavaScript

This article shares the specific code for JavaScr...

MySQL 8.0.13 installation and configuration method graphic tutorial

This article shares the installation and configur...

Three BOM objects in JavaScript

Table of contents 1. Location Object 1. URL 2. Pr...

Comprehensive understanding of Node event loop

Table of contents Node Event Loop Event loop diag...