TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: A Dark Mode Toggle Without JavaScript

4 点作者 TymekDev超过 1 年前
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 条评论

DHPersonal超过 1 年前
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 未加载
numtel超过 1 年前
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 未加载
baobaba超过 1 年前
Thanks for sharing! Very cool.