Technology News

The Big Gotcha With Custom Properties

I’ve seen this confuse more than a handful of people recently, including myself, so I’m making sure it’s written down.

Let’s chuck a couple of custom properties into CSS:

html { --color-1: red; --color-2: blue;
}

Let’s use them right away to make a background gradient:

html { --color-1: red; --color-2: blue; --bg: linear-gradient(to right, var(--color-1), var(--color-2));
}

Now say there is a couple of divs sitting on the page:

<div></div>
<div class="variation"></div>

Lemme style them up:

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

That totally works! Hell yes!

Now lemme style that variation. I don’t want it to go from red to blue, I want it to go from green to blue. Easy cheesy, I’ll update red to green:

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;
}

Nope! (Sirens blaring, horns honking, farm animals taking cover).

That doesn’t work, friends.

The problem, as best I understand it, is that --bg was never declared on either of the divs. It can use --bg, because it was declared higher up, but by the time it is being used there, the value of it is locked. Just because you change some other property that --bg happens to use at the time it was declared, it doesn’t mean that property goes out searching for places it was used and updating everything that’s used it as a dependency.

Ugh, that explanation doesn’t feel quite right. But it’s the best I got.

The solution? Well, there are a few.

Solution 1: Scope the variable to where you’re using it.

You could do this:

html { --color-1: red; --color-2: blue;
} div { --bg: linear-gradient(to right, var(--color-1), var(--color-2)); background: var(--bg);
}
.variant { --color-1: green;
}

Now that --bg is declared on both divs, the change to the --color-1 dependency does work.

Solution 2: Comma-separate the selector where you set most of the variables.

Say you do the common thing where you set a bunch of variables at the :root. Then you run into this problem. You can just add extra selectors to that main declaration to make sure you hit the right scope.

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;
}

In some other perhaps less-contrived example, it might look something like this:

: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;
}

Solution 3: Blanket Mode

Screw it — put the variables everywhere.

* { --access: me; --whereever: you; --want: to; --hogwild: var(--access) var(--whereever);
}

This is not a good plan. I overheard a chat recently in which a medium-sized site experienced a 500ms page rendering delay because every draw to the page needed to compute all the properties. It “works” but it’s one of the rare cases where you can cause legit performance problems with a selector.

Solution 4: Introduce a new “default” property and fallback

All credit here to Stephen Shaw who’s exploration on all this is one of the places I saw this confusion in the first place.

Let’s go back to our first demonstration of this problem:

html { --color-1: red; --color-2: blue; --bg: linear-gradient(to right, var(--color-1), var(--color-2));
}

What we want to do is give ourselves two things:

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

So we’re gonna do it this way:

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));
}

Notice that we haven’t declared --bg at all. It’s just sitting there waiting for a value, and if it ever gets one, that’s the value that “wins.” But without one, it’ll 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 (so long as I do it on a selector that touches one of the divs).
  2. Or, I can set --bg to reset the entire background to whatever I want.

Feels like a nice way to handle things.


Sometimes there are actual bugs with CSS custom properties. This isn’t one of them. Even though it sort of feels like a bug to me, apparently it’s not. Just one of those things you gotta know about.

12 Ways to Reduce Customer Churn Rate
The FTC has refiled its case against Facebook

Related Articles

The Humble `img` Element And Core Web Vitals

the-humble-img-element-and-core-web-vitals
About The Author Addy Osmani is an engineering manager working on Google Chrome. His team focuses on speed tools and frameworks, helping keep the web fast. Devoted to the ……

WordCamp US 2025: See You in Portland, Oregon!

WordCamp US 2025: See You in Portland, Oregon!
WordCamp US 2025 is heading to vibrant Portland, Oregon, from August 26–29, 2025! Join fellow open source enthusiasts, developers, designers, and WordPress professionals from across the United States and around…

Adding Vite to Your Existing Web App

adding-vite-to-your-existing-web-app
Vite (pronounced “veet”) is a newish JavaScript bundler. It comes batteries-included, requires almost no configuration to be useful, and includes plenty of configuration options. Oh—and it’s fast. Incredibly fast. This…

