AI CSS Animations

3D Rocker Switch

A power strip rocker in a dark bezel: the paddle tips on its center line with perspective, the pressed half shades darker, and the I side lights up in your accent when it is on.

Also known as: css rocker switch, power rocker switch, i o rocker switch css.

HTML

<label class="tgl-rocker">
  <input type="checkbox" role="switch" aria-label="Power">
  <span class="tgl-rocker-bezel" aria-hidden="true">
    <span class="tgl-rocker-paddle">
      <span class="tgl-rocker-i"></span>
      <span class="tgl-rocker-o"></span>
    </span>
  </span>
</label>

CSS

.tgl-rocker {
  --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-rocker input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-rocker-bezel {
  display: block;
  padding: 0.3em;
  border-radius: 0.6em;
  background: linear-gradient(#3b3e46, #1c1e23);
  box-shadow: 0 0.25em 0.6em rgb(0 0 0 / 0.3), inset 0 1px 0 rgb(255 255 255 / 0.14);
  perspective: 6em;
}
.tgl-rocker-paddle {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: 5em;
  height: 2.5em;
  border-radius: 0.35em;
  background: color-mix(in srgb, var(--c) 30%, #2b2d33);
  transform: rotateY(20deg);
  transition: transform 0.22s cubic-bezier(0.5, 0, 0.3, 1.5), background-color 0.2s ease, box-shadow 0.3s ease;
}
/* two shading layers, crossfaded: the pressed half always reads darker */
.tgl-rocker-paddle::before,
.tgl-rocker-paddle::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  transition: opacity 0.22s ease;
}
.tgl-rocker-paddle::before { background: linear-gradient(90deg, rgb(255 255 255 / 0.28), rgb(255 255 255 / 0.05) 49.5%, rgb(0 0 0 / 0.25) 50.5%, rgb(0 0 0 / 0.45)); }
.tgl-rocker-paddle::after {
  background: linear-gradient(90deg, rgb(0 0 0 / 0.45), rgb(0 0 0 / 0.25) 49.5%, rgb(255 255 255 / 0.05) 50.5%, rgb(255 255 255 / 0.28));
  opacity: 0;
}
.tgl-rocker-i,
.tgl-rocker-o {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
}
.tgl-rocker-i::before {
  content: "";
  width: 0.18em;
  height: 0.95em;
  border-radius: 0.1em;
  background: rgb(255 255 255 / 0.9);
}
.tgl-rocker-o::before {
  content: "";
  width: 0.9em;
  height: 0.9em;
  border: 0.17em solid rgb(255 255 255 / 0.9);
  border-radius: 50%;
}
.tgl-rocker input:checked + .tgl-rocker-bezel .tgl-rocker-paddle {
  background: var(--c);
  box-shadow: 0 0 0.9em color-mix(in srgb, var(--c) 55%, transparent);
  transform: rotateY(-20deg);
}
.tgl-rocker input:checked + .tgl-rocker-bezel .tgl-rocker-paddle::before { opacity: 0; }
.tgl-rocker input:checked + .tgl-rocker-bezel .tgl-rocker-paddle::after { opacity: 1; }
.tgl-rocker input:focus-visible + .tgl-rocker-bezel {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

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