AI CSS Animations

3D Push Checkbox

A keycap checkbox that stands on a solid edge. Checking pushes it down into the edge, floods it with the accent and pops the tick; pressing sinks it all the way. One box-shadow and a translate.

Also known as: 3d checkbox css, pressable checkbox, keycap checkbox.

HTML

<label class="chk-3d-push-checkbox">
  <input type="checkbox">
  <span class="chk-3d-push-checkbox-key">
    <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5.5 12.5l4 4L18.5 7.5"/></svg>
  </span>
  Two-factor login
</label>

CSS

.chk-3d-push-checkbox {
  --c: var(--chk-color, #3b82f6);
  font-size: var(--chk-size, 24px);
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.6em;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.3;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  gap: 0.7em;}
.chk-3d-push-checkbox input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.chk-3d-push-checkbox-key {
  --edge: color-mix(in srgb, var(--c) 30%, #94a3b8);
  box-sizing: border-box;
  display: grid;
  place-items: center;
  flex: none;
  width: 1.5em;
  height: 1.5em;
  border: 0.1em solid var(--edge);
  border-radius: 0.35em;
  background: color-mix(in srgb, var(--c) 6%, #fff);
  box-shadow: 0 0.25em 0 var(--edge);
  transform: translateY(-0.125em);
  transition:
    transform 0.18s cubic-bezier(0.3, 0.7, 0.4, 1),
    box-shadow 0.18s cubic-bezier(0.3, 0.7, 0.4, 1),
    background-color 0.2s ease,
    border-color 0.2s ease;
}
.chk-3d-push-checkbox-key svg {
  display: block;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  width: 0.95em;
  height: 0.95em;
  stroke: #fff;
  stroke-width: 3.2;
  transform: scale(0);
  transition: transform 0.12s ease-in;
}
.chk-3d-push-checkbox input:checked + .chk-3d-push-checkbox-key {
  --edge: color-mix(in srgb, var(--c) 62%, #000);
  background: var(--c);
  box-shadow: 0 0.06em 0 var(--edge);
  transform: translateY(0.065em);
}
.chk-3d-push-checkbox input:checked + .chk-3d-push-checkbox-key svg {
  transform: scale(1);
  transition: transform 0.4s cubic-bezier(0.34, 1.7, 0.64, 1) 0.06s;
}
.chk-3d-push-checkbox input:active + .chk-3d-push-checkbox-key {
  box-shadow: 0 0 0 var(--edge);
  transform: translateY(0.125em);
}
.chk-3d-push-checkbox input:focus-visible + .chk-3d-push-checkbox-key {
  outline: 0.125em solid var(--c);
  outline-offset: 0.18em;
}

@media (prefers-reduced-motion: reduce) {
  .chk-3d-push-checkbox, .chk-3d-push-checkbox *, .chk-3d-push-checkbox::before, .chk-3d-push-checkbox::after, .chk-3d-push-checkbox *::before, .chk-3d-push-checkbox *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --chk-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --chk-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 checkboxes