MOST POWERFUL MacBook vs PC Laptop (RIP Windows?)

MOST POWERFUL MacBook vs PC Laptop (RIP Windows?)

Upgrade your wardrobe with LESTRANGE. Use code CRTECH30 to get $30 off a minimum order of $100: https://bit.ly/3GDJ6lx

Or, shop the look I’m wearing:
⮕ 24 Trouser: https://bit.ly/3RiGnmD
⮕ The Knitted Crew: https://bit.ly/3GF2mzi
⮕ Cuban Collar Shirt: https://bit.ly/3Gzyog1

MacBooks have always been overpriced, slow, and they can’t even get over 95 FPS when gaming on Counterstrike… At least, that is what’s been thrown around internet forums for the better part of the last decade. With Apple’s new M3 Max chips though, are those shiny MacBooks finally able to compete with Intel and Nvidia’s most powerful systems?

Keyboard I use for both Mac and PC (Nuphy Air75 V2):
⮕ From Nuphy’s website (use code CT10): https://crtd.tech/air75v2

Products featured in this video:
⮕ Aluminum Laptop Stand: https://geni.us/mstand
⮕ Dell U2720Q 4K Monitor: https://geni.us/4kdisplay
⮕ Anker USB-C Hub: https://geni.us/ankerhub8in1
⮕ CalDigit Thunderbolt 3 Dock: https://geni.us/tbdock

FREQUENTLY ASKED QUESTIONS FOR THIS VIDEO:
Q: There are cheaper Windows laptops with similar performance to the Legion 9i, why didn’t you use them?
A: Sure, but I only own the Legion 9i, and didn’t want to spend thousands more on a new one lol. The 9i also has a very comparable screen to the M3 Max MacBook Pro (3.2k resolution and Mini-LED screen). Regardless, price is not what I’m comparing in this video. I’m mostly interested in raw performance (but yes, you can get a similarly powerful Windows laptop for 20-40% cheaper than the M3 Max).

Q: During the Resident Evil gaming comparison, why did you enable DLSS on the Legion laptop? Wouldn’t it be fairer to disable this because the Mac does not support it?
A: I think it’s more unfair to intentionally handicap the Legion by disabling DLSS. If you’re playing on an RTX GPU, you are going to enable DLSS. The Mac has no raytracing support yet on games, so this is a realistic representation of real-life differences. There is also no DLSS option for “quality” on the Windows machine either.

Q: You look like an Apple fanboy, therefore all the data in this video is inaccurate/fake/biased.
A: IDGAF, and also feel free to compare the results in this video to other publicly available data/benchmarks/results. I did this before releasing the video to cross-reference my findings. I’m confident this video paints an accurate picture of (most) differences.

Q: Why did you compare Intel 13th generation CPUs instead of the new 2024 Intel Ultra lineup?
A: Pretty simple lol – the Ultra lineup was released about 4 days before I uploaded this video. Also, the high-performance Ultra CPUs have not even been announced, let alone the laptops they will ship in.

Support the channel:
????️ Custom macOS wallpapers: https://crtd.tech/wallpapers
???? Follow me on Instagram: https://www.instagram.com/liamlorin

00:00 Introduction
01:02 A Few Things…
03:05 CPU Performance
04:23 Creative Workflows
05:31 LESTRANGE Clothes
06:58 Video Editing
08:13 3D Workflows
09:44 Gaming
11:55 Fan Noise and Heat
13:07 Efficiency
13:55 Performance on Battery
14:17 The Future

#m3max #intel #gaminglaptop
—
Disclaimer: Some of the above links are affiliated, and we may receive a small commission (at no cost to you) if you make a purchase. As an Amazon Associate we earn from qualifying purchases.

America’s Secret Database

America's Secret Database
FISA is a sweeping surveillance authority that allows the warrantless surveillance of Americans in the name of national security. It was originally created to stop intelligence agencies from spying on…