CSS max-height Property
The CSS max-height property sets the maximum height an element is allowed to have.
It prevents an element from growing beyond a specified height while allowing it to remain smaller when its content requires less space. The property is commonly used for scrollable panels, responsive images, content containers, and interface components with height restrictions.
max-height Syntax
The max-height property accepts nonnegative lengths, percentages, intrinsic sizing values, or the none keyword.
Basic Syntax
selector {
max-height: 300px;
}
Relative Length
.panel {
max-height: 20rem;
}
Percentage Value
.panel {
max-height: 80%;
}
Viewport-Relative Height
.panel {
max-height: 70vh;
}
Intrinsic Sizing
.panel {
max-height: min-content;
}
.panel {
max-height: max-content;
}
.panel {
max-height: fit-content(300px);
}
No Maximum Height
.panel {
max-height: none;
}
CSS-Wide Keywords
.panel {
max-height: initial;
}
.panel {
max-height: inherit;
}
.panel {
max-height: unset;
}
.panel {
max-height: revert;
}
.panel {
max-height: revert-layer;
}
The initial value is none, which means no maximum height is imposed by this property.
Property Values
| Value | Description |
|---|---|
none |
Specifies no maximum height constraint. This is the default. |
<length> |
Sets a maximum height using units such as px, em, rem, or vh. |
<percentage> |
Sets the maximum height relative to the containing block's height when that height can be resolved. |
min-content |
Uses the intrinsic minimum-content height as the maximum. |
max-content |
Uses the intrinsic maximum-content height as the maximum. |
fit-content() |
Uses intrinsic sizing while considering the supplied available-space limit. |
stretch |
Limits the element's margin box to the available height of its containing block. Browser support varies. |
initial |
Resets the property to none. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to none because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Elements that accept width or height constraints |
| Computed value | As specified, with relative lengths converted to absolute lengths |
| Animation type | By computed value type |
| Physical dimension | Height |
| Logical equivalent | max-block-size in horizontal writing modes |
How It Works
The max-height property establishes an upper limit on an element's used height. It does not automatically make the element as tall as the specified maximum.
Content Below the Maximum
.box {
max-height: 300px;
}
If the content requires only 100 pixels of height, the element can remain 100 pixels tall.
Content Above the Maximum
.box {
max-height: 150px;
overflow: auto;
}
If the content requires more than 150 pixels, the box is limited to the specified maximum and can provide scrolling for the additional content.
No Maximum Height
.box {
max-height: none;
}
The property does not restrict the element's height. Other sizing and layout rules can still impose constraints.
A Maximum Is Not a Minimum
Use min-height when an element must remain at least a certain height. Use max-height when it must not exceed a certain height.
max-height vs. height
The height property specifies an element's preferred height, while max-height limits how large that height can become.
Fixed Height
.box {
height: 200px;
}
The box has a specified height of 200 pixels, subject to other sizing constraints.
Maximum Height
.box {
max-height: 200px;
}
The box can be shorter than 200 pixels when its content requires less space.
Both Properties Together
.box {
height: 400px;
max-height: 250px;
}
The specified height is constrained to 250 pixels by the maximum height.
Comparison
| Property | Purpose |
|---|---|
height |
Sets the preferred height of an element. |
min-height |
Sets the minimum permitted height. |
max-height |
Sets the maximum permitted height. |
Length and Percentage Values
Lengths and percentages provide several ways to limit an element's height.
Pixels
.panel {
max-height: 250px;
}
Pixels provide a fixed maximum height.
Root Font Units
.panel {
max-height: 20rem;
}
The maximum height is calculated relative to the root element's font size.
Viewport Height
.panel {
max-height: 60vh;
}
The maximum height is 60% of the viewport height.
Dynamic Viewport Height
.panel {
max-height: 80dvh;
}
The dvh unit responds to changes in the dynamic viewport, including browser interface elements on supported mobile browsers.
Percentage Height
.container {
height: 400px;
}
.panel {
max-height: 50%;
}
The panel's maximum height is 200 pixels because its containing block has a definite height of 400 pixels.
Unresolved Percentages
If the containing block's height depends on its content, a percentage maximum may not resolve to a definite limit. In common non-absolute positioning contexts, it can behave as though none were specified.
Invalid Negative Values
.panel {
max-height: -100px;
}
Negative lengths and percentages are invalid. Zero and positive values are allowed.
Intrinsic Sizing Values
Intrinsic sizing values allow an element's maximum height to depend on its content and the available layout space.
min-content
.box {
max-height: min-content;
}
Uses the intrinsic minimum-content height as the maximum.
max-content
.box {
max-height: max-content;
}
Uses the intrinsic maximum-content height as the maximum.
fit-content()
.box {
max-height: fit-content(300px);
}
Uses intrinsic sizing while applying the specified available-space limit.
stretch
.box {
max-height: stretch;
}
The stretch keyword constrains the element's margin box to the containing block's available height.
Browser Compatibility
Intrinsic sizing keywords can have different levels of support depending on the value and layout context. Basic length and percentage values are more widely supported.
Minimum and Maximum Height
The min-height and max-height properties establish lower and upper limits on an element's height.
Height Range
.panel {
min-height: 100px;
max-height: 300px;
}
The panel cannot be shorter than 100 pixels or taller than 300 pixels, subject to applicable sizing rules.
Preferred Height with a Maximum
.panel {
height: 400px;
max-height: 250px;
}
The maximum limits the used height to 250 pixels.
Minimum Takes Precedence
.panel {
min-height: 300px;
max-height: 200px;
}
When the minimum height exceeds the maximum height, the minimum takes precedence.
Responsive Height Range
.panel {
min-height: 120px;
max-height: min(400px, 70vh);
}
The min() function selects the smaller of 400 pixels and 70% of the viewport height as the maximum.
Box Sizing
.panel {
box-sizing: border-box;
max-height: 300px;
padding: 20px;
border: 2px solid #64748b;
}
With border-box, the maximum includes padding and borders. With content-box, the maximum applies to the content box.
Overflow Behavior
When content exceeds the maximum height, the overflow property determines how the excess content is handled.
Visible Overflow
.panel {
max-height: 150px;
overflow: visible;
}
Content can extend beyond the panel's box. This is the default overflow behavior.
Automatic Scrolling
.panel {
max-height: 150px;
overflow: auto;
}
Scrollbars are provided when necessary so users can access additional content.
Vertical Scrolling
.panel {
max-height: 150px;
overflow-y: auto;
}
The panel can scroll vertically when its content exceeds the available height.
Hidden Overflow
.panel {
max-height: 150px;
overflow: hidden;
}
Content extending beyond the box is clipped. Avoid this approach when it would make important information inaccessible.
Important Consideration
Setting max-height alone does not create a scrollable area. An appropriate overflow declaration is also required.
Responsive Layouts
Maximum height constraints help interface components adapt to different screen sizes.
Viewport-Constrained Panel
.content-panel {
max-height: 70vh;
overflow-y: auto;
}
The panel can grow with its content but cannot exceed 70% of the viewport height.
Responsive Maximum
.content-panel {
max-height: 400px;
overflow-y: auto;
}
@media (max-width: 767px) {
.content-panel {
max-height: 250px;
}
}
The maximum height becomes smaller on narrower screens.
Using min()
.content-panel {
max-height: min(450px, 75vh);
overflow-y: auto;
}
The panel uses whichever maximum is smaller, preventing it from becoming excessively tall on short viewports.
Using clamp()
.content-panel {
max-height: clamp(200px, 50vh, 500px);
}
The maximum height adjusts with the viewport while remaining between 200 and 500 pixels.
Mobile Viewports
.content-panel {
max-height: 75vh;
max-height: 75dvh;
}
The first declaration provides a fallback. Browsers supporting dynamic viewport units use the second declaration.
Images and Media
The max-height property can constrain images and other replaced elements without requiring a fixed height.
Limit Image Height
img {
max-height: 300px;
width: auto;
height: auto;
}
The image retains its intrinsic aspect ratio while respecting the maximum height under normal intrinsic sizing conditions.
Responsive Image
.article-image {
max-width: 100%;
max-height: 400px;
width: auto;
height: auto;
}
The image is constrained by both the available width and the maximum height.
Image Inside a Fixed Frame
.image-frame {
height: 250px;
overflow: hidden;
}
.image-frame img {
width: 100%;
height: 100%;
max-height: 250px;
object-fit: cover;
}
The image fills the frame and may be cropped to preserve its aspect ratio.
Video Element
video {
max-width: 100%;
max-height: 70vh;
width: auto;
height: auto;
}
This limits video dimensions while allowing the media to retain its aspect ratio.
max-height Example
This example compares an unrestricted content panel with a panel limited to 120 pixels in height. The limited panel becomes scrollable when its content exceeds the maximum.
HTML Example
<div class="max-height-demo">
<div class="max-height-item">
<h3>max-height: none</h3>
<div class="max-height-panel no-limit">
<p>HTML defines the structure of a web page.</p>
<p>CSS controls colors, spacing, and layout.</p>
<p>JavaScript adds interactive features.</p>
<p>Responsive design adapts to screen sizes.</p>
</div>
</div>
<div class="max-height-item">
<h3>max-height: 120px</h3>
<div class="max-height-panel limited">
<p>HTML defines the structure of a web page.</p>
<p>CSS controls colors, spacing, and layout.</p>
<p>JavaScript adds interactive features.</p>
<p>Responsive design adapts to screen sizes.</p>
</div>
</div>
</div>
CSS Example
.max-height-demo {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 16px;
}
.max-height-item {
min-width: 0;
}
.max-height-item h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.max-height-panel {
padding: 15px;
background-color: #dbeafe;
border: 1px solid #2563eb;
border-radius: 6px;
}
.max-height-panel p {
margin: 0 0 12px;
}
.max-height-panel p:last-child {
margin-bottom: 0;
}
.no-limit {
max-height: none;
}
.limited {
max-height: 120px;
overflow-y: auto;
}
@media (max-width: 575px) {
.max-height-demo {
grid-template-columns: 1fr;
}
}
max-height: none
HTML defines the structure of a web page.
CSS controls colors, spacing, and layout.
JavaScript adds interactive features.
Responsive design adapts to screen sizes.
max-height: 120px
HTML defines the structure of a web page.
CSS controls colors, spacing, and layout.
JavaScript adds interactive features.
Responsive design adapts to screen sizes.
The unrestricted panel expands to display all its content. The limited panel remains within the maximum height and provides vertical scrolling for additional text.
Responsive Maximum Height Example
.responsive-panel {
max-height: min(350px, 65vh);
overflow-y: auto;
}
This rule limits the panel to whichever is smaller: 350 pixels or 65% of the viewport height.
Common Mistakes
Confusing max-height with height
.box {
max-height: 300px;
}
This does not force the element to become 300 pixels tall. It establishes an upper limit.
Using Negative Values
.box {
max-height: -100px;
}
Negative lengths and percentages are invalid.
Ignoring Overflow
Content can extend beyond a constrained element unless overflow behavior is handled appropriately.
Expecting Percentages to Always Work
.box {
max-height: 50%;
}
The containing block generally needs a resolvable height for the percentage maximum to apply as expected.
Conflicting Minimum and Maximum Heights
.box {
min-height: 400px;
max-height: 200px;
}
The minimum height takes precedence when it exceeds the maximum.
Clipping Important Content
.box {
max-height: 150px;
overflow: hidden;
}
Hidden overflow can make content inaccessible. Use scrolling or a different layout when users need access to the entire content.
Confusing Physical and Logical Sizing
max-height always limits the physical height. Use max-block-size when the maximum should follow the block axis of the writing mode.
Forgetting box-sizing
Padding and borders can affect the total rendered height. Use box-sizing: border-box when the specified maximum should include them.
Important Considerations
Keep the following points in mind when using max-height:
- The property sets an upper limit on an element's physical height.
- Its initial value is
none. - The property is not inherited.
- It does not force an element to expand to the maximum height.
- Length and percentage values must be nonnegative.
- Percentage values depend on the containing block's height.
- Viewport units can provide responsive height limits.
- Intrinsic sizing values depend on content and layout context.
min-heighttakes precedence when its minimum exceeds the maximum.heightspecifies a preferred height, whilemax-heightconstrains it.- Overflow behavior must be considered when content exceeds the limit.
box-sizingaffects whether padding and borders are included in the specified maximum.- The property can constrain images, videos, and other replaced elements.
max-heightalways controls the physical height, regardless of writing mode.- Use
max-block-sizewhen the constraint should follow the logical block axis. - Test scrollable content on smaller screens and with keyboard navigation.
Browser Support
The CSS max-height property is Baseline - Widely available. It is supported in current versions of all major browsers, including Chrome, Edge, Firefox, and Safari.
Basic length, percentage, and none values have broad support. Newer intrinsic sizing values, such as stretch, may have different browser compatibility requirements.
For current compatibility information, visit Can I Use? and search for max-height.
When using newer sizing functions or keywords, check compatibility for the specific value rather than relying only on general property support.
Specifications
The max-height property is defined in CSS Sizing Module Level 3.
CSS Sizing Module Level 3: Maximum Size Properties
The specification defines maximum size constraints, accepted sizing values, and their relationship to minimum and preferred sizes.
For additional documentation and examples, consult MDN Web Docs.
