CSS Dark, Light mode switch in CSS

How to create a Dark, Light mode switch in CSS

Posted by Box XV on December 18, 2019. 1 min read.

English

How To - Toggle Dark Mode | W3schools

Body Class: Change the class for the <body> to override the CSS for each theme

<body class="dark-mode">
body {
  background-color: white;
}
body.dark-mode {
  background-color: black;
}

Dark Mode in CSS | Css-tricks

@media (prefers-color-scheme: dark) {

How to create a dark\light mode switch in CSS and Javascript | Codyhouse

CSS Variables + Body data attribute

:root {
  --color-bg: #ffffff;
}
[data-theme="dark"] {
  --color-bg: #000000;
}
body {
  background-color: var(--color-bg);
}
<body data-theme="dark">

Dark and Light theme switcher using CSS variables and pure JavaScript | Medium

https://codepen.io/haxzie/pen/xxKNEGM
CSS Variables + <Html> Class

<html class="theme-dark">
localStorage.setItem('theme', 'theme-dark');
document.documentElement.className = 'theme-dark';

Create A Dark/Light Mode Switch with CSS Variables | dev.to

https://codepen.io/ananyaneogi/pen/zXZyMP
https://ananyaneogi.com/
CSS Variables + <Html> data attribute

<html data-theme="dark">
:root {
    --primary-color: #302AE6;
    --secondary-color: #536390;
    --bg-color: #fff;
}
[data-theme="dark"] {
    --primary-color: #9A97F3;
    --secondary-color: #818cab;
    --bg-color: #161625;
}
body {
    background-color: var(--bg-color);
}
localStorage.setItem('theme', 'theme-dark');
document.documentElement.setAttribute('data-theme', 'dark');

Dark/Light Mode Switcher
https://codepen.io/HarlemSquirrel/pen/NdMebZ

Vietnamese

Tạo Dark Theme với CSS Variable
https://completejavascript.com/tao-dark-theme-voi-css-variable

Hướng dẫn làm giao diện Dark mode cho website từ A-Z
https://evondev.com/huong-dan-lam-giao-dien-dark-mode-cho-website-tu-a-z/

Ứng dụng CSS variables để xây dựng tính năng dark theme
https://12bit.vn/articles/ung-dung-css-variables-de-xay-dung-dark-theme/