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

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

dgl.cx

SSH port knocking with OpenBSD 7.9 SSH port knocking with OpenBSD 7.9 Bash a newline: Exploiting SSH via ProxyCommand, again (CVE-2025-61984) Images over DNS CVE-2025-48384: Breaking Git with a carriage return and cloning RCE Can your terminal do emojis? How big? Blink and you'll miss it — a URL handler surprise Using HAProxy to protect me from scrapers Déjà vu: Ghostly CVEs in my terminal title Restrict sftp with Linux user namespaces ""?! ANSI Terminal security in 2023 and finding 10 CVEs NAT-Again: IRC NAT helper flaws ip.wtf and showing you your actual HTTP request
Switchable dark mode with 5 lines of JavaScript
2025-09-28 · via dgl.cx

In lyra's excellent, epic blog post "You no longer need JavaScript" there is a section (Lunalover) on how to make a switchable dark mode in only HTML + CSS.

The short version is you add some buttons (which you can style to not look like buttons), which then override the color-scheme as needed through :has() queries on the buttons' checked state:

:root {
  color-scheme: light dark;
  &:has(#theme-light:checked) {
    color-scheme: light;
  }
  &:has(#theme-dark:checked) {
    color-scheme: dark;
  }
}

She then goes on to say:

Pro tip! CSS can’t save the theme preference, but you can still do progressive enhancement. Make the themes work CSS-only, and then add the saving/loading of preference as an optional extra in JavaScript or server-side code.

So I thought I'd do exactly that. The icon at the top right of the banner of this site (scroll to the top to see it) will change the mode and save the preference to your browser's localStorage.

This works through some HTML to display the icons:

<span role="radiogroup">
  <label title="auto" id="theme-auto-label">
    <input type="radio" name="color-scheme" id="theme-auto" checked>✨
  </label>
  <label title="light" id="theme-light-label">
    <input type="radio" name="color-scheme" id="theme-light">☀️
  </label>
  <label id="theme-dark-label" title="dark">
    <input type="radio" name="color-scheme" id="theme-dark">☁️
  </label>
</span>

Some CSS to hide all but the next icon (i.e. the icon shows what clicking it will do):

header {
  @media (prefers-color-scheme: dark) {
    &:has(#theme-auto:checked) #theme-dark-label,
    &:has(#theme-light:checked) #theme-auto-label,
    &:has(#theme-dark:checked) #theme-light-label {
      display: none;
    }
  }

  /* ... 2 other sets of selectors for different media query + label combinations. */
}

That all works and is really only a minor modification of the original article, including essentially toggle buttons for the theme that is used, totally without JavaScript, but it doesn't save the state between pages.

That just needs a tiny piece of JavaScript:

for (const item of ["theme-auto", "theme-light", "theme-dark"])
  document.getElementById(item).addEventListener("change", () =>
    localStorage.setItem("theme", item));
let theme = localStorage.getItem("theme");
if (theme) document.getElementById(theme).click();

Basically save the state if the user changes it and when the page loads, "click" the relevant button if they saved the state. If the user visits multiple pages the JavaScript will be cached, so this basically happens right on load. If the user doesn't have the JavaScript cached it may result in a flash of colour. (This could obviously be avoided by inlining these few lines, but then I would have to adjust the CSP so I decided to live with this.)

One more hack was needed to fully implement dark mode, the GitHub icon at the bottom of each page didn't look good, so I changed the image via CSS, using object-position to hide the original image and override the background via CSS:

/* A lot of code just to get the github icon looking right... Has to be duplicated for different media queries. */
@media (prefers-color-scheme: dark) {
  body:not(:has(#theme-light:checked)) footer a[href="https://github.com/dgl"] img {
    object-position: -99999px 99999px;
    background-image: url('/static/github-white.png');
    background-size: contain;
    backdrop-filter: grayscale(1) opacity(.5);
    &:hover {
      backdrop-filter: opacity(1);
      transition: backdrop-filter 500ms;
    }
  }
}

All this is in the CSS and JavaScript resources for this site, feel free to use elsewhere (sorry the CSS is a bit disorganised, it has been evolved over many years...).

28th September 2025 in ,