AI CSS Animations

Outline Toggle Switch

An outline-only switch: a stroked track and a hollow ring for a knob. Turning it on recolors both strokes and floods the ring with your accent from the inside, with a faint glow around it.

Also known as: outline toggle switch css, border only switch, ghost toggle switch.

HTML

<label class="tgl-outline">
  <input type="checkbox" role="switch" aria-label="Show grid">
  <span class="tgl-outline-track" aria-hidden="true"></span>
</label>

CSS

.tgl-outline {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;

}
.tgl-outline input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-outline-track {
  position: relative;
  display: block;
  width: 3.6em;
  height: 2em;
  border: 0.125em solid color-mix(in srgb, currentColor 50%, transparent);
  border-radius: 1em;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}
.tgl-outline-track::before {
  content: "";
  position: absolute;
  top: 0.2em;
  left: 0.2em;
  width: 1.35em;
  height: 1.35em;
  border: 0.125em solid color-mix(in srgb, currentColor 50%, transparent);
  border-radius: 50%;
  box-shadow: inset 0 0 0 0 var(--c), 0 0 0 0 color-mix(in srgb, var(--c) 0%, transparent);
  transition:
    transform 0.4s cubic-bezier(0.3, 0.7, 0.3, 1),
    border-color 0.3s ease,
    box-shadow 0.35s ease 0.1s;
}
.tgl-outline input:checked + .tgl-outline-track {
  border-color: var(--c);
  box-shadow: 0 0 0.6em color-mix(in srgb, var(--c) 30%, transparent);
}
.tgl-outline input:checked + .tgl-outline-track::before {
  border-color: var(--c);
  box-shadow: inset 0 0 0 0.7em var(--c), 0 0 0.5em color-mix(in srgb, var(--c) 60%, transparent);
  transform: translateX(1.6em);
}
.tgl-outline input:focus-visible + .tgl-outline-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

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