AI CSS Animations

Planet Orbit Toggle

A ringed planet crosses a starry track and takes on the accent color, then a small moon starts orbiting it, passing in front and behind. Two alternating ease-in-out loops trace the ellipse.

Also known as: space toggle switch, planet switch css, saturn toggle.

HTML

<label class="tgl-planet-orbit-toggle">
  <input type="checkbox" role="switch" aria-label="Orbit">
  <span class="tgl-planet-orbit-toggle-track" aria-hidden="true">
    <span class="tgl-planet-orbit-toggle-planet">
      <span class="tgl-planet-orbit-toggle-ring-back"></span>
      <span class="tgl-planet-orbit-toggle-ball"></span>
      <span class="tgl-planet-orbit-toggle-ring-front"></span>
      <span class="tgl-planet-orbit-toggle-orbit">
        <span class="tgl-planet-orbit-toggle-moon"></span>
      </span>
    </span>
  </span>
</label>

CSS

.tgl-planet-orbit-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-planet-orbit-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-planet-orbit-toggle-track {
  position: relative;
  display: block;
  width: 7em;
  height: 2.8em;
  border-radius: 1.4em;
  background: color-mix(in srgb, var(--c) 15%, #0b1026);
  box-shadow: inset 0 0.1em 0.3em rgba(0, 0, 0, 0.5), inset 0 0 0 1px rgba(255, 255, 255, 0.1);
}
.tgl-planet-orbit-toggle-track::before {
  content: "";
  position: absolute;
  top: 0.45em;
  left: 1em;
  width: 0.1em;
  height: 0.1em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 1.2em 0.95em 0 rgba(255, 255, 255, 0.6), 2.3em 0.2em 0 #fff, 3.4em 1.5em 0 rgba(255, 255, 255, 0.7), 4.6em 0.35em 0 #fff, 0.4em 1.6em 0 rgba(255, 255, 255, 0.6), 5.3em 1.35em 0 #fff;
  animation: tgl-planet-orbit-toggle-twinkle 3s ease-in-out infinite;
}
.tgl-planet-orbit-toggle-planet {
  position: absolute;
  top: 0.3em;
  left: 0.3em;
  z-index: 0;
  width: 2.2em;
  height: 2.2em;
  transition: transform 0.7s cubic-bezier(0.5, 0, 0.25, 1);
}
.tgl-planet-orbit-toggle-planet > span { position: absolute; }
.tgl-planet-orbit-toggle-ball {
  left: 0.35em;
  top: 0.35em;
  z-index: 2;
  width: 1.5em;
  height: 1.5em;
  border-radius: 50%;
  overflow: hidden;
  background:
    repeating-linear-gradient(160deg, transparent 0 0.22em, rgba(255, 255, 255, 0.14) 0 0.34em),
    linear-gradient(160deg, #e2e8f0, #64748b);
  box-shadow: inset -0.22em -0.16em 0.3em rgba(0, 0, 0, 0.4);
}
.tgl-planet-orbit-toggle-ball::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    repeating-linear-gradient(160deg, transparent 0 0.22em, rgba(255, 255, 255, 0.16) 0 0.34em),
    linear-gradient(160deg, color-mix(in srgb, var(--c) 40%, #fff), var(--c) 55%, color-mix(in srgb, var(--c) 55%, #000));
  box-shadow: inset -0.22em -0.16em 0.3em rgba(0, 0, 0, 0.4);
  opacity: 0;
  transition: opacity 0.5s ease;
}
/* the ring is drawn twice: the back half under the planet, the front half over it */
.tgl-planet-orbit-toggle-ring-back,
.tgl-planet-orbit-toggle-ring-front {
  left: calc(50% - 1.15em);
  top: calc(50% - 0.3em);
  box-sizing: border-box;
  width: 2.3em;
  height: 0.6em;
  border: 0.13em solid #cbd5e1;
  border-radius: 50%;
  transform: rotate(-18deg);
  transition: border-color 0.5s ease;
}
.tgl-planet-orbit-toggle-ring-back { z-index: 1; }
.tgl-planet-orbit-toggle-ring-front {
  z-index: 3;
  clip-path: inset(50% 0 0 0);
}
.tgl-planet-orbit-toggle-orbit {
  left: 50%;
  top: 50%;
  z-index: 1;
  opacity: 0;
  transition: opacity 0.4s ease;
}
.tgl-planet-orbit-toggle-moon {
  position: absolute;
  left: -0.17em;
  top: -0.17em;
  width: 0.34em;
  height: 0.34em;
  border-radius: 50%;
  background: #e2e8f0;
  box-shadow: inset -0.06em -0.05em 0.08em rgba(0, 0, 0, 0.35);
}
.tgl-planet-orbit-toggle input:checked + .tgl-planet-orbit-toggle-track .tgl-planet-orbit-toggle-planet { transform: translateX(4.2em); }
.tgl-planet-orbit-toggle input:checked + .tgl-planet-orbit-toggle-track .tgl-planet-orbit-toggle-ball::before { opacity: 1; }
.tgl-planet-orbit-toggle input:checked + .tgl-planet-orbit-toggle-track .tgl-planet-orbit-toggle-ring-back,
.tgl-planet-orbit-toggle input:checked + .tgl-planet-orbit-toggle-track .tgl-planet-orbit-toggle-ring-front {
  border-color: color-mix(in srgb, var(--c) 35%, #fde68a);
}
/* y and front/back on the wrapper, x on the moon: together they trace an ellipse */
.tgl-planet-orbit-toggle input:checked + .tgl-planet-orbit-toggle-track .tgl-planet-orbit-toggle-orbit {
  opacity: 1;
  transition-delay: 0.5s;
  animation:
    tgl-planet-orbit-toggle-y 2s ease-in-out -1s infinite alternate,
    tgl-planet-orbit-toggle-z 4s linear infinite;
}
.tgl-planet-orbit-toggle input:checked + .tgl-planet-orbit-toggle-track .tgl-planet-orbit-toggle-moon {
  animation: tgl-planet-orbit-toggle-x 2s ease-in-out infinite alternate;
}
@keyframes tgl-planet-orbit-toggle-x {
  from { transform: translateX(-1.25em); }
  to { transform: translateX(1.25em); }
}
@keyframes tgl-planet-orbit-toggle-y {
  from { transform: translateY(-0.42em); }
  to { transform: translateY(0.42em); }
}
@keyframes tgl-planet-orbit-toggle-z {
  0%, 49.9% { z-index: 4; }
  50%, 100% { z-index: 1; }
}
@keyframes tgl-planet-orbit-toggle-twinkle {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.tgl-planet-orbit-toggle input:focus-visible + .tgl-planet-orbit-toggle-track {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}

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