Skip to content
IndexRequest a UIAboutContact

Sun-Moon Theme Toggle

A theme switch whose sun glows and then bites into a moon as the knob slides and the track darkens.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
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

HTML
<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>

Related patterns

Toggle Switch

↗
Toggle SwitchInputs & Forms

Hamburger to Close Morph

↗
Hamburger to Close MorphMicro Interactions

Animated Checkmark Button

↗
Animated Checkmark ButtonMicro Interactions