Skip to content

Instantly share code, notes, and snippets.

@djpogo
Last active February 15, 2020 20:42
Show Gist options
  • Save djpogo/4834733b2a212d362d03b1b3b1b6b612 to your computer and use it in GitHub Desktop.
Save djpogo/4834733b2a212d362d03b1b3b1b6b612 to your computer and use it in GitHub Desktop.
CSS Dark Mode Media Query with Custom Property usage
<!DOCTYPE html>
<html lang="de">
<head>
<title>I'm in the mode</title>
<meta charset="utf-8">
<meta name="description" content="Sometimes I ran…">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Big+Shoulders+Text&display=swap" rel="stylesheet">
<style>
:root {
--bg-color: hsla(0, 0%, 90%, 1);
--text-color: hsla(0, 0%, 20%, 1);
}
*,
*::before,
*::after {
box-sizing: border-box;
}
html {
width: 100vw;
height: 100vh;
max-width: 100%;
min-height: 100%;
display: flex;
overflow-x: hidden;
margin: 0;
align-items: center;
justify-content: center;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
font-family: 'Big Shoulders Text', cursive;
transition: color .2s ease-in, background-color .2s ease-out;
}
.mode::after {
content: "light";
}
@media (prefers-color-scheme: dark) {
body {
--bg-color: hsla(0, 0%, 20%, 1);
--text-color: hsla(0,0%, 80%, 1);
}
.mode::after {
content: "dark";
}
}
</style>
</head>
<body>
<section class="hero">
<h1>Welcome to the <span class="mode"></span> mode</h1>
</section>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment