Example of how to modify styles via CSS variables

Example of how to modify styles via CSS variables

question

How to modify CSS pseudo-class style with js? But js does not have a pseudo-class selector, so what should we do? There are many methods on the Internet, such as switching element classes, dynamically inserting new styles in style, etc.

Then here is another method, which is to set the CSS variable (var) and change this variable through JS to achieve it.

Example: Change the hover background color of a div

<!-- css -->
<style type="text/css">
    :root {
        --divHoverColor: red;
    }
    div {
        width: 100px;
        height: 100px;
        background: bisque;
    }
    div:hover {
        background: var(--divHoverColor);
    }
</style>
<!-- html -->
<div onClick="onDivClick('green')"/>
<!-- js -->
<script type="text/javascript">
    function onDivClick(value){
        document.documentElement.style.setProperty('--divHoverColor', value);
    }
</script>

So, let’s get to know CSS variables.

1. Basic usage

Local variables

div {
    --masterColor: red;
    background: var(--masterColor);
}

Global variables

/* For HTML, :root refers to the <html> element*/
:root {
    --masterColor: red;
}
div {
    background: var(--masterColor);
}

2. Syntax

var( <custom-property-name> [, <declaration-value> ]? )

<custom-property-name> : custom property name

<declaration-value> : declaration value (fallback value)

Example:

div {
    /*
    --masterColor is undefined, so the background color is red 
    It can define multiple declaration values ​​separated by commas, var(--masterColor, red, green)
    */
    background: var(--masterColor, red);
}

Variables can be referenced using var()

Example:

div {
    --masterColor: red;
    --bgColor:var(--masterColor)
}

Note: Variables cannot be used in attribute names.

Error example:

div {
    --bg: background;
    var(--bg): red;
}

3. Browser Support

Can I use

Using @support detection

@supports ( (--masterColor: red)) {
  /* supported */
}
@supports ( not (--masterColor: red)) {
  /* not supported */
}

Using JS detection

const isSupported = window.CSS && window.CSS.supports 
    && window.CSS.supports('--masterColor', 'red');

if (isSupported) {
  /* supported */
} else {
  /* not supported */
}

refer to

MDN
CSS Variable Specification

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.

<<:  HTML&CSS&JS compatibility tree (IE, Firefox, Chrome)

>>:  MySQL advanced learning index advantages and disadvantages and rules of use

Recommend

Six methods for nginx optimization

1. Optimize Nginx concurrency [root@proxy ~]# ab ...

Summary of new usage examples of computed in Vue3

The use of computed in vue3. Since vue3 is compat...

How to reset the root password in Linux mysql-5.6

1. Check whether the MySQL service is started. If...

Class in front-end JavaScript

Table of contents 1. Class 1.1 constructor() 1.2 ...

Mysql cannot select non-aggregate columns

1. Introduction I recently upgraded my blog and a...

CSS HACK for IE6/IE7/IE8/IE9/FF (summary)

Since I installed the official version of IE8.0, ...

Drawing fireworks effect of 2021 based on JS with source code download

This work uses the knowledge of front-end develop...

JS realizes the effect of Baidu News navigation bar

This article shares the specific code of JS to ac...

Tips for Mixing OR and AND in SQL Statements

Today, there is such a requirement. If the logged...

5 solutions to CSS box collapse

First, what is box collapse? Elements that should...

Vue implements a simple timer component

When doing a project, it is inevitable to encount...

Summary of Docker common commands and tips

Installation Script Ubuntu / CentOS There seems t...

Detailed explanation of the spacing problem between img tags

IMG tag basic analysis In HTML5, the img tag has ...