VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 学习

  • 面试

  • 心情杂货

  • 实用技巧

    • The 2-Minute Rule
    • Turn Any Website Dark with a Single Line of Code
  • Blogroll
  • 更多
  • 实用技巧
xugaoyi
2021-11-25

Turn Any Website Dark with a Single Line of CodeOriginal

# Turn Any Website Dark with a Single Line of Code

A practical trick: one line of JavaScript is all it takes to apply dark mode to any website instantly.

Let's start with a quick experiment. Open any website, launch the browser DevTools (F12), enter the following into the Console and hit Enter:

document.documentElement.style.filter='invert(85%) hue-rotate(180deg)'
1

The page instantly switches to dark mode.

How it works
  1. document.documentElement returns the root element of the document — the <html> tag.
  2. We apply a CSS filter to it: invert(85%) hue-rotate(180deg).
  3. invert() reverses all color values. At 85%, the result is a comfortable dark theme rather than a full negative.
  4. hue-rotate(180deg) rotates the hue wheel by 180 degrees, which compensates for the color shift introduced by invert() and keeps the original hues roughly intact.

For more on CSS filters, see the MDN reference: filter (opens new window).

To make this even more practical — a single click to toggle the effect on any page — here is an improved version. It cycles through three states (normal → 85% dark → 100% dark) and excludes images, pictures and videos from the inversion so they retain their original appearance:

javascript: (function () {  const docStyle = document.documentElement.style;  if (!window.modeIndex) {    window.modeIndex = 0;  }  const styleList = [    '',    'invert(85%) hue-rotate(180deg)',   'invert(100%) hue-rotate(180deg)',  ];  modeIndex = modeIndex >= styleList.length - 1 ? 0 : modeIndex + 1;  docStyle.filter = styleList[modeIndex];  document.body.querySelectorAll('img, picture, video').forEach(el => el.style.filter = modeIndex ? 'invert(1) hue-rotate(180deg)' : '');})();
1

Open your browser's bookmark manager, create a new bookmark, and paste this snippet into the URL field:

Now, on any website, just click the bookmark to switch to 85% dark mode. Click again for 100% dark. Click once more to return to normal.

Edit (opens new window)
#JavaScript#CSS#Practical Tips
Last Updated: 2026/03/21, 12:14:36
The 2-Minute Rule
Blogroll

← The 2-Minute Rule Blogroll→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode