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.

Show HN: A Dark Mode Toggle Without JavaScript

4 pointsby TymekDevover 1 year ago
Today I scratched my itch of implementing a dark mode toggle without JacaScript. The implementations I saw typically revolved about adding a &#x27;dark&#x27; class to body via JS and then controlling it with JS handlers.<p>Thanks to `:has()` selector [0], that passed 90% browser compatibility [1], it is possible to avoid JS.<p>My implementation uses CSS variables combined with a checkbox, `:checked`, and `:has()`. It is simple, yet supports color scheme preference and updates toggle&#x27;s label based on active mode.<p>[0]: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;CSS&#x2F;:has" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;CSS&#x2F;:has</a><p>[1]: <a href="https:&#x2F;&#x2F;caniuse.com&#x2F;css-has" rel="nofollow">https:&#x2F;&#x2F;caniuse.com&#x2F;css-has</a>

3 comments

DHPersonalover 1 year ago
This is really cool and a great example of the upgrades to CSS. The benefit of JavaScript would be setting a state in localStorage to remember the light&#x2F;dark value that was set, as yours does not remember what was set after a shift+f5 refresh of the site.
评论 #38964347 未加载
numtelover 1 year ago
I&#x27;m sure it&#x27;ll make it to Firefox for Android soon. I would imagine these &quot;in page&quot; toggles will fade away as the OS makes the switcher more prominent. In Gnome, it&#x27;s right in the main menu next to the volume.
评论 #38964313 未加载
baobabaover 1 year ago
Thanks for sharing! Very cool.