AI CSS Animations

CSS Accordion with Radio Buttons

An FAQ accordion where every header is a radio, so opening one closes the rest. Panels ease open with the grid-template-rows 0fr to 1fr trick and the chevron flips.

Also known as: css accordion radio button, pure css accordion, exclusive accordion css.

Most orders arrive in 2 to 4 days.

Yes, free returns within 30 days.

To 40 countries, duties included.

HTML

<div class="rad-radio-accordion" role="radiogroup" aria-label="FAQ">
  <div class="rad-radio-accordion-item">
    <label><input type="radio" name="rad-radio-accordion" value="shipping" checked><span>How fast is shipping?</span><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 9l6 6 6-6"/></svg></label>
    <div class="rad-radio-accordion-body"><p>Most orders arrive in 2 to 4 days.</p></div>
  </div>
  <div class="rad-radio-accordion-item">
    <label><input type="radio" name="rad-radio-accordion" value="returns"><span>Can I return it?</span><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 9l6 6 6-6"/></svg></label>
    <div class="rad-radio-accordion-body"><p>Yes, free returns within 30 days.</p></div>
  </div>
  <div class="rad-radio-accordion-item">
    <label><input type="radio" name="rad-radio-accordion" value="abroad"><span>Do you ship abroad?</span><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 9l6 6 6-6"/></svg></label>
    <div class="rad-radio-accordion-body"><p>To 40 countries, duties included.</p></div>
  </div>
</div>

CSS

.rad-radio-accordion {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  display: grid;
  width: 17em;
  border: 1px solid color-mix(in srgb, currentColor 15%, transparent);
  border-radius: 0.8em;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-radio-accordion-item + .rad-radio-accordion-item {
  border-top: 1px solid color-mix(in srgb, currentColor 12%, transparent);
}
.rad-radio-accordion label {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.6em;
  padding: 0.6em 0.9em;
  border-radius: 0.7em;
  font-size: 0.9em;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.25s ease;
}
.rad-radio-accordion label span {
  flex: 1;
}
.rad-radio-accordion input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.rad-radio-accordion svg {
  flex: none;
  width: 1.1em;
  height: 1.1em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  color: color-mix(in srgb, currentColor 50%, transparent);
  transition: transform 0.4s cubic-bezier(0.65, 0, 0.35, 1), color 0.25s ease;
}
.rad-radio-accordion label:hover svg {
  color: var(--c);
}
.rad-radio-accordion-item:has(:checked) label {
  color: var(--c);
}
.rad-radio-accordion-item:has(:checked) svg {
  color: var(--c);
  transform: rotate(180deg);
}
.rad-radio-accordion label:has(:focus-visible) {
  outline: 0.12em solid var(--c);
  outline-offset: -0.15em;
}
.rad-radio-accordion-body {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.4s cubic-bezier(0.65, 0, 0.35, 1);
}
.rad-radio-accordion-body p {
  min-height: 0;
  margin: 0;
  padding: 0 1.05em;
  overflow: hidden;
  font-size: 0.8em;
  line-height: 1.45;
  color: color-mix(in srgb, currentColor 70%, transparent);
  opacity: 0;
  transition: opacity 0.2s ease, padding 0.4s cubic-bezier(0.65, 0, 0.35, 1);
}
.rad-radio-accordion-item:has(:checked) .rad-radio-accordion-body {
  grid-template-rows: 1fr;
}
.rad-radio-accordion-item:has(:checked) .rad-radio-accordion-body p {
  padding-bottom: 0.8em;
  opacity: 1;
  transition: opacity 0.3s ease 0.15s, padding 0.4s cubic-bezier(0.65, 0, 0.35, 1);
}

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