AI CSS Animations

Windows 11 Toggle Switch

The Fluent toggle from Windows 11 settings: an outlined track with a small knob that grows on hover, stretches while pressed and turns white on a filled track, with the On or Off text on its left.

Also known as: fluent ui toggle switch, windows toggle switch css, fluent design switch.

HTML

<label class="tgl-windows-11">
  <input type="checkbox" role="switch" aria-label="Bluetooth">
  <span class="tgl-windows-11-track" aria-hidden="true"></span>
  <span class="tgl-windows-11-state" aria-hidden="true"></span>
</label>

CSS

.tgl-windows-11 {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-flex;
  vertical-align: middle;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  align-items: center;
  gap: 0.75em;
  font-family: "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif;
}
.tgl-windows-11 input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-windows-11-state {
  order: -1;
  width: 1.8em;
  font-size: 0.875em;
  text-align: right;
}
.tgl-windows-11-state::before { content: "Off"; }
.tgl-windows-11-track {
  position: relative;
  width: 2.5em;
  height: 1.25em;
  border: 1px solid color-mix(in srgb, currentColor 62%, transparent);
  border-radius: 0.625em;
  background: color-mix(in srgb, currentColor 4%, transparent);
  transition: background-color 0.15s ease, border-color 0.15s ease;
}
.tgl-windows-11-track::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 0.19em;
  width: 0.75em;
  height: 0.75em;
  margin-top: -0.375em;
  border-radius: 0.375em;
  background: color-mix(in srgb, currentColor 75%, transparent);
  transition:
    transform 0.25s cubic-bezier(0.1, 0.9, 0.2, 1),
    width 0.2s cubic-bezier(0.1, 0.9, 0.2, 1),
    scale 0.15s ease,
    background-color 0.15s ease;
}
.tgl-windows-11:hover .tgl-windows-11-track { background: color-mix(in srgb, currentColor 9%, transparent); }
.tgl-windows-11:hover .tgl-windows-11-track::before { scale: 1.17; }
.tgl-windows-11:active .tgl-windows-11-track::before { width: 1.06em; }
.tgl-windows-11 input:checked + .tgl-windows-11-track {
  border-color: var(--c);
  background: var(--c);
}
.tgl-windows-11 input:checked + .tgl-windows-11-track::before {
  background: #fff;
  transform: translateX(calc(1.37em - 2px));
}
.tgl-windows-11:active input:checked + .tgl-windows-11-track::before { transform: translateX(calc(1.06em - 2px)); }
.tgl-windows-11 input:checked ~ .tgl-windows-11-state::before { content: "On"; }
.tgl-windows-11 input:focus-visible + .tgl-windows-11-track {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

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