DEV Community

Said Mounaim
Said Mounaim

Posted on

3 2

Transitions an element's height 😍

Transitions an element's height from 0 to auto when its height is unknown.

  • Use transition to specify that changes to max-height should be transitioned over.
  • Use overflow: hidden to prevent the contents of the hidden element from overflowing its container.
  • Use max-height to specify an initial height of 0.
  • Use the :hover pseudo-class to change the max-height to the value of the --max-height variable set by JavaScript.
  • Use Element.scrollHeight and CSSStyleDeclaration.setProperty() to set the value of --max-height to the current height of the element.
<div class="trigger">
  Hover me
  <div class="el">Additional content</div>
</div>
Enter fullscreen mode Exit fullscreen mode
.el {
  transition: max-height 0.3s;
  overflow: hidden;
  max-height: 0;
}

.trigger:hover > .el {
  max-height: var(--max-height);
}
Enter fullscreen mode Exit fullscreen mode
let el = document.querySelector('.el');
let height = el.scrollHeight;
el.style.setProperty('--max-height', height + 'px');
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay