max-height
Example
.menu {
max-height: 300px;
}.menu {
max-height: 300px;
overflow-y: auto;
}max-height alone lets extra items overflow; overflow-y: auto adds a scrollbar only when needed.
Common mistakes
- Content spills out of a list with
max-heightbecauseoverflowwas left visible. - Animating
max-heightto '1000px' makes the collapse start with a delay.
Key points
- Elements with
max-heightand changing content also setoverflow-y: auto. - Menus and dialogs cap height relative to the viewport, like
max-height: 80dvh. - Expand animations don't rely on a large
max-heightguess, which makes timing uneven.
Interview questions
What is the default value of max-height?
'none', so the element has no height limit.
Does max-height override height?
Yes. If height is larger than max-height, the element stops at the max-height value.
Why does a max-height transition feel delayed?
The transition runs over the full max-height range, so with a large guessed value most of the time passes before the visible size changes.