AI CSS Animations

Two Label Toggle Switch

Two words side by side in one pill, with a colored highlight that slides under whichever is active. Reads like a two-option segmented control but it is one checkbox, so it submits as on or off.

Also known as: css toggle switch with two labels, segmented toggle switch, sliding pill toggle.

HTML

<label class="tgl-two-labels">
  <input type="checkbox" role="switch" aria-label="List view">
  <span class="tgl-two-labels-track" aria-hidden="true">
    <span class="tgl-two-labels-opt">Grid</span>
    <span class="tgl-two-labels-opt">List</span>
  </span>
</label>

CSS

.tgl-two-labels {
  --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;
  font-family: system-ui, sans-serif;
  font-weight: 600;
}
.tgl-two-labels input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-two-labels-track {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 0.25em;
  border-radius: 99em;
  background: color-mix(in srgb, currentColor 8%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 10%, transparent);
}
.tgl-two-labels-track::before {
  content: "";
  position: absolute;
  top: 0.25em;
  bottom: 0.25em;
  left: 0.25em;
  width: calc(50% - 0.25em);
  border-radius: 99em;
  background: var(--c);
  box-shadow: 0 0.15em 0.45em color-mix(in srgb, var(--c) 40%, transparent);
  transition: transform 0.4s cubic-bezier(0.45, 1.25, 0.5, 1);
}
.tgl-two-labels-opt {
  position: relative;
  padding: 0.6em 1.3em;
  font-size: 0.875em;
  line-height: 1;
  text-align: center;
  color: color-mix(in srgb, currentColor 60%, transparent);
  transition: color 0.3s ease;
}
.tgl-two-labels input:not(:checked) + .tgl-two-labels-track .tgl-two-labels-opt:first-child,
.tgl-two-labels input:checked + .tgl-two-labels-track .tgl-two-labels-opt:last-child { color: #fff; }
.tgl-two-labels input:checked + .tgl-two-labels-track::before { transform: translateX(100%); }
.tgl-two-labels input:focus-visible + .tgl-two-labels-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

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