Skip to content

Day/Night Scene

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

Theme ToggleHTMLJavaScriptCSSany framework

Copy into your project

HTML
<!-- Day/Night Scene — toggle aria-pressed to sink the sun and reveal stars -->
<button class="nuda-tt2-daynight" type="button" aria-pressed="true" aria-label="Toggle day and night theme">
  <span class="nuda-tt2-daynight__sun" aria-hidden="true"></span>
  <span class="nuda-tt2-daynight__star nuda-tt2-daynight__star--a" aria-hidden="true"></span>
  <span class="nuda-tt2-daynight__star nuda-tt2-daynight__star--b" aria-hidden="true"></span>
  <span class="nuda-tt2-daynight__star nuda-tt2-daynight__star--c" aria-hidden="true"></span>
  <span class="nuda-tt2-daynight__ground" aria-hidden="true"></span>
</button>
JavaScript
/* Day/Night Scene — flips aria-pressed; CSS drives the arc + star fade. */
(function () {
  var btn = document.querySelector('.nuda-tt2-daynight');
  if (!btn) return;
  btn.addEventListener('click', function () {
    var pressed = btn.getAttribute('aria-pressed') === 'true';
    btn.setAttribute('aria-pressed', String(!pressed));
    document.documentElement.classList.toggle('dark', !pressed);
  });
})();
CSS
.nuda-tt2-daynight {
  position: relative;
  display: block;
  width: 100%;
  max-width: 150px;
  aspect-ratio: 14/9;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  padding: 0;
  border: 1px solid rgba(255,255,255,.1);
  background: linear-gradient(180deg,#7ec8e3 0%,#fde68a 65%,#f59e0b 100%);
  transition: background 1s ease;
}

.nuda-tt2-daynight:focus-visible {
  outline: 2px solid #e4ff54;
  outline-offset: 2px;
}

.nuda-tt2-daynight[aria-pressed="true"] {
  background: linear-gradient(180deg,#0a0a1a 0%,#141432 65%,#0a0a18 100%);
}

.nuda-tt2-daynight__sun {
  position: absolute;
  top: 14%;
  left: 18%;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%,#fffbe6,#fbbf24);
  box-shadow: 0 0 16px #fbbf24;
  transform: translate(0,0) scale(1);
  opacity: 1;
  transition: transform 1.1s cubic-bezier(.4,0,.2,1),opacity .8s ease;
  will-change: transform,opacity;
}

.nuda-tt2-daynight[aria-pressed="true"] .nuda-tt2-daynight__sun {
  transform: translate(46px,58px) scale(.4);
  opacity: 0;
}

.nuda-tt2-daynight__star {
  position: absolute;
  width: 2px;
  height: 2px;
  border-radius: 50%;
  background: #fff;
  opacity: 0;
  transition: opacity .8s ease;
}

.nuda-tt2-daynight[aria-pressed="true"] .nuda-tt2-daynight__star {
  opacity: 1;
  animation: nuda-tt2-daynight-twinkle 2.4s ease-in-out infinite;
}

.nuda-tt2-daynight__star--a {
  top: 16%;
  left: 60%;
}

.nuda-tt2-daynight__star--b {
  top: 32%;
  left: 78%;
  animation-delay: .6s;
}

.nuda-tt2-daynight__star--c {
  top: 22%;
  left: 38%;
  animation-delay: 1.2s;
}

.nuda-tt2-daynight__ground {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 22%;
  background: linear-gradient(180deg,rgba(20,40,20,.4),rgba(10,20,10,.7));
}

@keyframes nuda-tt2-daynight-twinkle {
  0%,100% {
    opacity: .4;
  }
  50% {
    opacity: 1;
  }
}

@media (prefers-reduced-motion:reduce) {
  .nuda-tt2-daynight,.nuda-tt2-daynight__sun {
    transition: none;
  }
  .nuda-tt2-daynight__star {
    animation: none;
    opacity: 1;
  }
}

How to use Day/Night Scene

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