TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to have the browser pick a contrasting color in CSS

255 pointsby Kerrick2 days ago

20 comments

seanwilson1 day ago
For this problem, I&#x27;m working on a tool to help create palettes where color pairs have simple and predictable WCAG&#x2F;ACPA contrast by design (it has more features on desktop):<p><a href="https:&#x2F;&#x2F;www.inclusivecolors.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.inclusivecolors.com&#x2F;</a><p>So one approach is you create swatches of different colors that go from grade 100 (light) to grade 900 (dark), where the lightnesses are chosen such that all grade 700 colors contrast against grade 100 colors, all grade 800 colors contrast against grade 200 etc.<p>And then you know red-700 vs gray-100, green-800 vs yellow-200 and so on will contrast without having to check.<p>If you go to the Contrast menu, you can also explore how much stricter the APCA algorithm (meant to be more accurate) is compared to WCAG. For dark on light colors especially, APCA is much stricter about what contrast so you really shouldn&#x27;t use WCAG for dark themes.<p>Also, if you go to the Examples menu and check out the Tailwind and IBM Carbon color palettes, you can see how the swatches in hand designed palettes vary their saturation and hue across grades in a non-linear way. So automatically picking if white&#x2F;black contrasts the best is more straightforward (like the article mentions), but for more deliberate&#x2F;branded palettes, you can&#x27;t just generate a color with a simple lightness component shift, so this is more open ended.
qfr2 days ago
there is a way to do something close to this using lch:<p><pre><code> --text: lch(from var(--bg) calc((49.44 - l) * infinity) 0 0); </code></pre> source: <a href="https:&#x2F;&#x2F;til.jakelazaroff.com&#x2F;css&#x2F;swap-between-black-and-white-text-based-on-background-color&#x2F;" rel="nofollow">https:&#x2F;&#x2F;til.jakelazaroff.com&#x2F;css&#x2F;swap-between-black-and-whit...</a>
评论 #44020324 未加载
评论 #44016837 未加载
评论 #44016987 未加载
jjcm1 day ago
This is a great overview of the pros&#x2F;cons of this. For those creating just a simple site, this is a solid easy way to have proper contrast.<p>For those making anything at a production scale where you need wcag compliance however, I&#x27;d avoid this and leverage a proper semantic token layer. Semantic tokens will help both accelerate your dev cycle, and they&#x27;ll help guarantee proper contrast ratios in a way that looks visually better than just switching your foreground layer to black or white. The great thing about a semantic token layer is they&#x27;re extremely easy to theme, which means you get light&#x2F;dark theming for very little additional cost. You can also create separate WCAG2 &#x2F; APCA accessible themes, should your brand color be one of the ones that WCAG2 has issues with - will get you compliance while still providing a better visual contrast option.<p>This is kind of my niche domain specialty - I run the variables&#x2F;tokens stream at Figma, and I&#x27;ve worked on the dark mode implentation for both Figma and Atlassian. Happy to answer any questions about tokens&#x2F;themes&#x2F;accessible color.
评论 #44017429 未加载
评论 #44018266 未加载
politelemon2 days ago
I&#x27;m still not convinced that the contrasting colour should be the browser vendor&#x27;s decision, it won&#x27;t always be right or predictable. Will this be a definitive deterministic standard across all browsers? Instead this function feels like a tool to help UX teams during design phase.
评论 #44016314 未加载
评论 #44016897 未加载
评论 #44016192 未加载
mediumsmart2 days ago
&gt;But, on a large project, with a large team, carefully managing such details can become a really hard task to get right. Suddenly a dark button has unreadable black text, and users can’t figure out what to do.<p><i>Cant someone take a look at the buttons before the large project ships? Alternatively make it mandatory to never have black text on a dark button and tell every team member including the large ones.</i><p>Interesting to read about the perceptual contrast vs mathematical - I did not know that. Going to integrate that into my workflow.
评论 #44022258 未加载
评论 #44016117 未加载
econ1 day ago
Back when systeem colors were actually cool I made some system color styles. It looked really nice but you don&#x27;t know how they contrast. That one is called [say] buttonFace and another buttonText turned out to to be meaningless. Someone wrote some js for me that took getComputedStyle and calculated the contrast. If it was unacceptable it either took a second candidate color or failed back on text-shadow to darken or lighten an aura around the text sufficiently.<p><a href="https:&#x2F;&#x2F;i.sstatic.net&#x2F;18bQt.png" rel="nofollow">https:&#x2F;&#x2F;i.sstatic.net&#x2F;18bQt.png</a><p>I forget the calculation but thinking about it you can probably just take the average of the 3 rgb values and compare them(?) It would produce a low value for blue and give preference to white text.
jbritton2 days ago
At a minimum it would be nice to know good colors for the pseudo classes active, focus, hover, link, visited and their various combinations for a light and dark theme. Additionally material UI adds disabled, before, after.
atum472 days ago
I made a video tutorial about a similar thing long time ago - choosing black or white for text color given a color background. My solution was very simplistic. I just transformed the color to gray scale and compared it between black and white. It was a fun project. I&#x27;m not good making videos though.<p><a href="https:&#x2F;&#x2F;youtu.be&#x2F;tUJvE4xfTgo?si=vFlegFA_7lzijfSR" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;tUJvE4xfTgo?si=vFlegFA_7lzijfSR</a> (warning: video is in Portuguese)
评论 #44016795 未加载
rendaw1 day ago
You choose all the colors in a color scheme, so why is this easier than just choosing a contrasting button text color in the first place? This is a feature to help teams so dysfunctional that individuals are free to choose an inconsistent background color yet at the same time aren&#x27;t able to choose a contrasting foreground color?<p>What really needs a fix is when you have text over an image or other diverse background (like, sticky&#x2F;fixed text over a scrolling background) and need to have it always visible. And... this doesn&#x27;t help at all.<p>So not only does this only (maybe) help in very questionable circumstances, they needed to come up with an entirely new verb for it, it has an anemic feature set (only selects black or white), and they did it with the worst possible contrast selection algorithm (doesn&#x27;t select the choice with the most perceptual contrast). Way to go!
评论 #44018782 未加载
评论 #44018788 未加载
andix1 day ago
Is there a good alternative for this that is done at build time? Something that works on top of SASS, Tailwind, etc?<p>It will take some time until this feature is broadly available, and I&#x27;m having some doubt that it will be implemented in the same (or correct) way on all platforms.
ZYbCRq22HbJ2y71 day ago
Tentative future of this feature, that addresses many concerns in this thread:<p><a href="https:&#x2F;&#x2F;drafts.csswg.org&#x2F;css-color-6&#x2F;#colorcontrast" rel="nofollow">https:&#x2F;&#x2F;drafts.csswg.org&#x2F;css-color-6&#x2F;#colorcontrast</a>
akkartik1 day ago
Recently I made a little hypertext browser in 500 lines. Then I added this sort of automatic contrasting color selector in another 200 lines. In the process I learned a lot about color spaces.<p><a href="https:&#x2F;&#x2F;akkartik.name&#x2F;post&#x2F;2025-04-04-devlog" rel="nofollow">https:&#x2F;&#x2F;akkartik.name&#x2F;post&#x2F;2025-04-04-devlog</a><p>One difference in my approach is: it&#x27;s an authoring-time tool. If no sufficiently contrasting color exists you get an error. And so you have to change the background until there is one.
naggie1 day ago
Cool! I did a similar thing manually with python and terminal colours <a href="https:&#x2F;&#x2F;calbryant.uk&#x2F;blog&#x2F;destroying-the-right-server-with-colour-association&#x2F;" rel="nofollow">https:&#x2F;&#x2F;calbryant.uk&#x2F;blog&#x2F;destroying-the-right-server-with-c...</a>
lancekey1 day ago
I remember doing something similar back in the day using YIQ value - <a href="https:&#x2F;&#x2F;medium.com&#x2F;@gkobilansky&#x2F;a-color-changing-take-on-the-sticky-menu-8096ddd533df" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@gkobilansky&#x2F;a-color-changing-take-on-the...</a>
Hyperlisk1 day ago
Here&#x27;s one method for this that I have bookmarked: <a href="https:&#x2F;&#x2F;miunau.com&#x2F;posts&#x2F;dynamic-text-contrast-in-css&#x2F;" rel="nofollow">https:&#x2F;&#x2F;miunau.com&#x2F;posts&#x2F;dynamic-text-contrast-in-css&#x2F;</a>
crtasm2 days ago
&gt;This browser does not support contrast-color(). Try this demo in a browser that does, like Safari Technology Preview
评论 #44016068 未加载
评论 #44015956 未加载
dp-hackernews2 days ago
Surely the relative colour theory colour wheel is the answer to this problem.<p>&quot;Color Wheel: The Basic Color Theory for Artists and Designers&quot; <a href="https:&#x2F;&#x2F;dessign.net&#x2F;color-wheel-theory&#x2F;" rel="nofollow">https:&#x2F;&#x2F;dessign.net&#x2F;color-wheel-theory&#x2F;</a>
flysand71 day ago
Sadly, doesn&#x27;t work on firefox yet :(
HocusLocus1 day ago
TLDR: &quot;This browser does not support contrast-color(). Try this demo in a browser that does, like Safari Technology Preview.&quot;
refulgentis2 days ago
The article is wrong:<p>- Their work <i>does</i> ensure contrast.<p>- The white on blue clearly has <i>less</i> contrast, not <i>more</i>. (squinting is a cheap way to test, or, walking backwards from your monitor)<p>With APCA, backgrounds around L* 60 tend to still allow white foregrounds, which is <i>aesthetically</i> closer to what the eye wants.<p>A black foreground would have more contrast regardless, even by APCA.<p>To be fair, this is how APCA is almost always demonstrated as a win over the long-running standard, so people run with the premise that the demo image of APCA is <i>more contrast</i>, rather than &quot;ours say you&#x27;ll have <i>enough</i> contrast to be accessible with a white foreground, even if it also says the contrast would be higher with a black foreground&quot;.<p>(source: in 2020 built color system around the same science, enabling latest iterations of Material theming)
评论 #44019604 未加载
评论 #44016331 未加载