Last active
August 11, 2023 23:04
-
-
Save ivan/197e5d8bdd98d0aaf020d52475eb02fa to your computer and use it in GitHub Desktop.
CSS light and dark mode without JavaScript requirement and with a class-based override on <html>
This file contains hidden or 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
html { | |
background-color: | |
var(--LIGHT, #eee) | |
var(--DARK, #111); | |
color: | |
var(--LIGHT, #000) | |
var(--DARK, #ccc); | |
} | |
html { | |
--LIGHT: initial; | |
--DARK: ; | |
} | |
@media (prefers-color-scheme: dark) { | |
html { | |
--DARK: initial; | |
--LIGHT: ; | |
} | |
} | |
/* Override `prefers-color-scheme: dark` if class is present */ | |
html.light-mode { | |
--LIGHT: initial; | |
--DARK: ; | |
} | |
html.dark-mode { | |
--DARK: initial; | |
--LIGHT: ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
color: var(--LIGHT, #000) var(--DARK, #ccc);
, when--LIGHT
is invalid (set toinitial
) it falls back to#000
, at the same time--DARK
is valid (a whitespace) so that part just evaluates to a whitespace instead of#111
.