AI CSS Animations

Skeuomorphic Toggle Switch

The glossy iOS 6 switch: ON, a domed knob and OFF sit on one strip that slides behind a recessed window. Inset shadow, gradients and embossed text, all in CSS with one transform.

Also known as: ios 6 toggle switch, retro ios switch css, sliding on off switch.

HTML

<label class="tgl-skeuomorphic">
  <input type="checkbox" role="switch" aria-label="Sync">
  <span class="tgl-skeuomorphic-track" aria-hidden="true">
    <span class="tgl-skeuomorphic-strip">
      <span class="tgl-skeuomorphic-on">ON</span>
      <span class="tgl-skeuomorphic-off">OFF</span>
      <span class="tgl-skeuomorphic-knob"></span>
    </span>
  </span>
</label>

CSS

.tgl-skeuomorphic {
  --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: 700;
}
.tgl-skeuomorphic input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
/* the window: 4.8em wide, the knob is 1.8em, so the strip travels 3em */
.tgl-skeuomorphic-track {
  position: relative;
  isolation: isolate;
  display: block;
  width: 4.8em;
  height: 1.8em;
  border-radius: 0.9em;
  overflow: hidden;
  box-shadow: 0 1px 0 rgb(255 255 255 / 0.5);
}
.tgl-skeuomorphic-track::after {
  content: "";
  position: absolute;
  z-index: 1;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.3), inset 0 0.12em 0.25em rgb(0 0 0 / 0.35);
  pointer-events: none;
}
.tgl-skeuomorphic-strip {
  position: absolute;
  top: 0;
  left: 0;
  display: grid;
  grid-template-columns: 3.9em 3.9em;
  height: 100%;
  transform: translateX(-3em);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.tgl-skeuomorphic-on,
.tgl-skeuomorphic-off {
  display: grid;
  place-items: center;
  font-size: 0.8em;
  letter-spacing: 0.04em;
}
.tgl-skeuomorphic-on {
  padding-right: 1.125em;
  background: linear-gradient(color-mix(in srgb, var(--c) 70%, #000), var(--c) 55%, color-mix(in srgb, var(--c) 80%, #fff));
  color: #fff;
  text-shadow: 0 -1px 0 rgb(0 0 0 / 0.35);
}
.tgl-skeuomorphic-off {
  padding-left: 1.125em;
  background: linear-gradient(#d4d4d4, #f2f2f2 55%, #fff);
  color: #858585;
  text-shadow: 0 1px 0 #fff;
}
.tgl-skeuomorphic-knob {
  position: absolute;
  z-index: 2;
  top: 0;
  left: 3em;
  width: 1.8em;
  height: 1.8em;
  border-radius: 50%;
  background: linear-gradient(#cacaca, #fbfbfb);
  box-shadow:
    inset 0 1px 0 #fff,
    0 0 0 1px rgb(0 0 0 / 0.28),
    0 0.06em 0.2em rgb(0 0 0 / 0.35);
}
.tgl-skeuomorphic input:checked + .tgl-skeuomorphic-track .tgl-skeuomorphic-strip { transform: none; }
.tgl-skeuomorphic input:focus-visible + .tgl-skeuomorphic-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

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