Max Height in CSS
Overview
The max-height property specifies the maximum height of an HTML element.
- The height's value cannot be larger than the height's maximum value.
- The content will overflow if it exceeds the maximum height.
- The max height has no effect if the content's height is smaller than the maximum.
- The max height CSS property applies to block level and replaced elements.
NOTE :
- No matter where we place the max height property in our declaration, it overrides the height property.
- The min-height property overrides the max height property.
Syntax for Max Height in CSS
The CSS max height property accepts keyword values such as none, max-content, min-content, etc. It supports numeric values such as length in px, em, and percentage.
In the further sections of the article, we will discuss the values that the max-height property supports in more detail .
Property Values of Max Height in CSS
Value | Description | Syntax |
---|---|---|
It defines the maximum height in px, cm, etc. | max-height: | |
It specifies the maximum height in percent of the containing block. | max-height: | |
none | It specifies that there's no limit on the height of the box. | max-height |
max-content | The max-content value indicates the content's intrinsic maximum-height. The max-content height represents the height of a paragraph if each paragraph is one long sentence with no soft line breaks. | max-height |
min-content | A min-content sizing keyword specifies the content's intrinsic minimum height. The min-content height represents the tallest a box can get by breaking all lines at all potential breakpoints. | max-height |
fit-content | The fit-content value adjusts the height according to this formula: min(maximum size, max(minimum size, argument)) | max-height: fit-content(20em); |
Note: The fit-content value acts the same as that of max-content.
Examples of Max Height in CSS
Setting max-height using Percentage and Keyword Values
In this example, we'll set the maximum element height using the various values supported by the max height css property.
HTML:
CSS:
Output:
We can use the CSS overflow property to handle the overflowing content. overflow:auto; adds a scroll-bar if the content overflows.
Browser Support
The following browsers support the CSS max-height property:
Browser | Version |
---|---|
Google Chrome | 1.0 |
Safari | 1.0 |
Mozilla Firefox | 1.0 |
Microsoft Edge | 12.0 |
Opera | 4 |
Chrome Android | 18 |
Firefox for Android | 4 |
Opera Android | 10.1 |
Safari on iOS | 1 |
Samsung Internet | 1.0 |
WebView Android | 4.4 |
Conclusion
- The max height property in CSS defines the maximum height of an element.
- The content will overflow if it exceeds the maximum height.
- The max height property doesn't work if the content is smaller than the maximum-height.
- The CSS max height property applies to block level and replaced elements.
- The max height CSS property takes keyword values such as none, max-content, min-content, etc.
- It supports numeric values such as length in px, em, and percentage.
Related Properties
- CSS width property
- min-height in CSS
- min-width in CSS