Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BramEsposito/e2d94e1b0f2265344dd8f0c6a56a1f29 to your computer and use it in GitHub Desktop.
Save BramEsposito/e2d94e1b0f2265344dd8f0c6a56a1f29 to your computer and use it in GitHub Desktop.
Dark mode detection with toggle
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>Test dark mode</title>
<style type="text/css">
body.dark {
background-color: #212529;
color: #6c757d;
}
</style>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
document.body.classList.add('dark');
}
});
</script>
</head>
<body>
<h1>Test dark mode <small><a href="#" onclick="document.body.classList.toggle('dark');return false;">toggle</a></small> </h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment