HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Switch between dark and light mode with CSS and JavaScript
Step 1) Add HTML:
Use any element that should store the content you want to toggle the design
for. In our example, we will use <body>
for the sake of simplicity:
<body>
Step 2) Add CSS:
Style the <body>
element and create a .dark-mode
class for toggle:
body {
padding: 25px;
background-color: white;
color: black;
font-size: 25px;
}
.dark-mode {
background-color: black;
color: white;
}
Step 3) Add JavaScript:
Get the <body>
element and toggle between the .dark-mode
class:
function myFunction() {
var element =
document.body;
element.classList.toggle("dark-mode");
}
Try it Yourself »