π Introduction
Just like width controls the horizontal size, height, min-height, andmax-height control an elementβs vertical space. These properties are essential for handling content blocks, layouts, banners, cards, and any element that needs predictable height behavior β especially in responsive design.
π What is height?
The height property defines the elementβs vertical size. It can use units like px, %, vh, rem, auto.
height Example
.box {
height: 300px;
}This makes the element exactly 300px tall β unless overridden by min/max constraints.
Common Use Cases
- Fixed banners or hero sections
- Uniform-height cards
- Containers inside grid or flex layouts
Note
π What is min-height?
min-height sets the minimum height an element is allowed to shrink to, ensuring content always has enough space to display.
min-height Example
.card {
min-height: 200px;
}Even if content is tiny, the card will stay at least 200px tall.
Common Use Cases
- Preventing content from being squished on mobile
- Maintaining uniform card heights
- Ensuring UI elements remain visually balanced
Note
π What is max-height?
max-height sets the maximum height an element can grow to. It prevents elements from expanding too much when content increases.
max-height Example
.accordion {
max-height: 400px;
overflow-y: auto;
}Useful for scrollable sections like dropdowns, modals, and accordions.
Common Use Cases
- Limiting image or video height
- Scroll-enabled containers
- Preventing oversized UI blocks
π How These Properties Interact
height + min-height + max-height
.box {
height: 50vh;
min-height: 300px;
max-height: 600px;
}The element will:
- Try to be 50% of viewport height
- Never shrink below 300px
- Never exceed 600px
π§ Priority Rules (Important!)
| Property | Overrides |
|---|---|
| min-height | Overrides height when height < min-height |
| max-height | Overrides height when height > max-height |
| height | Only respected between min & max limits |
Note
π§ͺ Practical Examples
1οΈβ£ Responsive Hero Section
Hero Section
.hero {
height: 60vh;
min-height: 400px;
max-height: 900px;
}Works on all monitors: tall enough for laptops, not too huge on ultra-wide screens.
2οΈβ£ Scrollable Modal Window
Modal Example
.modal {
max-height: 500px;
overflow-y: auto;
}The modal scrolls internally when content exceeds 500px.
3οΈβ£ Flexible Card Layout
Card Layout
.card {
min-height: 180px;
max-height: 350px;
}Perfect for dynamic content while maintaining visual consistency.
4οΈβ£ Prevent Overflow with min-height
Avoiding Overflow
.container {
height: auto;
min-height: 100px;
}Ensures content has at least 100px space even when empty.
π± Responsive Example Using Media Queries
Responsive Height Adjustment
.sidebar {
min-height: 400px;
}
@media (max-width: 900px) {
.sidebar {
min-height: 250px;
}
}Sidebar becomes smaller on tablets but still maintains a safe minimum height.
β οΈ Common Mistakes
- Using fixed height with dynamic text β causes overflow
- Setting height instead of min-height for flexible layouts
- Forgetting that percentage heights need a parent with a defined height
- Misusing max-height without enabling scroll (overflow-y: auto)
Note
π₯ Summary
height, min-height, and max-heightgive you complete control over vertical sizing in your layouts. Mastering these properties is essential for responsive UI design and preventing overflow issues.
Learn more βMDN Docs π