AI CSS Animations

Segmented Control with Sliding Pill

An iOS style segmented control built from radios. A colored pill glides behind the chosen label with a soft spring, positioned by :has(:checked) and a translateX.

Also known as: ios segmented control css, radio button as button css, sliding radio toggle.

HTML

<div class="rad-segmented-control" role="radiogroup" aria-label="Range">
  <span class="rad-segmented-control-pill" aria-hidden="true"></span>
  <label><input type="radio" name="rad-segmented-control" value="day"><span>Day</span></label>
  <label><input type="radio" name="rad-segmented-control" value="week" checked><span>Week</span></label>
  <label><input type="radio" name="rad-segmented-control" value="month"><span>Month</span></label>
  <label><input type="radio" name="rad-segmented-control" value="year"><span>Year</span></label>
</div>

CSS

.rad-segmented-control {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  --i: 0;
  position: relative;
  display: inline-grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 0.25em;
  border-radius: 0.8em;
  background: color-mix(in srgb, currentColor 8%, transparent);
  font-weight: 600;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-segmented-control:has(label:nth-of-type(2) :checked) { --i: 1; }
.rad-segmented-control:has(label:nth-of-type(3) :checked) { --i: 2; }
.rad-segmented-control:has(label:nth-of-type(4) :checked) { --i: 3; }
.rad-segmented-control-pill {
  position: absolute;
  top: 0.25em;
  bottom: 0.25em;
  left: 0.25em;
  width: calc((100% - 0.5em) / 4);
  border-radius: 0.6em;
  background: var(--c);
  box-shadow: 0 0.25em 0.6em -0.2em color-mix(in srgb, var(--c) 70%, transparent);
  transform: translateX(calc(var(--i) * 100%));
  transition: transform 0.45s cubic-bezier(0.34, 1.25, 0.5, 1);
}
.rad-segmented-control label {
  position: relative;
  cursor: pointer;
}
.rad-segmented-control input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.rad-segmented-control input + span {
  position: relative;
  display: block;
  padding: 0.5em 1.1em;
  border-radius: 0.6em;
  text-align: center;
  color: color-mix(in srgb, currentColor 60%, transparent);
  transition: color 0.15s ease, transform 0.2s ease;
}
.rad-segmented-control label:hover input:not(:checked) + span {
  color: currentColor;
}
.rad-segmented-control input:checked + span {
  color: #fff;
  transition: color 0.25s ease 0.12s, transform 0.2s ease;
}
.rad-segmented-control label:active span {
  transform: scale(0.94);
}
.rad-segmented-control input:focus-visible + span {
  outline: 0.12em solid var(--c);
  outline-offset: 0.12em;
}

@media (prefers-reduced-motion: reduce) {
  .rad-segmented-control, .rad-segmented-control *, .rad-segmented-control::before, .rad-segmented-control::after, .rad-segmented-control *::before, .rad-segmented-control *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --rad-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --rad-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 18px.
  • 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 radio buttons