• About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us
AimactGrow
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
AimactGrow
No Result
View All Result

Utilizing light-dark() with out getting caught out

Admin by Admin
November 27, 2026
Home Coding
Share on FacebookShare on Twitter


I truthfully hadn’t considered “getting caught” by light-dark() — notably when leaning closely on it for this website — however Martin Underhill explains:

With out fallbacks, browsers that don’t help light-dark()usually use black textual content on a white background in Mild Mode, and white textual content on a darkish gray background in Darkish Mode. They’re not the precise colors I’d need, however my web site seems to be effective.

Once more, it’s dangerous, so check totally.

Like Martin, I initially started implementing shade themes based mostly on media queries sniffing out person preferences. However it grew to become tremendous cumbersome to jot down prefers-color-scheme repeatedly. I imply, completely DX-world issues. I additionally remembered the piece Sara Pleasure wrote with us some time again encouraging a soar into the light-dark() waters and thought it’d be good to take that recommendation.

Doing that, I bumped into one other DX annoyance: writing two shade values for every light-dark(). CSS variables assist, in fact, however even then it exhausted me. That claims one thing extra about me than the function, however I digress. So, I took Martin’s course of one step additional to summary all of the issues:

:root {
  --gray-1: #eaeaea; /* Mild */
  /* ... */
  --gray-10: #111; /* Darkish */

  --text: light-dark(var(--gray-1), var(--gray-10));
}

.article-content {
  shade: var(--text);
}

Very good, a minimum of for my penchant for laziness. However I bumped into one other difficulty as a result of typically textual content must go the opposite course. Maintain abstracting. I might outline the background variable which is often the distinction to textual content:

:root {
  --gray-1: #eaeaea; /* Mild */
  /* ... */
  --gray-10: #111; /* Darkish */

  --text: light-dark(var(--gray-1), var(--gray-10));
  --surface: light-dark(var(--gray-10), var(--gray-1));
}

.widget-content {
  shade: var(--surface);
}

Works, however utilizing a helper referred to as “floor” on a property controlling shade bothers me too. I could also be lazy, however I’m just a little anal with semantics. Therefore:

:root {
  --gray-1: #eaeaea; /* Mild */
  /* ... */
  --gray-10: #111; /* Darkish */

  --text: light-dark(var(--gray-1), var(--gray-10));
  --text-reverse: light-dark(var(--gray-10), var(--gray-1));
}

.article-content {
  shade: var(--text-reverse); /* Mild then darkish */
}

.widget-content {
  shade: var(--text-reverse); /* Darkish then mild */
}

…the place “reverse” means it goes the other way of a traditional “light-dark” name and the “darkish” worth known as first. However even that’s nonetheless an excessive amount of for my mind to deal with as a result of now I’m left with a number of tokens to recollect if I would like issues to be tremendous semantic:

:root {
  --gray-1: #eaeaea; /* Mild */
  /* ... */
  --gray-10: #111; /* Darkish */

  --text: light-dark(var(--gray-1), var(--gray-10));
  --text-reverse: light-dark(var(--gray-10), var(--gray-1));

  --surface: light-dark(var(--gray-1), var(--gray-10));
  --surface-reverse: light-dark(var(--gray-10), var(--gray-1));
}

.article-content {
  background-color: var(--surface); /* Darkish then mild */
  shade: var(--text-reverse); /* Mild then darkish */
}

.widget-content {
  background-color: var(--surface-reverse); /* Mild then darkish */
  shade: var(--text-reverse); /* Darkish then mild */
}

So I went with an much more normal abstraction:

:root {
  --gray-1: #eaeaea; /* Mild */
  /* ... */
  --gray-10: #111; /* Darkish */

  --light-dark: light-dark(var(--gray-1), var(--gray-10));
  --dark-light: light-dark(var(--gray-10), var(--gray-1));
}

.article-content {
  background-color: var(--dark-light); /* Darkish then mild */
  shade: var(--light-dark); /* Mild then darkish */
}

.widget-content {
  background-color: var(--dark-light); /* Darkish then mild */
  shade: var(--light-dark); /* Mild then darkish */
}

Good, no. However I’m fairly proud of it.

Again to Martin and fallbacks, he goes about it with a basic cascade safeguard:

shade: black;
shade: light-dark(black, white);

background-color: white;
background-color: light-dark(white, black);

He’s completely proper and I must be doing that as nicely. Possibly I’ll, however I couldn’t assist considering {that a} light-dark() fallback argument can be good. Spitballing right here, in fact:

shade: light-dark(var(--dark-light), var(--light-dark) / var(--dark-light));

Now we’re getting verbose. However so are fallbacks basically. Certainly, Martin requested if shifting to light-dark() is even price it given the necessity to consider fallbacks. Is it actually extra environment friendly than a media question at this level? His byte-savings had been negligible. I made a decision to check what the variations would have been on CSS-Tips:


light-dark()
 perform
prefers-color-scheme media question Distinction
Minified 169,948 B 182,210 B +12,262 B (+7.2%)
Gzip 30,215 B 31,422 B +1,207 B (+4.0%)
Brotli 24,933 B 25,270 B +337 B (+1.4%)

Not unhealthy, really. Not an earth shattering distinction nevertheless it does assist me justify favoring DX even when I used to be simply fortunate it labored out that method.


Direct Hyperlink →

Tags: caughtlightdark
Admin

Admin

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

Is Gears Of Battle: E-Day On Steam?

Is Gears Of Battle: E-Day On Steam?

September 28, 2026
Meta Unveils Lab for Superintelligent AI

Meta Unveils Lab for Superintelligent AI

July 21, 2025

Trending.

HunyuanCustom Brings Single-Picture Video Deepfakes, With Audio and Lip Sync

HunyuanCustom Brings Single-Picture Video Deepfakes, With Audio and Lip Sync

May 8, 2025
AI & data-driven Starbucks – Deep Brew

AI & data-driven Starbucks – Deep Brew

May 18, 2026
A SQL MERGE assertion performs actions primarily based on a RIGHT JOIN

Use jOOQ’s code generator to simply go table-valued parameters

April 26, 2025
A SQL MERGE assertion performs actions primarily based on a RIGHT JOIN

Implicit path joins could now skip pointless tables within the be a part of tree

April 11, 2025
DDL, DML, DQL, DCL, and TCL – SitePoint

DDL, DML, DQL, DCL, and TCL – SitePoint

May 13, 2025

AimactGrow

Welcome to AimactGrow, your ultimate source for all things technology! Our mission is to provide insightful, up-to-date content on the latest advancements in technology, coding, gaming, digital marketing, SEO, cybersecurity, and artificial intelligence (AI).

Categories

  • AI
  • Coding
  • Cybersecurity
  • Digital marketing
  • Gaming
  • SEO
  • Technology

Recent News

Utilizing light-dark() with out getting caught out

Utilizing light-dark() with out getting caught out

November 27, 2026
This Moveable Spot Cleaner Is An Glorious Reasonably priced Different to the Shark StainForce on Black Friday

This Moveable Spot Cleaner Is An Glorious Reasonably priced Different to the Shark StainForce on Black Friday

November 27, 2026
  • About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved

No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved