惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
The Cloudflare Blog
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
Jina AI
Jina AI
V
V2EX
罗磊的独立博客
V
Visual Studio Blog
A
About on SuperTechFans
IT之家
IT之家
P
Proofpoint News Feed
B
Blog
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog

Starred Articles

Why some people mow a lawn better than others Improvements to Web for AI Should Benefit All Users chriscoyier.net When Sites Need to Walk Away AI & Alignment New to the web platform in April  |  Blog  |  web.dev Delivering a dynamic hexagonal world map in just 10kb | Calibre My house alerts me when my cat climbs into the ceiling Eames Pavilion System Squarespace & Web Standards: How We Helped Bring HTML Video & Audio Lazy Loading to Today’s Browsers | Scott Jehl, Web Designer/Developer The Web is a Guitar Amp Now (Literally) Claude is an Electron App Making keyboard navigation effortless Miscalibrated I used Claude Code and GSD to build the accessibility tool I’ve always wanted - blakewatson.com Sizing chaos The Good & Not Good Scroll indicators on tables with background colours using animation-timeline Webspace Invaders Fresh Hot CSS: Trig Functions The Year Of The Linux Desktop (for fitness games) Almost Plain Text, Nicely Done – Email is good. Uncrate's 100-ish favorite things on Amazon VS Code – highlight just the active indent guide How musicals use motifs to tell stories Good Tidings! Review: MoErgo Go60, a split ergonomic and fully programmable keyboard The line and the stream. — Ethan Marcotte Testing HTML Light DOM Web Components: Easier Than Expected! Enhancing Web Components Safely with Self-Destructing CSS | Scott Jehl, Web Designer/Developer
A custom --light-dark() function in CSS that works with a...
Published by Bramus! · 2025-10-01 · via Starred Articles

In CSS @function + CSS if() = 🤯 I explored combining CSS custom functions and CSS if(), leading to the creation of a custom --light-dark() CSS function that works with any value (not just colors!).

The built function relied on a style query that query a custom property --scheme, which itself was set to mirror the value of the color-scheme property.

Thanks to the recently resolved color-scheme() function, this indirection is no longer needed, and the code can become much, much simpler.

~

⚠️ This post is about an upcoming CSS feature. You can’t use it … yet.

While custom functions with @function and CSS if() are available in Chrome (and only Chrome, for now), the mentioned color-scheme() only exists on paper right now.

~

# The Code

If you are here just for the code, here it is:

@function --light-dark(--l, --d) {
  result: if(color-scheme(dark): var(--d); else: var(--l));
}

Usage is similar to the built-in light-dark(), but difference is that it can be used with any type of value:

#element {
  color-scheme: light dark;
  color: light-dark(#333, #e4e4e4);
  background-image: --light-dark(url(light.svg), url(dark.svg));
}

To reiterate, you can’t use this code yet, because no browser supports everything that is needed. In Chrome, only schemed-color() support is missing. Thankfully, you can work around this lack of support by storing the color-scheme into a custom property, and querying that with a style query. See this post to get the code that works (in Chrome).

~

# The color-scheme() function

Powering this custom --light-dark() function is the new color-scheme() function. It’s a new addition to CSS which we only recently resolved on adding with the CSS Working Group.

The color-scheme() function allows you to query the used color scheme of an element. The function can be used in both @container queries and the new if()].

In the following example I am using the function directly on an element (instead of placing it into a custom function):

#element {
  color-scheme: light dark;
  background-image: if(color-scheme(dark): url(dark.svg); else: url(light.svg));
}

The reason we need this new color-scheme() function is because the color-scheme property only lists which color schemes are supported. When set to light dark, you are indicating the element can adapt its rendering to either a light or dark version. This is controlled by your light/dark setting, with the resulting used value being one of those listed values.

FYI: You can also force a component into a specific mode by listing only 1 value for color-scheme, e.g. color-scheme: light will force the component into light mode, regardless of your OS setting.

~

# Spread the word

Feel free to reshare one of the following posts on social media to help spread the word:

~

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)