AI CSS Animations

Image Checkbox

Photo tiles that select like a gallery app: the picture shrinks inside a tinted frame and a round check badge fills in. The scenes are CSS gradients; swap in your own img or background-image.

Also known as: css checkbox image, replace checkbox with image css, photo select checkbox.

HTML

<div class="chk-image-checkbox" role="group" aria-label="Photos">
  <label class="chk-image-checkbox-tile">
    <input type="checkbox" checked>
    <span class="chk-image-checkbox-frame"><span class="chk-image-checkbox-pic"></span></span>
    <span class="chk-image-checkbox-badge"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6.5 12.5l3.5 3.5 7.5-7.5"/></svg></span>
    <span class="chk-image-checkbox-caption">Alps</span>
  </label>
  <label class="chk-image-checkbox-tile">
    <input type="checkbox">
    <span class="chk-image-checkbox-frame"><span class="chk-image-checkbox-pic"></span></span>
    <span class="chk-image-checkbox-badge"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6.5 12.5l3.5 3.5 7.5-7.5"/></svg></span>
    <span class="chk-image-checkbox-caption">Dusk</span>
  </label>
  <label class="chk-image-checkbox-tile">
    <input type="checkbox">
    <span class="chk-image-checkbox-frame"><span class="chk-image-checkbox-pic"></span></span>
    <span class="chk-image-checkbox-badge"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6.5 12.5l3.5 3.5 7.5-7.5"/></svg></span>
    <span class="chk-image-checkbox-caption">Night</span>
  </label>
</div>

CSS

.chk-image-checkbox {
  --c: var(--chk-color, #3b82f6);
  font-size: var(--chk-size, 24px);
  display: flex;
  gap: 0.6em;}
.chk-image-checkbox-tile {
  --sky: color-mix(in srgb, var(--c) 30%, #dbeafe);
  --far: color-mix(in srgb, var(--c) 55%, #475569);
  --near: color-mix(in srgb, var(--c) 70%, #0f172a);
  --sun: 72% 28%;
  --peak1: 30% 42%;
  --peak2: 70% 58%;
  position: relative;
  display: grid;
  justify-items: center;
  gap: 0.35em;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}
.chk-image-checkbox-tile:nth-of-type(2) {
  --sky: linear-gradient(#fcd9a8, color-mix(in srgb, var(--c) 30%, #f9a8d4));
  --far: color-mix(in srgb, var(--c) 40%, #7c3aed);
  --near: color-mix(in srgb, var(--c) 45%, #3b0764);
  --sun: 35% 58%;
  --peak1: 68% 50%;
  --peak2: 18% 70%;
}
.chk-image-checkbox-tile:nth-of-type(3) {
  --sky: linear-gradient(#0b1026, color-mix(in srgb, var(--c) 45%, #1e1b4b));
  --far: color-mix(in srgb, var(--c) 35%, #334155);
  --near: color-mix(in srgb, var(--c) 25%, #020617);
  --sun: 28% 30%;
  --peak1: 62% 46%;
  --peak2: 25% 66%;
}
.chk-image-checkbox-tile input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.chk-image-checkbox-frame {
  display: block;
  width: 4.4em;
  height: 4.4em;
  border-radius: 0.7em;
  transition: background-color 0.3s ease;
}
.chk-image-checkbox-pic {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 0.7em;
  background:
    radial-gradient(circle at var(--sun), #fff7d6 0 0.42em, transparent 0.46em),
    conic-gradient(from 135deg at var(--peak2), var(--near) 90deg, transparent 0),
    conic-gradient(from 135deg at var(--peak1), var(--far) 90deg, transparent 0),
    var(--sky);
  transition: transform 0.35s cubic-bezier(0.34, 1.3, 0.64, 1), border-radius 0.35s ease;
}
.chk-image-checkbox-badge {
  position: absolute;
  top: 0.3em;
  left: 0.3em;
  display: grid;
  place-items: center;
  width: 1.1em;
  height: 1.1em;
  border: 0.1em solid rgb(255 255 255 / 0.9);
  border-radius: 50%;
  background: rgb(15 23 42 / 0.25);
  transition: background-color 0.2s ease, border-color 0.2s ease, transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.chk-image-checkbox-badge svg {
  width: 0.85em;
  height: 0.85em;
  fill: none;
  stroke: #fff;
  stroke-width: 3.4;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 17;
  stroke-dashoffset: 17;
  transition: stroke-dashoffset 0.15s ease-in;
}
.chk-image-checkbox-caption {
  font-size: 0.72em;
  opacity: 0.7;
}
.chk-image-checkbox-tile:hover .chk-image-checkbox-pic {
  transform: scale(0.96);
}
.chk-image-checkbox-tile input:checked + .chk-image-checkbox-frame {
  background: color-mix(in srgb, var(--c) 22%, transparent);
}
.chk-image-checkbox-tile input:checked + .chk-image-checkbox-frame .chk-image-checkbox-pic {
  transform: scale(0.8);
  border-radius: 0.45em;
}
.chk-image-checkbox-tile input:checked ~ .chk-image-checkbox-badge {
  background: var(--c);
  border-color: var(--c);
  transform: scale(1.12);
}
.chk-image-checkbox-tile input:checked ~ .chk-image-checkbox-badge svg {
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.25s ease-out 0.1s;
}
.chk-image-checkbox-tile input:focus-visible + .chk-image-checkbox-frame {
  outline: 0.12em solid var(--c);
  outline-offset: 0.18em;
}

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