Skip to content

Instantly share code, notes, and snippets.

@redeux
Last active November 22, 2020 20:59
Show Gist options
  • Save redeux/6537e91ba6eccf784afa91d3271167e4 to your computer and use it in GitHub Desktop.
Save redeux/6537e91ba6eccf784afa91d3271167e4 to your computer and use it in GitHub Desktop.
Simple theme toggler
<html lang="en">
<head>
<style type="text/css">
body {
background-color: #000;
color: #fff;
}
body.light {
background-color: #fff;
color: #000;
}
#t {
visibility: hidden;
}
#t::before {
content: 'light ';
}
body.light #t::before {
content: 'dark ';
}
</style>
</head>
<body>
</body>
<footer>
<a id="t">theme</a>
</footer>
<script>
function c() {return localStorage.getItem("c")}
c() === "1" ? document.body.classList.toggle('light') : null;
var t = document.getElementById("t");
t.style.visibility = 'visible';
t.onclick = function () {
c() === "1" ? localStorage.setItem("c", 0) : localStorage.setItem("c", 1);
document.body.classList.toggle('light');
}
</script>
</html>
@redeux
Copy link
Author

redeux commented Nov 22, 2020

Simple binary theme toggle

  • Changes theme by toggling a single class on <body>.
  • Remembers theme selection between pages and sessions.
  • Toggle isn't visible when JavaScript is disabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment