<main>
  <section>
    <h1>CSS grid</h1>
    <p>Animate accordion height with CSS grid work around.
    </p>
  </section>

  <ul class="accordion-group">
    <!-- Accordion Item -->
    <li class="accordion-item">
      <!-- Button Trigger -->
      <button type="button" class="accordion-trigger">
        <span>Label 1</span>
        <span class="accordion-icon">
          <svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
            <rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
            <rect y="70" rx="8" width="100%" height="15" />
          </svg>
        </span>
      </button>
      <!-- Content Container -->
      <div class="accordion-content">
        <p class="contents">
          <span>
            Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
          </span>
        </p>
      </div>
    </li>
    <!-- Accordion Item -->
    <li class="accordion-item">
      <!-- Button Trigger -->
      <button type="button" class="accordion-trigger">
        <span>Label 2</span>
        <span class="accordion-icon">
          <svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
            <rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
            <rect y="70" rx="8" width="100%" height="15" />
          </svg>
        </span>
      </button>
      <!-- Content Container -->
      <div class="accordion-content">
        <p class="contents">
          <span>
            Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
          </span>
        </p>
      </div>
    </li>
    <!-- Accordion Item -->
    <li class="accordion-item">
      <!-- Button Trigger -->
      <button type="button" class="accordion-trigger">
        <span>Label 3</span>
        <span class="accordion-icon">
          <svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
            <rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
            <rect y="70" rx="8" width="100%" height="15" />
          </svg>
        </span>
      </button>
      <!-- Content Container -->
      <div class="accordion-content">
        <p class="contents">
          <span>
            Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
          </span>
        </p>
      </div>
    </li>
    <!-- Accordion Item -->
    <li class="accordion-item">
      <!-- Button Trigger -->
      <button type="button" class="accordion-trigger">
        <span>Label 4</span>
        <span class="accordion-icon">
          <svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
            <rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
            <rect y="70" rx="8" width="100%" height="15" />
          </svg>
        </span>
      </button>
      <!-- Content Container -->
      <div class="accordion-content">
        <p class="contents">
          <span>
            Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
          </span>
        </p>
      </div>
    </li>
    <!-- Accordion Item -->
    <li class="accordion-item">
      <!-- Button Trigger -->
      <button type="button" class="accordion-trigger">
        <span>Label 5</span>
        <span class="accordion-icon">
          <svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
            <rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
            <rect y="70" rx="8" width="100%" height="15" />
          </svg>
        </span>
      </button>
      <!-- Content Container -->
      <div class="accordion-content">
        <p class="contents">
          <span>
            Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
          </span>
        </p>
      </div>
    </li>
  </ul>
</main>
@import url("https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap");

:root {
  --text-black: #09080a;
  --text-gray: #3b4b68;
  --highlight: #f1f2f4;
}

@layer reset {
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: "Inter", sans-serif;
  }

  body {
    color: var(--text-black);
  }

  ul,
  li {
    list-style: none;
  }

  button {
    border: none;
    background: none;
    appearance: none;
    cursor: pointer;
    white-space: nowrap;
  }
}

main {
  width: 100%;
  padding-inline: 1rem;
  padding-bottom: 2rem;
}

section {
  max-width: 64ch;
  gap: 0.5rem 0;
  margin-block: 3rem;

  p {
    color: var(--text-gray);
  }
}

main,
section {
  display: flex;
  flex-direction: column;
  align-items: center;
}

h1 {
  font-size: 2rem;
  letter-spacing: 1px;
}

p {
  line-height: 1.5rem;
}

.accordion-group {
  max-width: 420px;
  width: 100%;
  color: var(--text-gray);
}

.accordion-item + .accordion-item {
  border-top: 2px solid var(--highlight);
}

.accordion-trigger {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding-inline: 0.75rem;
  padding-block: 1rem;
  border-radius: 0.5rem;
  font-size: 1.2rem;
  font-weight: 600;
  color: inherit;

  &:hover {
    background: var(--highlight);
  }

  span {
    display: block;
    flex-shrink: 0;
  }

  .accordion-icon {
    width: 18px;
    height: 18px;
  }

  svg {
    width: 100%;
    height: auto;
  }

  .vertical-cross {
    transition: transform 750ms;
    transform: scaleY(1);
    transform-origin: 50% 50%;

    [data-open] & {
      transform: scaleY(0);
    }
  }
}

.accordion-content {
  display: grid;
  grid-template-rows: 0fr;
  background: #fff;
  transition-property: grid-template-rows;
  transition-duration: 750ms;

  .contents {
    line-height: 1.5rem;
    overflow: hidden;
    grid-row: 1 / span2;

    & span {
      display: block;
      padding-inline: 0.75rem;
      padding-top: 0.25rem;
      padding-bottom: 1rem;
    }
  }

  [data-open] & {
    grid-template-rows: 1fr;
  }
}
View Compiled
const accordionTrigger = document.querySelectorAll(".accordion-trigger");

const accordionItem = document.querySelectorAll(".accordion-item");

function toggleAccordion(event) {
  currentTarget = event.target;

  const currentParent = currentTarget.closest(".accordion-item");

  const isOpen = currentParent.hasAttribute("data-open");

  for (let item of accordionItem) {
    item.removeAttribute("data-open");
  }

  if (!isOpen) {
    currentParent.setAttribute("data-open", "");
  }
}

for (let trigger of accordionTrigger) {
  trigger.addEventListener("click", toggleAccordion);
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.