How to change a HTML element height transition. Sourced under a permissive license.

How to change a HTML element height transition – A HTML and CSS code snippet

<div class="trigger">
  Hover me to see a height transition.
  <div class="el">Additional content</div>
</div>

<style>
.el {
  transition: max-height 0.3s;
  overflow: hidden;
  max-height: 0;
}
</style>

<script>
.trigger:hover > .el {
  max-height: var(--max-height);
}

let el = document.querySelector('.el');
let height = el.scrollHeight;
el.style.setProperty('--max-height', height + 'px');
</script>

Tags: CSS, HTML, CSS, CSS code snippet, html code snippet, javascript, element height, html element, querySelector, scrollHeight

Image: Unsplash license

CC BY 4.0 added intro and tags – 30 Seconds of Code