AI CSS Animations

Pure CSS Tabs with Radio Buttons

Tabs built on the radio hack: each tab is a radio, an underline slides under the active one and its panel fades up in place. :has() matches the checked tab to its panel.

Also known as: css tabs radio buttons, css only tabs, tabs without javascript.

A lightweight tab set where each tab is a radio, so exactly one panel shows.

Three tabs, one underline, zero lines of JavaScript.

"Swapped our settings page to this in ten minutes."

HTML

<div class="rad-radio-tabs" role="radiogroup" aria-label="Sections">
  <div class="rad-radio-tabs-list">
    <label><input type="radio" name="rad-radio-tabs" value="overview" checked><span>Overview</span></label>
    <label><input type="radio" name="rad-radio-tabs" value="specs"><span>Specs</span></label>
    <label><input type="radio" name="rad-radio-tabs" value="reviews"><span>Reviews</span></label>
    <span class="rad-radio-tabs-bar" aria-hidden="true"></span>
  </div>
  <div class="rad-radio-tabs-panels">
    <p>A lightweight tab set where each tab is a radio, so exactly one panel shows.</p>
    <p>Three tabs, one underline, zero lines of JavaScript.</p>
    <p>"Swapped our settings page to this in ten minutes."</p>
  </div>
</div>

CSS

.rad-radio-tabs {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  --i: 0;
  display: grid;
  gap: 0.9em;
  width: 17em;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-radio-tabs:has(label:nth-of-type(2) :checked) { --i: 1; }
.rad-radio-tabs:has(label:nth-of-type(3) :checked) { --i: 2; }
.rad-radio-tabs-list {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-bottom: 1px solid color-mix(in srgb, currentColor 15%, transparent);
}
.rad-radio-tabs label {
  position: relative;
  cursor: pointer;
}
.rad-radio-tabs input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.rad-radio-tabs input + span {
  display: block;
  padding: 0.5em 0 0.7em;
  border-radius: 0.3em;
  text-align: center;
  font-size: 0.9em;
  font-weight: 600;
  color: color-mix(in srgb, currentColor 55%, transparent);
  transition: color 0.25s ease;
}
.rad-radio-tabs label:hover input:not(:checked) + span {
  color: color-mix(in srgb, currentColor 85%, transparent);
}
.rad-radio-tabs input:checked + span {
  color: var(--c);
}
.rad-radio-tabs input:focus-visible + span {
  outline: 0.12em solid var(--c);
  outline-offset: -0.12em;
}
.rad-radio-tabs-bar {
  position: absolute;
  left: 0;
  bottom: -1px;
  box-sizing: border-box;
  width: calc(100% / 3);
  height: 0.18em;
  padding: 0 1em;
  border-radius: 0.2em;
  background: var(--c);
  background-clip: content-box;
  transform: translateX(calc(var(--i) * 100%));
  transition: transform 0.45s cubic-bezier(0.65, 0, 0.35, 1);
}
.rad-radio-tabs-panels {
  display: grid;
}
.rad-radio-tabs-panels p {
  grid-area: 1 / 1;
  margin: 0;
  font-size: 0.85em;
  line-height: 1.45;
  color: color-mix(in srgb, currentColor 75%, transparent);
  opacity: 0;
  visibility: hidden;
  transform: translateY(0.5em);
  transition: opacity 0.15s ease, transform 0.3s ease, visibility 0s linear 0.15s;
}
.rad-radio-tabs:has(label:nth-of-type(1) :checked) p:nth-child(1),
.rad-radio-tabs:has(label:nth-of-type(2) :checked) p:nth-child(2),
.rad-radio-tabs:has(label:nth-of-type(3) :checked) p:nth-child(3) {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity 0.3s ease 0.15s, transform 0.4s cubic-bezier(0.22, 1, 0.36, 1) 0.15s, visibility 0s;
}

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