Skip to content

Animated BG Swap

A copy-paste theme toggle component in pure HTML & CSS. Zero dependencies, framework-agnostic, MIT-licensed.

Theme ToggleHTMLCSSany framework

Day

Night

Copy into your project

HTML
<!-- Animated BG Swap — full-section day/night switcher (toggle .is-dark) -->
<div class="nuda-bgswap">
  <div class="nuda-bgswap__layer nuda-bgswap__layer--light">
    <div class="nuda-bgswap__sun"></div>
    <p>Day</p>
  </div>
  <div class="nuda-bgswap__layer nuda-bgswap__layer--dark">
    <div class="nuda-bgswap__moon"></div>
    <div class="nuda-bgswap__star" style="top: 20%; left: 30%"></div>
    <div class="nuda-bgswap__star" style="top: 40%; left: 70%"></div>
    <div class="nuda-bgswap__star" style="top: 60%; left: 20%"></div>
    <p>Night</p>
  </div>
</div>
CSS
/* Animated BG Swap
   Two stacked layers cross-fade to flip a hero section between day/night.
   Customize: gradients, content per layer. */

.nuda-bgswap {
  position: relative;
  width: 100%;
  max-width: 320px;
  aspect-ratio: 16 / 10;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
}

.nuda-bgswap__layer {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.8s ease;
}

.nuda-bgswap__layer p {
  font: 700 1rem ui-sans-serif, system-ui, sans-serif;
  margin: 0;
}

.nuda-bgswap__layer--light {
  background: linear-gradient(180deg, #fde68a 0%, #fbbf24 50%, #f59e0b 100%);
  color: #92400e;
}

.nuda-bgswap__layer--dark {
  background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
  color: #e4ff54;
  opacity: 0;
}

.nuda-bgswap:hover .nuda-bgswap__layer--light,
.nuda-bgswap.is-dark .nuda-bgswap__layer--light { opacity: 0; }

.nuda-bgswap:hover .nuda-bgswap__layer--dark,
.nuda-bgswap.is-dark .nuda-bgswap__layer--dark  { opacity: 1; }

.nuda-bgswap__sun {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 16px #fff, 0 0 24px #fbbf24;
  animation: nuda-bs-sun 3s ease-in-out infinite;
}

.nuda-bgswap__moon {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 40%, #e4ff54, #a8c93a);
  box-shadow: 0 0 12px rgba(228, 255, 84, 0.6);
}

.nuda-bgswap__star {
  position: absolute;
  width: 2px;
  height: 2px;
  background: #fff;
  border-radius: 50%;
  animation: nuda-bs-twink 2s ease-in-out infinite;
}

.nuda-bgswap__star:nth-child(3) { animation-delay: 0.5s; }
.nuda-bgswap__star:nth-child(4) { animation-delay: 1s; }

@keyframes nuda-bs-sun {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.1); }
}

@keyframes nuda-bs-twink {
  0%, 100% { opacity: 0.3; }
  50%      { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .nuda-bgswap__sun,
  .nuda-bgswap__star { animation: none; }
}

How to use Animated BG Swap

Paste the HTML where you need it and the CSS into a global stylesheet (or a <style> tag). Every class is prefixed nuda- so it never collides with Tailwind or your own styles. Tweak the CSS custom properties to match your design system.

Works in React, Vue, Svelte, Astro, Next.js, Nuxt, Laravel Blade, Django, Rails — or a single .html file. No npm install, no build step.

More theme toggle components

← Browse all NudaUI components