Created
January 18, 2024 21:46
-
-
Save gustavjorlov/8bf65b793f9f866bca8c6536eeea5e47 to your computer and use it in GitHub Desktop.
A CSS Custom Property based theme toggle implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
<style> | |
:root { | |
--background: #dddddd; | |
--text-color: #333333; | |
} | |
.dark { | |
--background: #333333; | |
--text-color: #dddddd; | |
} | |
body { | |
background-color: var(--background); | |
color: var(--text-color); | |
} | |
</style> | |
</head> | |
<body> | |
<button id="darkmode">Theme Toggle</button> | |
<h1>Title</h1> | |
<p>Text</p> | |
</body> | |
<script> | |
document.querySelector("#darkmode").addEventListener("click", () => { | |
document.querySelector("body").classList.toggle("dark"); | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment