Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Box Model and Sizing

The max-height property

Reference for the CSS max-height property: cap an element's height, make scrollable panels, and use it for expand and collapse animations, with live examples.

The max-height property sets an upper limit for an element's height. When the content is taller than the limit, the box stops growing and the content overflows, so you almost always pair it with an overflow value to make the extra content scrollable or clipped.

It suits menus, chat windows, code panes and any region that should not push the rest of the page down.

CSS
.panel { max-height: 12rem; overflow: auto; }

Try it

A scrollable panel
Expanding with a max-height transition

The trick animates between two lengths, which works everywhere. The downside is that the timing follows the guessed maximum rather than the real height, so a very large number makes the closing delay noticeable.

Syntax

CSS
max-height: none
          | <length-percentage [0,โˆž]>
          | min-content | max-content | fit-content | fit-content(<length-percentage>)
          | stretch;
  • Initial value: none
  • Applies to: all elements except non-replaced inline elements, table columns and column groups
  • Inherited: no
  • Animatable: yes, as a length or percentage
  • Computed value: a percentage or none as specified, otherwise an absolute length

Values

Value Description
none No upper limit.
<length> A fixed cap such as 300px, 20rem or 80vh.
<percentage> A percentage of the containing block's height, used only if that height is definite; otherwise treated as none.
min-content, max-content, fit-content Content-based caps.
stretch Fill the available height. Newer, limited support.

Usage notes

  • Add overflow. Without it, content spills out of the box and may overlap what follows.
  • max-height: 80vh keeps modals and dropdowns inside the screen. Use dvh where mobile toolbars matter.
  • Accessible scroll regions. A scrollable box should be reachable by keyboard; add tabindex="0" and an accessible name if it holds important content.
  • min-height wins over max-height when both conflict.
  • Percentages need a definite height on the containing block.
  • Animating to the real height. Transitions to auto are not possible with max-height; modern alternatives are grid-template-rows: 0fr to 1fr and, where supported, interpolate-size: allow-keywords.
  • Images. max-height: 100% inside a fixed-height container keeps a picture from overflowing. Combine with object-fit.

Browser support

Supported in every browser.