Skip to content

Instantly share code, notes, and snippets.

@gustavjorlov
Created January 18, 2024 21:46
Show Gist options
  • Save gustavjorlov/8bf65b793f9f866bca8c6536eeea5e47 to your computer and use it in GitHub Desktop.
Save gustavjorlov/8bf65b793f9f866bca8c6536eeea5e47 to your computer and use it in GitHub Desktop.
A CSS Custom Property based theme toggle implementation
<!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