CSS Day / Night Toggle
A dark-mode switch where the sun slides across and turns into a crescent moon.
<label class="theme-toggle">
<input type="checkbox" aria-label="Dark mode">
<span></span>
</label>
.theme-toggle {
position: relative;
display: inline-block;
width: 64px;
height: 34px;
}
.theme-toggle input {
position: absolute;
inset: 0;
z-index: 1;
margin: 0;
opacity: 0;
cursor: pointer;
}
.theme-toggle span {
position: absolute;
inset: 0;
border-radius: 999px;
background: #38bdf8;
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15);
transition: background 0.4s ease;
}
/* The sun */
.theme-toggle span::before {
content: "";
position: absolute;
top: 4px;
left: 4px;
width: 26px;
height: 26px;
border-radius: 50%;
background: #fde047;
box-shadow:
inset 0 0 0 0 #f8fafc,
0 0 10px 2px rgba(253, 224, 71, 0.6);
transition: transform 0.4s cubic-bezier(0.4, 1.4, 0.6, 1), background 0.4s ease, box-shadow 0.4s ease;
}
.theme-toggle input:checked + span {
background: #1e1b4b;
}
/* ...becomes the moon */
.theme-toggle input:checked + span::before {
background: rgba(253, 224, 71, 0);
box-shadow:
inset -9px -3px 0 0 #f8fafc,
0 0 10px 2px rgba(248, 250, 252, 0);
transform: translateX(30px);
}
.theme-toggle input:focus-visible + span {
outline: 2px solid #6366f1;
outline-offset: 3px;
}
About this CSS Day / Night Toggle
This CSS Day / Night Toggle is 58 lines of pure CSS (1.1 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and the :checked state (no JavaScript). It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
Customize it
The colors in this snippet. Search and replace them to match your brand:
#38bdf8#fde047#f8fafc#1e1b4b#6366f1
How to use this day / night toggle
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.theme-toggleclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.