AI CSS Animations

Light Bulb Toggle Switch

A switch wired to a light bulb: flip it and the glass fills with warm light, the filament glows and five rays draw outward one after another. The bulb is inline SVG, the glow a drop-shadow.

Also known as: light bulb switch css, lamp on off toggle, bulb toggle animation.

HTML

<label class="tgl-light-bulb-toggle">
  <input type="checkbox" role="switch" aria-label="Light">
  <span class="tgl-light-bulb-toggle-track" aria-hidden="true"></span>
  <svg class="tgl-light-bulb-toggle-bulb" viewBox="0 0 48 48" aria-hidden="true">
    <path class="tgl-light-bulb-toggle-glass" d="M24 11a9 9 0 0 0-5.2 16.35c.9.65 1.2 1.55 1.2 2.65V31h8v-1c0-1.1.3-2 1.2-2.65A9 9 0 0 0 24 11z"/>
    <path class="tgl-light-bulb-toggle-filament" d="M21.5 30v-4.5l-1.6-4M26.5 30v-4.5l1.6-4M19.9 21.5c1.4-1.3 2.7 1.3 4.1 0s2.7 1.3 4.1 0"/>
    <path d="M20 34.5h8M21.5 38h5"/>
    <path class="tgl-light-bulb-toggle-ray" style="--i: 0" d="M12 20H8.5"/>
    <path class="tgl-light-bulb-toggle-ray" style="--i: 1" d="M15.5 11.5 13 9"/>
    <path class="tgl-light-bulb-toggle-ray" style="--i: 2" d="M24 8V4.5"/>
    <path class="tgl-light-bulb-toggle-ray" style="--i: 3" d="M32.5 11.5 35 9"/>
    <path class="tgl-light-bulb-toggle-ray" style="--i: 4" d="M36 20h3.5"/>
  </svg>
</label>

CSS

.tgl-light-bulb-toggle {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  gap: 0.4em;
}
.tgl-light-bulb-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-light-bulb-toggle-track {
  position: relative;
  display: block;
  width: 3.2em;
  height: 1.8em;
  border-radius: 0.9em;
  background: color-mix(in srgb, currentColor 18%, transparent);
  transition: background-color 0.3s ease;
}
.tgl-light-bulb-toggle-track::before {
  content: "";
  position: absolute;
  top: 0.2em;
  left: 0.2em;
  width: 1.4em;
  height: 1.4em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0.08em 0.2em rgba(0, 0, 0, 0.3);
  transition: transform 0.35s cubic-bezier(0.4, 1.5, 0.5, 1);
}
.tgl-light-bulb-toggle-bulb {
  display: block;
  width: 3.4em;
  height: 3.4em;
  overflow: visible;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: filter 0.4s ease;
}
.tgl-light-bulb-toggle-glass {
  fill: color-mix(in srgb, currentColor 7%, transparent);
  transition: fill 0.3s ease;
}
.tgl-light-bulb-toggle-filament {
  stroke: color-mix(in srgb, currentColor 45%, transparent);
  stroke-width: 1.2;
  transition: stroke 0.3s ease;
}
.tgl-light-bulb-toggle-ray {
  stroke: #facc15;
  stroke-width: 2;
  stroke-dasharray: 4 6;
  stroke-dashoffset: 5;
  transition: stroke-dashoffset 0.25s ease;
}
.tgl-light-bulb-toggle input:checked + .tgl-light-bulb-toggle-track { background: var(--c); }
.tgl-light-bulb-toggle input:checked + .tgl-light-bulb-toggle-track::before { transform: translateX(1.4em); }
.tgl-light-bulb-toggle input:checked ~ .tgl-light-bulb-toggle-bulb { filter: drop-shadow(0 0 0.35em rgba(250, 204, 21, 0.75)); }
.tgl-light-bulb-toggle input:checked ~ .tgl-light-bulb-toggle-bulb .tgl-light-bulb-toggle-glass { fill: #fde047; }
.tgl-light-bulb-toggle input:checked ~ .tgl-light-bulb-toggle-bulb .tgl-light-bulb-toggle-filament { stroke: #b45309; }
.tgl-light-bulb-toggle input:checked ~ .tgl-light-bulb-toggle-bulb .tgl-light-bulb-toggle-ray {
  stroke-dashoffset: 0;
  transition-delay: calc(0.12s + var(--i) * 0.05s);
}
.tgl-light-bulb-toggle input:focus-visible + .tgl-light-bulb-toggle-track {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-light-bulb-toggle, .tgl-light-bulb-toggle *, .tgl-light-bulb-toggle::before, .tgl-light-bulb-toggle::after, .tgl-light-bulb-toggle *::before, .tgl-light-bulb-toggle *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --tgl-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --tgl-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 24px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar toggle switches