max-height

Term side of the Card.
Topic: Sizes & Margins
Unit title: max-height
Categories: Property
Explanation side of the Card

Explanation: The property that sets the maximum height of an element. It prevents the element from growing taller than the specified value, ensuring that its content doesn't overflow beyond this limit, which helps to control the layout and appearance.

Browser compatibility:
  • The chrome browser supports this feature starting from version 18.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.3.
Availability: 

Example

Wrongcss
.menu {
  max-height: 300px;
}
Rightcss
.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-height because overflow was left visible.
  • Animating max-height to '1000px' makes the collapse start with a delay.

Key points

  • Elements with max-height and changing content also set overflow-y: auto.
  • Menus and dialogs cap height relative to the viewport, like max-height: 80dvh.
  • Expand animations don't rely on a large max-height guess, 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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy