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.
.panel { max-height: 12rem; overflow: auto; }
Try it
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
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
noneas 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: 80vhkeeps modals and dropdowns inside the screen. Usedvhwhere 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-heightwins overmax-heightwhen both conflict.- Percentages need a definite height on the containing block.
- Animating to the real height. Transitions to
autoare not possible withmax-height; modern alternatives aregrid-template-rows: 0frto1frand, where supported,interpolate-size: allow-keywords. - Images.
max-height: 100%inside a fixed-height container keeps a picture from overflowing. Combine withobject-fit.
Browser support
Supported in every browser.
Related
min-heightโ lower limitheightโ preferred heightoverflowโ scrolling or clippingmax-widthโ the horizontal cap- Lesson: Overflow
- Lesson: Width and Height