Table of Contents
Solution 1: Limit the variable scope to where it is used.
Solution 2: Set selectors for most variables with comma separated.
Solution 3: Comprehensive Mode
Solution 4: Introduce new "default" properties and fallback
Home Web Front-end CSS Tutorial The Big Gotcha With Custom Properties

The Big Gotcha With Custom Properties

Mar 20, 2025 am 09:27 AM

The Big Gotcha With Custom Properties

Recently I found that many people (including myself) are confused by this feature of CSS custom attributes, so I decided to record it.

Let's add a few custom properties in CSS:

1

2

3

4

html {

  --color-1: red;

  --color-2: blue;

}

Copy after login

We use them immediately to create a background gradient:

1

2

3

4

5

6

html {

  --color-1: red;

  --color-2: blue;

 

  --bg: linear-gradient(to right, var(--color-1), var(--color-2));

}

Copy after login
Copy after login

Now suppose there are two divs on the page:

1

2

<div></div>

<div></div>

Copy after login

Let's style them:

1

2

3

div {

  background: var(--bg);

}

Copy after login

This works completely! marvelous!

Now let's modify the style. I don't want it to change from red to blue, I want it to change from green to blue. It's very simple, I updated the red to green:

1

2

3

4

5

6

7

8

9

10

11

12

html {

  --color-1: red;

  --color-2: blue;

 

  --bg: linear-gradient(to right, var(--color-1), var(--color-2));

}

div {

  background: var(--bg);

}

.variation {

  --color-1: green;

}

Copy after login

invalid! (The sirens are loud, the horns are loud, and domestic animals are hiding everywhere).

Friends, this doesn't work.

As far as I understand, the problem is that --bg has never been declared on any div. It can use --bg because it is declared at a higher level, but when it is used, its value has been locked. Just because you changed the other attributes used when --bg declaration does not mean that the attribute will look for all the locations where it is used as a dependency and update everything that uses it.

Alas, this explanation doesn't feel right. But this is the best explanation I can think of.

Solution? Well, there are a few.

Solution 1: Limit the variable scope to where it is used.

You can do this:

1

2

3

4

5

6

7

8

9

10

11

12

html {

  --color-1: red;

  --color-2: blue;

}

 

div {

  --bg: linear-gradient(to right, var(--color-1), var(--color-2));

  background: var(--bg);

}

.variation {

  --color-1: green;

}

Copy after login

Now --bg is declared on both divs, and changes to --color-1 dependencies do work.

Solution 2: Set selectors for most variables with comma separated.

Suppose you did the common operations of setting a bunch of variables in :root . Then you run into this problem. You can simply add extra selectors to the main declaration to make sure you hit the correct range.

1

2

3

4

5

6

7

8

9

10

11

12

13

html,

div {

  --color-1: red;

  --color-2: blue;

 

  --bg: linear-gradient(to right, var(--color-1), var(--color-2));

}

div {

  background: var(--bg);

}

.variation {

  --color-1: green;

}

Copy after login

In some other examples that may not be too far-fetched, it might look like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

:root,

.button,

.whatever-it-is-a-bandaid {

  --padding-inline: 1rem;

  --padding-block: 1rem;

  --padding: var(--padding-block) var(--padding-inline);

}

 

.button {

  padding: var(--padding);

}

.button.less-wide {

  --padding-inline: 0.5rem;

}

Copy after login

Solution 3: Comprehensive Mode

Go to his - put variables everywhere.

1

2

3

4

5

6

7

* {

  --access: me;

  --whereever: you;

  --want: to;

 

  --hogwild: var(--access) var(--whereever);

}

Copy after login

This is not a good solution. I've heard a chat recently where one of the medium-sized sites was delayed by 500ms due to page rendering, because every draw of the page requires all attributes to be calculated. It "works", but this is one of the few examples where you can cause legitimate performance issues through selectors.

Solution 4: Introduce new "default" properties and fallback

All the credit here is credited to Stephen Shaw, who I saw first in his exploration of all of this content.

Let's go back to where we first demonstrated this question:

1

2

3

4

5

6

html {

  --color-1: red;

  --color-2: blue;

 

  --bg: linear-gradient(to right, var(--color-1), var(--color-2));

}

Copy after login
Copy after login

What we have to do is give ourselves two things:

  1. A way to cover the entire background
  2. A way to cover part of a gradient background

So we have to do this:

1

2

3

4

5

6

7

8

html {

  --color-1: red;

  --color-2: blue;

}

div {

  --bg-default: linear-gradient(to right, var(--color-1), var(--color-2));

  background: var(--bg, var(--bg-default));

}

Copy after login

Note that we do not declare --bg at all. It just sits there waiting for a value, and if it gets a value, it is the value of "winning". But if not, it will fall back to our --bg-default . Now……

  1. If I set --color-1 or --color-2 , it replaces that part of the gradient as expected (as long as I do this on a selector that touches one of the divs).
  2. Alternatively, I can set --bg to reset the entire background to whatever I want.

It feels like a good way to deal with it.

Sometimes there is indeed an error in CSS custom properties. But that's not one of them. Even if it's a bit like a bug to me, obviously it's not. Just one of those things you have to know.

The above is the detailed content of The Big Gotcha With Custom Properties. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn&#039;t have to be the case. Let’s look at how PHP projects can enforce a basic

Programming Sass to Create Accessible Color Combinations Programming Sass to Create Accessible Color Combinations Apr 09, 2025 am 11:30 AM

We are always looking to make the web more accessible. Color contrast is just math, so Sass can help cover edge cases that designers might have missed.

See all articles