<input class="dark" type="checkbox" />
<input class="light" type="checkbox" />
<article>
<h1>Dark Mode w Pure CSS</h1>
<p>
This is an example to display how to support dark mode without using
any Javascript.
</p>
</article>
:root {
--bg-color: #ffffff75;
--font-color: #121212;
--em-font-color: #121212;
--border-color: #218095;
--gradient-style: radial-gradient(
circle,
#f6ff1c,
#ffdc00,
#ffb800,
#ff951a
);
--gradient-size: 8px;
--gradient-shadow: #ffb800;
}
@media (prefers-color-scheme: light) {
.light {
display: none;
}
}
@media (prefers-color-scheme: dark) {
.dark {
display: none;
}
--bg-color: #121212;
--font-color: #ffffff75;
--em-font-color: #ffffff87;
--border-color: #c9e3e9;
--gradient-style: radial-gradient(
circle,
#ccd325,
#d3d84b,
#dadd68,
#e0e183,
#e6e69c
);
--gradient-size: 2px;
--gradient-shadow: #fff;
}
/* swith to dark mode when dark button checked */
.dark:checked,
.dark:checked + * + * {
--bg-color: #121212;
--font-color: #ffffff75;
--em-font-color: #ffffff87;
--border-color: #c9e3e9;
--gradient-style: radial-gradient(
circle,
#ccd325,
#d3d84b,
#dadd68,
#e0e183,
#e6e69c
);
--gradient-size: 2px;
--gradient-shadow: #fff;
}
/* swith to light mode when light button checked */
.light:checked,
.light:checked + * {
--bg-color: #ffffff75;
--font-color: #121212;
--em-font-color: #121212;
--border-color: #218095;
--gradient-style: radial-gradient(
circle,
#f6ff1c,
#ffdc00,
#ffb800,
#ff951a
);
--gradient-size: 8px;
--gradient-shadow: #ffb800;
}
/* apply css variable */
body {
margin: 0;
}
* {
background: var(--bg-color);
color: var(--font-color);
}
input[type="checkbox"] {
-webkit-appearance: none;
outline: 0;
position: absolute;
z-index: 2;
top: 10px;
right: 10px;
width: 20px;
height: 20px;
border-radius: 50%;
background-image: var(--gradient-style);
box-shadow: 0 0 14px var(--gradient-size) var(--gradient-shadow);
cursor: pointer;
}
article {
position: absolute;
width: 100vw;
height: 100vh;
padding: 24px;
}
h1 {
font-size: 24px;
margin-top: 20px;
padding-bottom: 4px;
border-bottom: 3px solid var(--border-color);
color: var(--em-font-color);
}
p {
font-size: 18px;
margin: 10px 0;
line-height: 1.4;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.