<body>
<input id="color-mode" type="checkbox" name="color-mode">
<label for="color-mode">
<span class="dark-mode-hide">Dark Mode</span>
<span class="light-mode-hide">Light Mode</span>
</label>
<div class="color-scheme-wrapper">
<h1>No Javascript Dark Mode Toggle</h1>
<p>Sometimes <code>javascript</code> <strong>fails</strong>, and sometimes people have it <strong>switched off</strong>. </p>
<p>For those people it's still nice to have the option of toggling <code>dark-mode</code> on and off.</p>
<p><strong>This example uses:</strong></p>
<ul>
<li>No <code>javascript</code></li>
<li><code>media-queries</code> to default to user preferences.</li>
<li><code>css variables</code> to make styling way easier.</li>
</ul>
<p>If you're interested in learning how to make this you can read the tutorial here: <a href="https://endtimes.dev/no-javascript-dark-mode-toggle/">endtimes.dev</a></p>
<p>or here: <a href="https://dev.to/shadowfaxrodeo/an-html-and-css-only-dark-mode-toggle-button-1p3c">dev.to</a></p>
<p>See a live version using <strong>five</strong> color schemes and radio buttons here: <a href="https://missingdice.com/dice-roller">missingdice.com</a></p>
</div>
</body>
/* Tutorial CSS starts here */
:root {
--bg: white;
--text: black;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: black;
--text: white;
}
}
#color-mode:checked ~ * {
--bg: black;
--text: white;
}
@media (prefers-color-scheme: dark) {
#color-mode:checked ~ * {
--bg: white;
--text: black;
}
}
.color-scheme-wrapper {
min-height: 100vh;
background: var(--bg);
color: var(--text);
}
.light-mode-hide {
display: none;
}
@media (prefers-color-scheme: dark) {
.dark-mode-hide {
display: none;
}
}
@media (prefers-color-scheme: dark) {
.light-mode-hide {
display: initial;
}
}
/* Tutorial css ends here, the rest is extra styles for prettiness */
:root {
font-size: 16px;
font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir,
helvetica neue, helvetica, Ubuntu, roboto, noto, segoe ui, arial, sans-serif;
}
* {
box-sizing: border-box;
max-width: 35rem;
}
html,
body,
div {
max-width: none;
}
p {
line-height: 1.5;
}
code {
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console,
monospace;
}
input[type="checkbox"] {
opacity: 0.00001;
position: absolute;
}
html,
body {
padding: 0;
margin: 0;
}
div > * + *,
ul > * + * {
margin-top: 1.5rem;
}
::selection {
background: var(--text);
color: var(--bg);
}
label {
cursor: pointer;
position: absolute;
background: var(--text);
color: var(--bg);
padding: 1rem;
top: 1rem;
left: 1rem;
display: flex;
align-items: center;
border-radius: 6px;
box-shadow: 0 2px var(--bg), 0 4px var(--text);
user-select: none;
}
input[type="checkbox"]:focus + label {
outline: 2px dashed red;
outline-offset: 4px;
}
label:active {
transform: translateY(1px);
box-shadow: 0 1px var(--bg), 0 3px var(--text);
}
label:after {
content: "✔\FE0E";
height: 1.2rem;
width: 1.2rem;
margin-left: 1rem;
border: solid 2px var(--bg);
text-align: center;
border-radius: 4px;
color: transparent;
}
#color-mode:checked + label:after {
color: var(--bg);
}
.color-scheme-wrapper {
padding: 5rem 1rem;
}
a {
color: inherit;
text-decoration: none;
border: dotted 1px var(--text);
padding: 0.1em 0.2em;
}
ul {
list-style: none;
margin: 0;
padding-left: 1rem;
}
li:before {
margin-left: -1rem;
float: left;
content: "▸";
color: red;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.