How to animate auto height with pure CSS

June 15, 2024

TLDR: https://codepen.io/jofrly/pen/XWwVjZR

It's quite easy to animate the height of a HTML element with CSS if the Element has a fixed height. You simply set height: 0 and overflow:hidden when it should not be visible and for example height: 300px when it should be visible. Additionally, add transition: height 0.5s ease-in-out and you're all set!

When trying to animate the height of an element that does not have a fixed height but instead a dynamic one it gets trickier. It's not as simple as setting height: auto. Unfortunately, this does not work.

However, it is possible to animate the grid-template-rows CSS property with the value from 0fr to 1fr. The basic idea is a CSS code like this.

.content {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.5s ease-in-out;

  &.open {
    grid-template-rows: 1fr;
  }

  .inner {
    overflow: hidden;
  }
}

There is one problem with the above code, though which becomes apparent once you slow down the animation (e.g. 2.5s):

As you can see, it looks like the content within the .inner element grows slower than the surrounding .content element. This can be solved by moving the overflow: hidden to .content and adding min-height: 0 to the .inner element:

.content {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.5s ease-in-out;
  overflow: hidden;

  &.open {
    grid-template-rows: 1fr;
  }

  .inner {
    min-height: 0;
  }
}

The full code for this example looks like this:

<style>
  .expandable-card {
    border: 1px solid black;
    width: 300px;

    .header {
      padding: 4px;
      cursor: pointer;
    }

    .content {
      display: grid;
      grid-template-rows: 0fr;
      transition: grid-template-rows 0.5s ease-in-out;
      overflow: hidden;

      &.open {
        grid-template-rows: 1fr;
      }

      .inner {
        min-height: 0;

        .lorem-ipsum {
          padding: 4px;
          border-top: 1px solid black;
        }
      }
    }
  }
</style>

<div class="expandable-card">
  <div class="header">
    Click to Show / Hide
  </div>

  <div class="content">
    <div class="inner">
      <div class="lorem-ipsum">
        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
      </div>
    </div>
  </div>
</div>

<script>
  const headerElement = document.querySelector('.header');
  const contentElement = document.querySelector('.content');
  
  headerElement.addEventListener('click', () => contentElement.classList.toggle('open'));
</script>