CSS Dark Mode Media Query with Custom Property usage
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="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