Because CSS cannot transition to auto dimensions, animating a container open to its natural height requires a workaround. The common approach pairs a transition on max-height with a max-height: 0 closed state, relying on a fixed pixel value large enough to exceed the content.

.dropdown {
  transition: 0.2s;
  max-height: 0;
}
.dropdown.open {
  /* 🎉 */
  max-height: 400px;
}

The obvious drawback is the magic number: the 400px in this example is a guess, not a measurement, though the technique's simplicity keeps it in production use.

The scrollbar side effect

Setting max-height: 0 also requires overflow: hidden so the element is fully hidden when collapsed. In the expanded state, overflow: auto is the safer choice — content taller than the expanded max-height won't be clipped. But overflow: auto creates a second problem: while the expansion is under way, the element always shows scrollbars for at least part of the animation, even when the final height needs none.

Overriding overflow with keyframes

One fix keeps overflow: auto for the expanded state and overrides it only while the animation runs. @keyframes take precedence over other declarations while they are active, so they can be used purely for scrollbar hiding rather than for animating the opening:

.dropdown {
  max-height: 0;
  overflow: hidden;
  transition: max-height 1.2s ease-in-out;
}
.dropdown.open {
  overflow: auto;
  max-height: 400px;
  animation: hide-scroll 1.2s backwards;
}
@keyframes hide-scroll {
  from, to { overflow: hidden; } 
}

The result: no scrollbars mid-animation, and scrollbars at the end only when the content actually needs them. The scrollbar popping in at that point introduces a small amount of jerkiness. When it rarely occurs, that can be acceptable; eliminating it entirely would mean applying a custom scrollbar to the element at all times and possibly restyling it during the animation.

The technique came from Stephen Shaw of the @keyframers, who worked it out with the author for a CodePen demo. The two later recorded it as a video for the CodePen channel, using Collab Mode to work through the problem and solution.