How to make a light and dark mode button in HTML, CSS, and JS!

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous"><title>Dark mode</title></head><body></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="style.css"><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous"><title>Dark mode</title></head><body><button type="button" class="btn btn-dark">Dark Mode</button></body></html>
.dark {background-color: black;}
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="style.css"><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous"><title>Dark mode</title></head><body id="body" class=""><button type="button" class="btn btn-dark" class="button">Dark Mode</button><script>let button = document.querySelector('button');let body = document.getElementById('body');button.addEventListener('click', () => {body.classList.toggle('dark');if(body.classList.contains('dark')) {button.style.backgroundColor = "white";button.innerHTML = "Light Mode"button.style.color = "black";} else {button.style.backgroundColor = "black";button.innerHTML = "Dark Mode"button.style.color = "white";}});</script></body></html>

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to make “nanoid” a test double with Sinon.js

YouTube Video | Indian State Details Search app

React: State vs. Props

IBM Business Automation Workflow Creating a Reusable JavaScript Library

Webpack: Installing different versions of the same package

10 Effective Coding Style & JavaScript DOM

How to Shake Up a React Game using onKeyPress

LeetCode Algorithm Challenge: Remove Duplicates from Sorted List — Lists vs Arrays

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kirill McQuillin

Kirill McQuillin

More from Medium

The Complete JS Notes #2 🧵

An Introduction To CSS Box Models

How to compile SASS into CSS and watch for changes?

What does CSS do? The power of Cascading Style Sheets (1/3)