User Agents

Web browsers and everyday web tech, explained

Enable Dark Mode on Websites with CSS and Browser Tools

· 6 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Enabling dark mode on websites allows you to reduce eye strain in low-light environments and potentially save battery life on OLED screens by replacing bright white backgrounds with dark tones. This can be achieved either by implementing CSS variables and media queries if you are the developer, or by using browser developer tools and extensions to force a dark theme on sites you do not own. By manipulating the Document Object Model (DOM) and CSS styles, you can invert colors or apply a custom dark color palette to any webpage in real-time.

What You'll Need

  • A modern web browser (Google Chrome, Mozilla Firefox, Microsoft Edge, or Safari).
  • Basic knowledge of CSS selectors (e.g., knowing what body or .class means).
  • Access to Browser Developer Tools (usually accessed via F12 or right-click > Inspect).
  • Optional: A CSS extension like Stylus or Stylish for permanent changes.

Step-by-Step

  1. Open the Website and Developer Tools: Navigate to the website you wish to modify. Right-click anywhere on the page and select "Inspect" or press F12 (Windows/Linux) or Cmd + Option + I (Mac). This opens the Developer Tools panel, which allows you to view and edit the HTML and CSS of the page live.
  2. Identify the Primary Background and Text Colors: In the "Elements" tab, select the <body> tag. Look at the "Styles" pane to see which CSS rules are controlling the background-color and color (text color). Note whether these are hard-coded hex values (like #ffffff) or CSS variables (like var(--bg-color)).
  3. Apply a Quick Inversion Filter: For a fast, temporary dark mode, go to the "Styles" tab in the inspector and add the following rule to the body selector: filter: invert(1) hue-rotate(180deg);. The invert(1) flips all colors to their opposites, and hue-rotate(180deg) prevents the colors from shifting too drastically (e.g., keeping blues blue instead of turning them orange).
  4. Implement a Custom CSS Dark Theme: If you want a professional look rather than a simple inversion, create a custom style block. In the "Elements" tab, find the <head> section, right-click it, and select "Edit as HTML". Insert a <style> tag and define your dark mode colors:
    body {
          background-color: #121212 !important;
          color: #e0e0e0 !important;
        }
        a {
          color: #bb86fc !important;
        }
        div, section, nav {
          background-color: #1e1e1e !important;
          border-color: #333 !important;
        }
    The !important flag is crucial here because it overrides the website's existing inline styles and specific selectors.
  5. Handle Images and Media: A common problem with inverted dark modes is that images and videos also become inverted, looking like negatives. To fix this, add a rule that re-inverts media elements: img, video, canvas { filter: invert(1) hue-rotate(180deg); }. This ensures the background remains dark while the visual content remains natural.
  6. Automate with Browser Flags (Chrome/Edge): If you want a system-wide "forced" dark mode without writing CSS for every site, type chrome://flags (or edge://flags) into your address bar. Search for "Auto Dark Mode for Web Contents". Change the setting from "Default" to "Enabled". Restart the browser. This uses an internal algorithm to analyze page colors and invert them automatically.
  7. Make Changes Permanent with User CSS: Since Developer Tools changes disappear upon refreshing the page, install an extension like Stylus. Create a new style for the specific domain, paste the CSS you developed in Step 4, and save it. The extension will now inject this CSS every time you visit the site.

Common Mistakes

One of the most frequent errors is forgetting the !important declaration. Modern websites use highly specific CSS selectors (like .main-content .article-body p) that will easily override a simple body selector. Without !important, your dark mode styles may only apply to a small fraction of the page.

Another mistake is neglecting contrast ratios. Simply changing a white background to black and black text to white can sometimes lead to "haloing" or vibration effects for users with astigmatism. Using a deep charcoal (#121212) instead of pure black (#000000) and an off-white (#e0e0e0) instead of pure white (#ffffff) creates a more comfortable reading experience.

Users often forget to target nested elements. Many websites do not set the background color on the body tag alone; they use multiple nested div containers with their own white backgrounds. If you only change the body color, you will see "white boxes" where the content actually resides. You must target div, section, article, nav, header, footer to ensure total coverage.

Tips

To make your dark mode feel native, try using CSS variables. If the site already uses variables (e.g., --primary-bg), you can simply redefine those variables in the :root selector in your CSS. This preserves the site's original design logic while changing the color palette.

Utilize the prefers-color-scheme media query if you are building a site. This allows the website to automatically switch to dark mode based on the user's operating system settings: @media (prefers-color-scheme: dark) { ... }. This is the gold standard for modern web accessibility.

When using the inversion filter method, try invert(0.9) instead of invert(1). A slight reduction in inversion often results in a softer, more natural dark grey rather than a stark, high-contrast black, which is easier on the eyes during long browsing sessions.

Troubleshooting

The page is still white in some areas: This happens when elements have inline styles (styles written directly in the HTML tag). Inspect the white area, find the specific element, and check if it has a style="background-color: white;" attribute. To fix this globally via CSS, you must use !important on your selectors.

Images look like negatives: This occurs when you apply a filter to the body or html tag. The filter cascades down to all children. To resolve this, apply the same filter to the img and video tags to "double-invert" them back to their original state.

The layout shifted or broke: Some websites use background images or gradients that rely on specific color blending. Inverting these can occasionally cause visual glitches. In these cases, avoid the filter: invert method and instead manually define colors for the specific classes causing the issue.

Changes disappear after refresh: Remember that the Browser Inspector is for temporary debugging. To keep your dark mode, you must use a browser extension that manages User CSS or enable the browser's internal "Auto Dark Mode" flag.

Frequently Asked Questions

Does enabling dark mode save battery?

On devices with OLED or AMOLED screens, yes. These screens turn off pixels entirely to show black, which consumes significantly less power than illuminating pixels for white or grey.

Will this change the website for everyone else?

No. Changes made through Developer Tools or browser extensions are local to your machine. Other users will still see the original version of the website.

Is the "Auto Dark Mode" flag safe to use?

Yes, it is a built-in feature of the Chromium engine. However, because it is an experimental flag, it may occasionally render some websites incorrectly or cause slight visual bugs.

Can I create a dark mode for a site that uses iframes?

It is difficult. Due to security restrictions called the Same-Origin Policy, you cannot easily inject CSS into an iframe that loads content from a different domain than the main page.

What is the best background color for dark mode?

Avoid pure black (#000) as it can cause high contrast strain. Most designers recommend dark greys like #121212 or #181818 for a more professional and readable look.