Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a CSS-only dark mode toggle for my website? Pending Review
Asked on Apr 15, 2026
Answer
Creating a CSS-only dark mode toggle involves using the `:checked` pseudo-class on a hidden checkbox input to switch between light and dark themes. This method is simple and doesn't require JavaScript.
<!-- BEGIN COPY / PASTE -->
<input type="checkbox" id="dark-mode-toggle" style="display: none;">
<label for="dark-mode-toggle">Toggle Dark Mode</label>
<style>
body {
background-color: white;
color: black;
transition: background-color 0.3s, color 0.3s;
}
#dark-mode-toggle:checked ~ body {
background-color: black;
color: white;
}
</style>
<!-- END COPY / PASTE -->Additional Comment:
- This example uses a hidden checkbox to toggle the dark mode.
- The `label` element is used to trigger the checkbox when clicked.
- The `:checked` pseudo-class is used to apply dark mode styles when the checkbox is checked.
- CSS transitions are used for smooth color changes.
Recommended Links:
