AI CSS Animations

Car Toggle Switch

A little car on a road: switch it on and it pulls away, pitching back as it accelerates and dipping as it brakes, then cruises with spinning wheels, a scrolling lane line and its headlight on.

Also known as: driving car toggle css, car switch animation, road toggle switch.

HTML

<label class="tgl-car-toggle">
  <input type="checkbox" role="switch" aria-label="Drive">
  <span class="tgl-car-toggle-track" aria-hidden="true">
    <span class="tgl-car-toggle-lane"></span>
    <span class="tgl-car-toggle-car">
      <span class="tgl-car-toggle-beam"></span>
      <svg viewBox="0 0 28 16">
        <path class="tgl-car-toggle-body" d="M1.5 11V8.6c0-.9.6-1.7 1.5-1.9L7 6l3-3.4c.5-.6 1.3-1 2.1-1h5.6c.8 0 1.6.4 2.1 1L22.5 6l2.8.7c.9.2 1.5 1 1.5 1.9V11c0 .6-.4 1-1 1H2.5c-.6 0-1-.4-1-1z"/>
        <path class="tgl-car-toggle-glass" d="M10.8 3.2 8.6 5.9h5V3h-2.1c-.3 0-.6.1-.7.2zM14.8 3v2.9h5.3l-2.2-2.6c-.2-.2-.5-.3-.8-.3z"/>
      </svg>
      <span class="tgl-car-toggle-wheel"></span>
      <span class="tgl-car-toggle-wheel"></span>
    </span>
  </span>
</label>

CSS

.tgl-car-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-car-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-car-toggle-track {
  position: relative;
  display: block;
  width: 6.4em;
  height: 2.6em;
  border-radius: 1.3em;
  overflow: hidden;
  isolation: isolate;
  background: color-mix(in srgb, currentColor 12%, transparent);
  transition: background-color 0.4s ease;
}
/* the road */
.tgl-car-toggle-track::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 0.72em;
  background: #374151;
}
.tgl-car-toggle-lane {
  position: absolute;
  left: 0;
  right: -1em;
  top: 2.22em;
  height: 0.09em;
  background: repeating-linear-gradient(90deg, #fde047 0 0.5em, transparent 0 1em);
}
.tgl-car-toggle-car {
  position: absolute;
  left: 0.35em;
  bottom: 0.45em;
  width: 2.7em;
  height: 1.55em;
  transition: transform 0.9s cubic-bezier(0.55, 0, 0.3, 1);
}
.tgl-car-toggle-car svg {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  transform-origin: 50% 100%;
}
.tgl-car-toggle-body { fill: var(--c); }
.tgl-car-toggle-glass { fill: rgba(224, 242, 254, 0.9); }
.tgl-car-toggle-wheel {
  position: absolute;
  bottom: 0.14em;
  left: 0.473em;
  width: 0.5em;
  height: 0.5em;
  border-radius: 50%;
  background:
    radial-gradient(circle, #d1d5db 0 0.07em, transparent 0.08em),
    repeating-conic-gradient(#4b5563 0 45deg, #111827 0 90deg);
  box-shadow: 0 0 0 0.05em #111827;
  transform: rotate(-540deg);
  transition: transform 0.9s cubic-bezier(0.55, 0, 0.3, 1);
}
.tgl-car-toggle-wheel + .tgl-car-toggle-wheel { left: 1.775em; }
.tgl-car-toggle-beam {
  position: absolute;
  left: 2.55em;
  top: 0.55em;
  width: 1.4em;
  height: 0.8em;
  clip-path: polygon(0 35%, 100% 0, 100% 100%, 0 65%);
  background: linear-gradient(90deg, rgba(253, 224, 71, 0.75), rgba(253, 224, 71, 0));
  opacity: 0;
  transition: opacity 0.3s ease;
}
.tgl-car-toggle input:checked + .tgl-car-toggle-track { background: color-mix(in srgb, var(--c) 22%, transparent); }
.tgl-car-toggle input:checked + .tgl-car-toggle-track .tgl-car-toggle-car { transform: translateX(3em); }
.tgl-car-toggle input:checked + .tgl-car-toggle-track .tgl-car-toggle-car svg {
  animation:
    tgl-car-toggle-pitch 0.9s ease-in-out,
    tgl-car-toggle-bob 0.3s ease-in-out 0.9s infinite alternate;
}
.tgl-car-toggle input:checked + .tgl-car-toggle-track .tgl-car-toggle-wheel {
  transform: rotate(0deg);
  animation: tgl-car-toggle-spin 0.35s linear 0.9s infinite;
}
.tgl-car-toggle input:checked + .tgl-car-toggle-track .tgl-car-toggle-lane {
  animation: tgl-car-toggle-road 0.45s linear 0.6s infinite;
}
.tgl-car-toggle input:checked + .tgl-car-toggle-track .tgl-car-toggle-beam {
  opacity: 1;
  transition-delay: 0.3s;
}
@keyframes tgl-car-toggle-pitch {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-4deg); }
  65% { transform: rotate(-1deg); }
  82% { transform: rotate(3deg); }
}
@keyframes tgl-car-toggle-bob {
  from { transform: translateY(0); }
  to { transform: translateY(-0.04em); }
}
@keyframes tgl-car-toggle-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
@keyframes tgl-car-toggle-road {
  from { transform: translateX(0); }
  to { transform: translateX(-1em); }
}
.tgl-car-toggle input:focus-visible + .tgl-car-toggle-track {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}

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