AI CSS Animations

Candle Toggle Switch

A candle you click to light: the flame grows from the wick and flickers inside a warm glow. Blow it out and the flame shrinks away while the wick's ember slowly cools from orange to dark.

Also known as: candle light switch css, flame toggle, css candle animation.

HTML

<label class="tgl-candle-toggle">
  <input type="checkbox" role="switch" aria-label="Candle">
  <span class="tgl-candle-toggle-candle" aria-hidden="true">
    <span class="tgl-candle-toggle-glow"></span>
    <span class="tgl-candle-toggle-flame"></span>
    <span class="tgl-candle-toggle-wick"></span>
    <span class="tgl-candle-toggle-wax"></span>
  </span>
</label>

CSS

.tgl-candle-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;
}
.tgl-candle-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-candle-toggle-candle {
  position: relative;
  display: block;
  width: 3.4em;
  height: 5.8em;
}
.tgl-candle-toggle-candle > span { position: absolute; }
/* the dish */
.tgl-candle-toggle-candle::after {
  content: "";
  position: absolute;
  left: calc(50% - 1.3em);
  bottom: 0;
  width: 2.6em;
  height: 0.5em;
  border-radius: 50%;
  background: color-mix(in srgb, currentColor 28%, transparent);
  box-shadow: inset 0 -0.08em 0 color-mix(in srgb, currentColor 25%, transparent);
}
.tgl-candle-toggle-glow {
  left: calc(50% - 1.9em);
  top: -0.5em;
  width: 3.8em;
  height: 3.8em;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(253, 224, 71, 0.5), rgba(253, 224, 71, 0) 65%);
  opacity: 0;
  transition: opacity 0.4s ease;
}
.tgl-candle-toggle-wax {
  left: calc(50% - 0.7em);
  bottom: 0.25em;
  width: 1.4em;
  height: 3em;
  border-radius: 0.2em 0.2em 0.3em 0.3em;
  background: linear-gradient(90deg,
    color-mix(in srgb, var(--c) 70%, #000),
    var(--c) 30%,
    color-mix(in srgb, var(--c) 45%, #fff) 45%,
    var(--c) 65%,
    color-mix(in srgb, var(--c) 70%, #000));
}
.tgl-candle-toggle-wax::before,
.tgl-candle-toggle-wax::after {
  content: "";
  position: absolute;
  background: color-mix(in srgb, var(--c) 50%, #fff);
}
/* the top of the candle, and a drip */
.tgl-candle-toggle-wax::before {
  left: 0;
  right: 0;
  top: -0.18em;
  height: 0.36em;
  border-radius: 50%;
}
.tgl-candle-toggle-wax::after {
  left: 0.25em;
  top: 0;
  width: 0.28em;
  height: 0.9em;
  border-radius: 0 0 0.14em 0.14em;
}
.tgl-candle-toggle-wick {
  left: calc(50% - 0.045em);
  bottom: 3.3em;
  z-index: 1;
  width: 0.09em;
  height: 0.38em;
  border-radius: 0.04em;
  background: #292524;
  transition: background-color 1.8s ease, box-shadow 1.8s ease;
}
.tgl-candle-toggle-flame {
  left: calc(50% - 0.3em);
  bottom: 3.5em;
  z-index: 2;
  width: 0.6em;
  height: 1.3em;
  transform-origin: 50% 100%;
  transform: scale(0);
  opacity: 0;
  transition: transform 0.25s ease-in, opacity 0.25s ease;
}
/* a teardrop: a square with one sharp corner, turned to point up */
.tgl-candle-toggle-flame::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0.3em;
  width: 0.6em;
  height: 0.6em;
  border-radius: 50% 0 50% 50%;
  background: radial-gradient(circle at 38% 62%, #fff 0 15%, #fef08a 32%, #fb923c 62%, #f97316 85%);
  box-shadow: 0 0 0.35em 0.08em rgba(251, 146, 60, 0.6);
  transform: scale(1, 1.45) rotate(-45deg);
  animation: tgl-candle-toggle-flicker 1.6s ease-in-out infinite;
}
.tgl-candle-toggle input:checked + .tgl-candle-toggle-candle .tgl-candle-toggle-glow {
  opacity: 1;
  animation: tgl-candle-toggle-breathe 2.4s ease-in-out infinite;
}
.tgl-candle-toggle input:checked + .tgl-candle-toggle-candle .tgl-candle-toggle-flame {
  transform: scale(1);
  opacity: 1;
  transition: transform 0.5s cubic-bezier(0.3, 1.6, 0.5, 1) 0.1s, opacity 0.2s ease 0.1s;
}
.tgl-candle-toggle input:checked + .tgl-candle-toggle-candle .tgl-candle-toggle-wick {
  background: #7c2d12;
  box-shadow: 0 -0.05em 0.15em #fb923c;
  transition-duration: 0.2s;
}
@keyframes tgl-candle-toggle-flicker {
  0%, 100% { transform: scale(1, 1.45) rotate(-45deg); }
  30% { transform: scale(0.94, 1.55) rotate(-42deg); }
  55% { transform: scale(1.04, 1.38) rotate(-48deg); }
  80% { transform: scale(0.97, 1.5) rotate(-44deg); }
}
@keyframes tgl-candle-toggle-breathe {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.75; }
}
.tgl-candle-toggle input:focus-visible + .tgl-candle-toggle-candle .tgl-candle-toggle-wax {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-candle-toggle, .tgl-candle-toggle *, .tgl-candle-toggle::before, .tgl-candle-toggle::after, .tgl-candle-toggle *::before, .tgl-candle-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