Sun-Moon Theme Toggle
A theme switch whose sun glows and then bites into a moon as the knob slides and the track darkens.
AI prompt
Design a dark mode switch, 64 by 32 pixels, role switch with aria-label "Dark mode". Light state: pale sky-blue track, a round warm-yellow sun knob on the left with a soft glow ring. Dark state: deep navy track, the knob slides to the right and turns pale silver; a navy circle slides over the knob's upper right to cut a crescent moon; the glow fades out; two tiny stars fade in on the track. Transition 300 milliseconds with an ease-out. Next to it show a small preview card that changes from white with dark text to near-black with light text in sync. Keyboard accessible with a 2 pixel focus ring, prefers-reduced-motion fallback with instant changes. Plain HTML, CSS and a few lines of JavaScript.About this pattern
Definition
A theme toggle is a switch that changes between light and dark colour schemes. The sun-to-moon morph makes the state recognisable even without reading a label.
Purpose & UX principles
- Use a real switch (role switch) with an accessible name ("Dark mode") and keep the icon decorative.
- The morph should be quick (under 400ms) and never delay the actual theme change.
- Store the user's choice and respect prefers-color-scheme on first visit.
Interaction states
- Light (sun), dark (moon), focus-visible, pressed.
Motion specification
- The knob slides 300ms with an ease-out; the sun's glow fades and a crescent bite slides in; the preview card colours cross-fade over 300ms.
- Reduced motion: state changes instantly.
Responsive behaviour
- The switch is 64 by 32px, comfortable for touch; keep the surrounding hit area at least 44px tall.
- Place it in the header where people expect it.
Code
<div class="bx" id="bx"><button class="sw" id="sw" type="button" role="switch" aria-checked="false" aria-label="Dark mode"><span class="star s1"></span><span class="star s2"></span><span class="kn"><i class="bite"></i></span></button><div class="pv"><b>Preview</b><span id="lb">Light theme</span></div></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.bx{--tr:#9fd0f5;--kn:#ffc83d;--glow:rgba(255,200,61,.4);--bite:var(--tr);--card:#fff;--ink:#171717;display:grid;gap:14px;justify-items:center}
.sw{position:relative;width:64px;height:32px;border:0;border-radius:99px;background:var(--tr);cursor:pointer;overflow:hidden;transition:background .3s}
.kn{position:absolute;left:3px;top:3px;width:26px;height:26px;border-radius:50%;background:var(--kn);box-shadow:0 0 0 4px var(--glow),0 0 0 8px rgba(255,200,61,.12);transition:transform .3s ease-out,background .3s,box-shadow .3s;overflow:hidden}
.bite{position:absolute;right:-10px;top:-12px;width:22px;height:22px;border-radius:50%;background:var(--bite);transform:translate(24px,-4px);transition:transform .3s ease-out,background .3s}
.star{position:absolute;width:3px;height:3px;border-radius:50%;background:#fff;opacity:0;transition:opacity .3s}.s1{left:12px;top:9px}.s2{left:22px;top:21px}
.dark{--tr:#1b2a4a;--kn:#e4e7ee;--glow:transparent;--bite:#1b2a4a;--card:#171717;--ink:#f2f2f0}
.dark .kn{transform:translateX(32px);box-shadow:none}.dark .bite{transform:translate(0,0)}.dark .star{opacity:1}
.pv{width:200px;padding:12px 14px;border-radius:12px;background:var(--card);color:var(--ink);display:grid;border:1px solid var(--line);transition:background .3s,color .3s;font-size:13px}.pv span{font-size:12px;opacity:.7}
.sw:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
@media (prefers-reduced-motion:reduce){.sw,.kn,.bite,.star,.pv{transition:none}}const sw=document.getElementById('sw'),bx=document.getElementById('bx'),lb=document.getElementById('lb');
sw.addEventListener('click',()=>{const d=sw.getAttribute('aria-checked')!=='true';sw.setAttribute('aria-checked',String(d));bx.classList.toggle('dark',d);lb.textContent=d?'Dark theme':'Light theme';});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sun-Moon Theme Toggle — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.bx{--tr:#9fd0f5;--kn:#ffc83d;--glow:rgba(255,200,61,.4);--bite:var(--tr);--card:#fff;--ink:#171717;display:grid;gap:14px;justify-items:center}
.sw{position:relative;width:64px;height:32px;border:0;border-radius:99px;background:var(--tr);cursor:pointer;overflow:hidden;transition:background .3s}
.kn{position:absolute;left:3px;top:3px;width:26px;height:26px;border-radius:50%;background:var(--kn);box-shadow:0 0 0 4px var(--glow),0 0 0 8px rgba(255,200,61,.12);transition:transform .3s ease-out,background .3s,box-shadow .3s;overflow:hidden}
.bite{position:absolute;right:-10px;top:-12px;width:22px;height:22px;border-radius:50%;background:var(--bite);transform:translate(24px,-4px);transition:transform .3s ease-out,background .3s}
.star{position:absolute;width:3px;height:3px;border-radius:50%;background:#fff;opacity:0;transition:opacity .3s}.s1{left:12px;top:9px}.s2{left:22px;top:21px}
.dark{--tr:#1b2a4a;--kn:#e4e7ee;--glow:transparent;--bite:#1b2a4a;--card:#171717;--ink:#f2f2f0}
.dark .kn{transform:translateX(32px);box-shadow:none}.dark .bite{transform:translate(0,0)}.dark .star{opacity:1}
.pv{width:200px;padding:12px 14px;border-radius:12px;background:var(--card);color:var(--ink);display:grid;border:1px solid var(--line);transition:background .3s,color .3s;font-size:13px}.pv span{font-size:12px;opacity:.7}
.sw:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
@media (prefers-reduced-motion:reduce){.sw,.kn,.bite,.star,.pv{transition:none}}
</style></head>
<body><div class="stage">
<div class="bx" id="bx"><button class="sw" id="sw" type="button" role="switch" aria-checked="false" aria-label="Dark mode"><span class="star s1"></span><span class="star s2"></span><span class="kn"><i class="bite"></i></span></button><div class="pv"><b>Preview</b><span id="lb">Light theme</span></div></div>
</div>
<script>
const sw=document.getElementById('sw'),bx=document.getElementById('bx'),lb=document.getElementById('lb');
sw.addEventListener('click',()=>{const d=sw.getAttribute('aria-checked')!=='true';sw.setAttribute('aria-checked',String(d));bx.classList.toggle('dark',d);lb.textContent=d?'Dark theme':'Light theme';});
</script>
</body></html>