Implementing Dark Mode with CSS Variables


Create a dark mode theme using CSS variables and prefer-color-scheme media query.

Source Code

:root {
    --background-color: #ffffff;
    --text-color: #000000;
}
@media (prefers-color-scheme: dark) {
    :root {
        --background-color: #333333;
        --text-color: #ffffff;
    }
}
body {
    background-color: var(--background-color);
    color: var(--text-color);
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments