Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
CSS

CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode

- 04.10.26 - ErcanOPAK

🎨 The Dark Mode Query That Wasn’t the Only One Running

A prefers-color-scheme media query reads the operating system’s reported color scheme preference and switches a site’s own styles accordingly, which works correctly and predictably as long as the only thing deciding how the page looks is the page’s own stylesheet. A browser extension that forces a dark appearance onto every site by rewriting computed colors after the page renders operates at a completely different layer, applied after your stylesheet has already done its job, and the two systems can disagree without either one reporting an error – the media query correctly detects a light-mode system preference and renders light-mode styles, while the extension still forces a dark overlay on top of them a moment later, producing a combination of colors neither the page nor the extension ever actually intended together.

🔎 The Problem

@media (prefers-color-scheme: dark) {
  body { background: #121212; color: #eaeaea; }
  .card { background: #1e1e1e; border-color: #333; }
}

@media (prefers-color-scheme: light) {
  body { background: #ffffff; color: #1a1a1a; }
  .card { background: #f5f5f5; border-color: #ddd; }
}

/* This correctly renders the light-mode rules when the OS reports
   a light preference - the media query itself is not wrong. A
   browser extension with forced dark mode enabled then rewrites
   the computed background and text colors AFTER layout, on top
   of the already-correct light styles, producing a page where a
   light-styled .card sits on a forced-dark body, with image and
   icon contrast tuned for one scheme sitting on a background from
   the other - a combination this stylesheet never had to account
   for, because it assumes only one system is deciding the colors. */

✅ Fix: Don’t Assume Your Media Query Is the Only Thing Deciding Colors

  • Reproduce the issue by actually enabling a forced-dark-mode browser extension during testing, rather than relying solely on the operating system’s own light/dark toggle, since that toggle alone never exercises the extension layer at all.
  • Set an explicit color-scheme meta value or CSS property declaring which schemes the page actually supports, which gives some forced-dark extensions a stronger signal to respect the page’s own styling instead of overriding it uniformly.
  • Avoid relying on an image, icon, or illustration whose contrast was tuned for only one color scheme, and prefer styling approaches – a background shorthand instead of separate layered properties, for instance – that degrade more predictably if something outside the page’s own stylesheet later adjusts individual color values.

⚠️ Why This Is Easy to Miss

  • The media query itself is implemented correctly and passes every test that checks the operating system’s own preference toggle, so there is nothing in the stylesheet’s logic to suggest the final rendered result depends on anything beyond that one setting.
  • A forced-dark-mode extension is specific to whichever browser and configuration an individual visitor happens to be running, so the mismatched colors only appear for a subset of visitors and never show up in a design review done on a clean, extension-free browser.

A color-scheme media query answers one question correctly – what did the operating system report – and has no say at all over what happens to the colors after that answer leaves its hands.

— UI Engineer

Related posts:

CSS: Use Scroll-Driven Animations for Parallax Without JS

CSS: Use Scroll-Driven Animations for Parallax Without JS

How to align inconsistently sized logos with CSS

How to align inconsistently sized logos with CSS

CSS — display: none Skips Layout but Breaks Animations

CSS — display: none Skips Layout but Breaks Animations

Views: 12
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
JavaScript: Fix an Array.flat() Call That Silently Stops at One Level Because the Default Depth Isn’t Infinity

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com