📏 CSS max-height
: Controlling Vertical Growth
In responsive and dynamic layouts, you may want an element to grow but not exceed a certain height. This is exactly what the max-height
property is designed for — it sets the maximum allowable height of an element.
It’s especially useful for content containers, modals, image scaling, and preventing overflow in unpredictable layouts.
🧾 What is max-height
?
The max-height
property in CSS defines the maximum height an element can expand to. If the content inside the element exceeds this height, it will either be clipped, scrollable, or cause overflow, depending on the overflow
setting.
🧬 Syntax
selector {
max-height: none | <length> | <percentage> | inherit | initial;
}
Value Options:
Value | Description |
---|---|
<length> | A fixed height (e.g., 300px , 10em , 50vh ) |
<percentage> | Relative to the height of the containing element |
none | Default; no maximum height (element can grow indefinitely) |
inherit | Inherits from the parent element |
initial | Resets to default (none ) |
🎯 Example Usage
1. Limit content height
.article-preview {
max-height: 200px;
overflow: hidden;
}
This limits the height of a text block and hides any overflow content, making previews tidy.
2. Responsive image cap
img {
max-height: 100vh;
}
Prevents an image from exceeding the height of the viewport — great for full-screen hero images.
3. Scrollable panel
.scroll-box {
max-height: 300px;
overflow-y: auto;
}
This creates a box that scrolls vertically if the content grows too tall — useful for chat windows or dropdown menus.
🧠 How It Works
- If content is smaller than
max-height
, the element behaves normally. - If content exceeds
max-height
, it stops growing beyond the limit. - Use in combination with
overflow
to manage behavior:overflow: hidden
→ hides extra contentoverflow: auto
orscroll
→ shows scrollbars
📐 Common Use Cases
✅ Preventing content from overflowing a section
✅ Creating scrollable dropdowns or menus
✅ Controlling responsive media scaling
✅ Limiting expandable content like accordions or modals
🛠️ Best Practices
- Combine
max-height
withtransition
to create smooth animations:
.collapsible {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease-out;
}
.collapsible.open {
max-height: 500px;
}
- Avoid mixing
height
andmax-height
unless needed —height
can overridemax-height
.
✅ Browser Support
The max-height
property is well supported across all major browsers:
- ✅ Chrome
- ✅ Firefox
- ✅ Safari
- ✅ Edge
- ✅ Internet Explorer 7+
🔚 Conclusion
The max-height
property is a flexible CSS tool that lets you constrain vertical size without completely restricting it. It enables more adaptable, responsive designs while maintaining visual balance and usability.