0
to auto
when its height is unknown.transition
to specify that changes to max-height
should be transitioned over.overflow: hidden
to prevent the contents of the hidden element from overflowing its container.max-height
to specify an initial height of 0
.:hover
pseudo-class to change the max-height
to the value of the --max-height
variable set by JavaScript.Element.scrollHeight
and CSSStyleDeclaration.setProperty()
to set the value of --max-height
to the current height of the element.<div class="trigger"> Hover me to see a height transition. <div class="el">Additional content</div> </div>
.el {
transition: max-height 0.3s;
overflow: hidden;
max-height: 0;
}
.trigger:hover > .el {
max-height: var(--max-height);
}
let el = document.querySelector('.el');
let height = el.scrollHeight;
el.style.setProperty('--max-height', height + 'px');
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️