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

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
aimingoo的专栏
aimingoo的专栏
J
Java Code Geeks
腾讯CDC
大猫的无限游戏
大猫的无限游戏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
博客园_首页
F
Fortinet All Blogs
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
博客园 - 【当耐特】
量子位
博客园 - 叶小钗
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
MyScale Blog
MyScale Blog
爱范儿
爱范儿
The Cloudflare Blog
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Why Your Dark Mode Looks Weird (And How to Fix It)
Yue Geng · 2026-05-13 · via DEV Community

TL;DR: If you're using both prefers-color-scheme and a manual .dark class to toggle themes, don't forget to also control the color-scheme CSS property. Otherwise, native browser UI (scrollbars, selects, etc.) will stay light while your page is dark. And that looks broken.

Read Original Vesion in Chinese


You probably know that prefers-color-scheme lets you detect a user's OS-level light/dark preference.

@media (prefers-color-scheme: dark) {
  :root {
    /* dark variables */
  }
}

Enter fullscreen mode Exit fullscreen mode

It's clean, it's native, and it even styles native form controls automatically — thanks to color-scheme.

:root {
  color-scheme: light dark;
}

Enter fullscreen mode Exit fullscreen mode

This tells the browser: "respect the user's preference, please."

But what if you need a manual toggle?

Many sites add a theme switcher button that overrides the OS setting. The common pattern? Add/remove a .dark class to the <html> element.

function toggleTheme() {
  const root = document.documentElement;
  root.classList.toggle("dark");
  localStorage.setItem("theme", root.classList.contains("dark") ? "dark" : "light");
}

Enter fullscreen mode Exit fullscreen mode

Then you write CSS like:

:root.dark {
  /* dark styles */
}

Enter fullscreen mode Exit fullscreen mode

Looks fine — until you notice that native UI elements (scrollbars, <select> dropdowns, date pickers) are still in light mode. Oops.

That’s the trap: color-scheme: light dark at the root still says "follow the OS", ignoring your manual .dark class.

The fix is simple

Don't let color-scheme listen to the OS if you're manually toggling themes. Let the .dark class control it:

:root {
  color-scheme: light;
}
:root.dark {
  color-scheme: dark;
}

Enter fullscreen mode Exit fullscreen mode

That's it. Now your native browser UI follows your manual toggle.

Real-world example: Memos

Memos is a beautiful open-source note app. But until recently, dark mode had a white scrollbar — jarring, right?

White scrollbar in dark mode

I opened Issue #5839, and the maintainers quickly fixed it by syncing color-scheme with the current theme:

const isDarkTheme = (theme) => theme.endsWith("-dark") || theme.endsWith(".dark");
document.documentElement.style.colorScheme = isDarkTheme(theme) ? "dark" : "light";

Enter fullscreen mode Exit fullscreen mode

If you're stuck on an older Memos version, drop this script into Settings → System → Additional script:

(function() {
  const isDarkTheme = (theme) => theme === "default-dark" || theme === "midnight";
  const updateColorScheme = (theme) => {
    document.documentElement.style.colorScheme = isDarkTheme(theme) ? "dark" : "light";
  };
  const observer = new MutationObserver(() => updateColorScheme(document.documentElement.getAttribute("data-theme")));
  observer.observe(document.documentElement, { attributes: true });
  updateColorScheme(document.documentElement.getAttribute("data-theme"));
})();

Enter fullscreen mode Exit fullscreen mode

So, do users really need a manual toggle?

Honestly? Most of the time, prefers-color-scheme + color-scheme: light dark is the best solution — zero JS, works everywhere, respects the user.

A manual toggle adds complexity. Ask yourself: is your user actually going to switch themes back and forth? Or are you just over-engineering?

Sometimes, less really is more.


What’s your take? Do you provide a theme switcher, or just follow the OS? Let me know in the comments 👇