Day and Night Theme Toggle
A sun and moon switch. The new theme spreads out from the knob in a circle over the whole page, using the View Transitions API.
- Buttons & switches
- Free
- transition
- Added Sep 14, 2026
<style>
:root { --bg: #0A0D16; --card: #121726; --text: #E7EBF5; --sub: #8A93A8; --accent: #8FA8FF; color-scheme: dark; }
[data-theme="light"] { --bg: #EAF1FA; --card: #FFFFFF; --text: #0E1320; --sub: #6B7385; --accent: #FF9F1C; color-scheme: light; }
/* the browser snapshots old and new page, we only animate the circle */
::view-transition-old(root), ::view-transition-new(root) { animation: none; mix-blend-mode: normal; }
.settings { box-sizing: border-box; width: min(380px, 92vw); padding: 26px; border-radius: 26px; background: var(--card); text-align: center; }
.settings p { margin: 10px 0 0; color: var(--sub); font-size: 13px; }
.theme-toggle { position: relative; width: 148px; height: 64px; border: 0; padding: 0; border-radius: 32px; cursor: pointer; overflow: hidden;
background: linear-gradient(#161C3C, #0A0E22); transition: background .4s ease; }
[data-theme="light"] .theme-toggle { background: linear-gradient(#6EBEFF, #BEE6FF); }
.theme-toggle .knob { position: absolute; left: 7px; top: 7px; width: 50px; height: 50px; border-radius: 50%; background: #E8ECF6;
box-shadow: inset -6px -6px 0 #C5CCDA, 0 3px 8px rgba(0,0,0,.3);
transition: transform .7s cubic-bezier(.45, 1.2, .5, 1), background .35s ease, box-shadow .35s ease; }
[data-theme="light"] .theme-toggle .knob { transform: translateX(84px); background: #FFC94D; box-shadow: 0 0 0 6px rgba(255,201,77,.35), 0 3px 8px rgba(0,0,0,.2); }
.theme-toggle .stars, .theme-toggle .clouds { position: absolute; inset: 0; transition: opacity .35s ease, transform .5s ease; }
.theme-toggle .stars { background: radial-gradient(1.5px 1.5px at 80px 18px, #fff, transparent), radial-gradient(1px 1px at 104px 40px, #fff, transparent), radial-gradient(1.5px 1.5px at 124px 22px, #fff, transparent); }
.theme-toggle .clouds { opacity: 0; transform: translateX(-30px);
background: radial-gradient(circle at 26px 44px, #fff 10px, transparent 11px), radial-gradient(circle at 42px 38px, #fff 13px, transparent 14px), radial-gradient(circle at 58px 46px, #fff 9px, transparent 10px); }
[data-theme="light"] .theme-toggle .stars { opacity: 0; }
[data-theme="light"] .theme-toggle .clouds { opacity: 1; transform: none; }
@media (prefers-reduced-motion: reduce) { .theme-toggle .knob { transition: none; } }
</style>
<div class="settings">
<button class="theme-toggle" aria-label="Switch theme" aria-pressed="false"><span class="stars"></span><span class="clouds"></span><span class="knob"></span></button>
<p class="mode">Dark mode</p>
</div>
<script>
const toggle = document.querySelector('.theme-toggle');
const knob = toggle.querySelector('.knob');
const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
function flip() {
const light = document.documentElement.dataset.theme !== 'light';
document.documentElement.dataset.theme = light ? 'light' : 'dark';
toggle.setAttribute('aria-pressed', light);
document.querySelector('.mode').textContent = light ? 'Light mode' : 'Dark mode';
}
const knobCenter = () => { const r = knob.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; };
const EASE = 'cubic-bezier(.77, 0, .175, 1)';
toggle.onclick = async () => {
if (!document.startViewTransition || reduce) return flip();
const { x, y } = knobCenter();
const r = Math.hypot(innerWidth, innerHeight);
await document.startViewTransition(flip).ready;
document.documentElement.animate(
{ clipPath: [`circle(0 at ${x}px ${y}px)`,
`circle(${r}px at ${x}px ${y}px)`] },
{ duration: 700, easing: EASE,
pseudoElement: '::view-transition-new(root)' });
};
</script>
Click the switch a few times.
What you can change
Change a number, save, and reload the page to see the difference.
700durationHow long the circle takes to cover the page, in milliseconds.
EASEcubic-bezierThe curve of the reveal. This one is slow at both ends and fast in the middle.
--bg, --card, --textin :rootYour theme colors. Use them in your own CSS and the whole site switches.
Add it to your site
Put the switch in your header. Use the CSS variables across your site.
CSS
Paste into your stylesheet, or inside a <style> tag in <head>.
:root { --bg: #0A0D16; --card: #121726; --text: #E7EBF5; --sub: #8A93A8; --accent: #8FA8FF; color-scheme: dark; } [data-theme="light"] { --bg: #EAF1FA; --card: #FFFFFF; --text: #0E1320; --sub: #6B7385; --accent: #FF9F1C; color-scheme: light; } /* the browser snapshots old and new page, we only animate the circle */ ::view-transition-old(root), ::view-transition-new(root) { animation: none; mix-blend-mode: normal; } .settings { box-sizing: border-box; width: min(380px, 92vw); padding: 26px; border-radius: 26px; background: var(--card); text-align: center; } .settings p { margin: 10px 0 0; color: var(--sub); font-size: 13px; } .theme-toggle { position: relative; width: 148px; height: 64px; border: 0; padding: 0; border-radius: 32px; cursor: pointer; overflow: hidden; background: linear-gradient(#161C3C, #0A0E22); transition: background .4s ease; } [data-theme="light"] .theme-toggle { background: linear-gradient(#6EBEFF, #BEE6FF); } .theme-toggle .knob { position: absolute; left: 7px; top: 7px; width: 50px; height: 50px; border-radius: 50%; background: #E8ECF6; box-shadow: inset -6px -6px 0 #C5CCDA, 0 3px 8px rgba(0,0,0,.3); transition: transform .7s cubic-bezier(.45, 1.2, .5, 1), background .35s ease, box-shadow .35s ease; } [data-theme="light"] .theme-toggle .knob { transform: translateX(84px); background: #FFC94D; box-shadow: 0 0 0 6px rgba(255,201,77,.35), 0 3px 8px rgba(0,0,0,.2); } .theme-toggle .stars, .theme-toggle .clouds { position: absolute; inset: 0; transition: opacity .35s ease, transform .5s ease; } .theme-toggle .stars { background: radial-gradient(1.5px 1.5px at 80px 18px, #fff, transparent), radial-gradient(1px 1px at 104px 40px, #fff, transparent), radial-gradient(1.5px 1.5px at 124px 22px, #fff, transparent); } .theme-toggle .clouds { opacity: 0; transform: translateX(-30px); background: radial-gradient(circle at 26px 44px, #fff 10px, transparent 11px), radial-gradient(circle at 42px 38px, #fff 13px, transparent 14px), radial-gradient(circle at 58px 46px, #fff 9px, transparent 10px); } [data-theme="light"] .theme-toggle .stars { opacity: 0; } [data-theme="light"] .theme-toggle .clouds { opacity: 1; transform: none; } @media (prefers-reduced-motion: reduce) { .theme-toggle .knob { transition: none; } }HTML
Put it where the effect should appear on your page.
<div class="settings"> <button class="theme-toggle" aria-label="Switch theme" aria-pressed="false"><span class="stars"></span><span class="clouds"></span><span class="knob"></span></button> <p class="mode">Dark mode</p> </div>JavaScript
Paste right before the closing </body> tag, inside <script></script>.
const toggle = document.querySelector('.theme-toggle'); const knob = toggle.querySelector('.knob'); const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches; function flip() { const light = document.documentElement.dataset.theme !== 'light'; document.documentElement.dataset.theme = light ? 'light' : 'dark'; toggle.setAttribute('aria-pressed', light); document.querySelector('.mode').textContent = light ? 'Light mode' : 'Dark mode'; } const knobCenter = () => { const r = knob.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; }; const EASE = 'cubic-bezier(.77, 0, .175, 1)'; toggle.onclick = async () => { if (!document.startViewTransition || reduce) return flip(); const { x, y } = knobCenter(); const r = Math.hypot(innerWidth, innerHeight); await document.startViewTransition(flip).ready; document.documentElement.animate( { clipPath: [`circle(0 at ${x}px ${y}px)`, `circle(${r}px at ${x}px ${y}px)`] }, { duration: 700, easing: EASE, pseudoElement: '::view-transition-new(root)' }); };
Good to know
- Works in Chrome and Edge 111+, Safari 18+ and Firefox 144+. Older browsers switch the theme instantly without the circle, nothing breaks.
Tilda, Webflow, WordPress
Press Copy all code at the top and paste it into one HTML block.
Add block, Other, T123 HTML code. Paste and save. Check the published page, the editor preview does not run scripts.
Add panel, Embed. Paste, then Save & Close. Custom code runs on the published site.
Add block, Custom HTML. Paste and press Update.



