Exclusive FAQ accordion

CSS Observer·8 hours ago
:root {
  interpolate-size: allow-keywords;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: #f6f5f1;
  color: #18191d;
  font: 15px/1.55 system-ui, sans-serif;
}

.faq {
  width: min(520px, 100% - 32px);
}

.faq__title {
  margin: 0 0 14px;
  font-size: 1.3rem;
  letter-spacing: -0.02em;
}

details {
  border-bottom: 1px solid #e1ded7;
}

summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 2px;
  font-weight: 600;
  list-style: none;
  cursor: pointer;
}

summary::-webkit-details-marker {
  display: none;
}

summary::after {
  content: "";
  flex: none;
  width: 10px;
  height: 10px;
  border-right: 2px solid #8a8c94;
  border-bottom: 2px solid #8a8c94;
  transform: translateY(-3px) rotate(45deg);
  transition: transform 0.25s;
}

details[open] summary::after {
  transform: translateY(2px) rotate(-135deg);
}

summary:focus-visible {
  outline: 2px solid #18191d;
  outline-offset: 2px;
  border-radius: 4px;
}

details p {
  margin: 0;
  padding: 0 2px 18px;
  color: #55585f;
}

details::details-content {
  block-size: 0;
  overflow: hidden;
  transition: block-size 0.3s ease, content-visibility 0.3s allow-discrete;
}

details[open]::details-content {
  block-size: auto;
}

@media (prefers-reduced-motion: reduce) {
  details::details-content,
  summary::after {
    transition: none;
  }
}

How to use it

Giving every <details> the same name makes them exclusive: opening one closes the others. The height animation uses ::details-content with interpolate-size: allow-keywords. Browsers without those features still open and close, just without animating.