Skip to content

Theme Switch

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

Theme ToggleHTMLCSSany framework

Copy into your project

HTML
<!-- Theme Switch — labelled checkbox with sky→night gradient + star reveal -->
<label class="nuda-tswitch">
  <input type="checkbox" />
  <span class="nuda-tswitch__track">
    <span class="nuda-tswitch__thumb">
      <svg class="nuda-tswitch__sun" viewBox="0 0 24 24" fill="currentColor">
        <circle cx="12" cy="12" r="5" />
      </svg>
      <svg class="nuda-tswitch__moon" viewBox="0 0 24 24" fill="currentColor">
        <path d="M21 13 A 9 9 0 1 1 11 3 A 7 7 0 0 0 21 13 Z" />
      </svg>
    </span>
    <span class="nuda-tswitch__star nuda-tswitch__star--a"></span>
    <span class="nuda-tswitch__star nuda-tswitch__star--b"></span>
  </span>
</label>
CSS
/* Theme Switch
   Day/night switch: gradient track shifts, thumb morphs sun↔moon,
   stars twinkle in dark mode.
   Customize: track gradients, --ts-accent */

.nuda-tswitch {
  --ts-accent: #e4ff54;
  position: relative;
  display: inline-block;
  cursor: pointer;
}

.nuda-tswitch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.nuda-tswitch__track {
  display: flex;
  align-items: center;
  width: 56px;
  height: 28px;
  background: linear-gradient(90deg, #87ceeb 0%, #4a7ba8 100%);
  border-radius: 999px;
  padding: 3px;
  transition: background 0.4s ease;
  position: relative;
  overflow: hidden;
}

.nuda-tswitch input:checked + .nuda-tswitch__track {
  background: linear-gradient(90deg, #1a1a2e 0%, #0a0a18 100%);
}

.nuda-tswitch__thumb {
  position: relative;
  width: 22px;
  height: 22px;
  background: #fafafa;
  border-radius: 50%;
  transform: translateX(0);
  transition:
    transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1),
    background 0.4s;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.nuda-tswitch input:checked + .nuda-tswitch__track .nuda-tswitch__thumb {
  transform: translateX(28px);
  background: var(--ts-accent);
}

.nuda-tswitch__sun,
.nuda-tswitch__moon {
  position: absolute;
  width: 12px;
  height: 12px;
  color: #1a1a2e;
  transition: opacity 0.3s, transform 0.35s;
}

.nuda-tswitch__moon { opacity: 0; transform: scale(0.5); }

.nuda-tswitch input:checked + .nuda-tswitch__track .nuda-tswitch__sun {
  opacity: 0;
  transform: scale(0.5);
}

.nuda-tswitch input:checked + .nuda-tswitch__track .nuda-tswitch__moon {
  opacity: 1;
  transform: scale(1);
}

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

.nuda-tswitch__star--a { top: 6px; right: 10px; }
.nuda-tswitch__star--b { bottom: 6px; right: 16px; }

.nuda-tswitch input:checked + .nuda-tswitch__track .nuda-tswitch__star {
  opacity: 1;
  animation: nuda-ts-twink 2s ease-in-out infinite;
}

.nuda-tswitch input:checked + .nuda-tswitch__track .nuda-tswitch__star--b {
  animation-delay: 0.5s;
}

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

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

How to use Theme Switch

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