Animated Dark Mode Toggle
A sun-and-moon switch that flips color tokens and remembers the theme, with a knob that eases between day and night.

An animated dark mode toggle is the control people look for when a site offers two themes and the switch itself should explain the change. The knob slides, the sun’s highlight becomes a moon cutout, and the page swaps a small set of color tokens. The motion is the explanation. The tokens are the theme.
This pattern is the interactive cousin of transforming color themes on scroll. Scroll themes change as you read. This one changes because someone asked.
The tokens
Keep the theme as custom properties on body. Components read var(--bg), var(--fg), var(--card), and var(--line). Adding a theme means replacing those four values, not hunting hex codes through the stylesheet.
body {
background: var(--bg);
color: var(--fg);
transition: background .5s ease, color .5s ease;
--bg: #f6f1e8;
--fg: #1c1917;
--card: #fffdf8;
--line: #e7dfd4;
}
body[data-theme="dark"] {
--bg: #14120f;
--fg: #f6f1e8;
--card: #1f1b17;
--line: #3a342c;
}
Transition background and color only. A universal transition: all will also animate layout, which feels like the page is melting.
The sun and the moon
The track is a 92 by 48 pill. The knob is a 40px disc. Light mode paints it gold with an inset shadow that reads as the sun’s lower rim. Dark mode translates it 44px and uses a hard inset shadow as the moon’s bite.
.knob {
width: 40px;
height: 40px;
border-radius: 50%;
background: #f6c453;
transition: transform .45s cubic-bezier(.2,.8,.2,1), background .45s, box-shadow .45s;
box-shadow: inset -6px -4px 0 2px #f59e0b;
}
body[data-theme="dark"] .knob {
transform: translateX(44px);
background: #e7e5e4;
box-shadow: inset -10px -4px 0 0 #14120f;
}
The easing is a soft ease-out. A bounce on a theme switch feels like a toy and makes the knob overshoot the track.
Remember the choice
function apply(dark) {
document.body.dataset.theme = dark ? "dark" : "light";
button.setAttribute("aria-pressed", String(dark));
localStorage.setItem("dd-theme", dark ? "dark" : "light");
}
const saved = localStorage.getItem("dd-theme");
apply(saved ? saved === "dark" : matchMedia("(prefers-color-scheme: dark)").matches);
First visit follows the operating system. After a click, the saved value wins. Update the button label too: “Switch to dark theme” or “Switch to light theme”. aria-pressed tells assistive tech the current state.
Contrast in both themes
Pick the pairs before you animate them. The demo’s light text is #1c1917 on #f6f1e8 and #fffdf8. The dark text is #f6f1e8 on #14120f and #1f1b17. Muted copy uses a gray that still clears 4.5:1 on both cards. If a muted color only works in one theme, it is not a token yet.
Questions about dark mode toggles
How do I animate a dark mode toggle with CSS?
Slide a round knob with transform: translateX and cross-fade its box-shadow so a sun disc becomes a moon. Drive the page from data-theme on body and custom properties, and transition background and color over about half a second.
Should the site start in dark mode?
Start from prefers-color-scheme until the person chooses. Store that choice in localStorage and read it before paint if you can. This demo applies the theme as soon as the module runs, which is early enough for a snippet and late enough that a production page may flash. For a real site, set the attribute from a tiny inline script in the document head.
Is a theme toggle only a color change?
The control can be delightful and the theme still has to be readable. Animate the knob. Do not animate every border, shadow, and image filter on the page. Readers came for the content in the new colors, not for a second scene change.