Skip to: content, navigation

by Tommy Olsson and Paul O’Brien

max-height (CSS property)

Browser support full matrix
IE7+ FF1+ Saf2+ Op9.2+
Full Full Full Full
Spec
Inherited Initial Version
No none CSS2

Example

This style rule assigns a maximum height of 100 pixels to paragraphs within the element with ID "example":

#example p {
  max-height: 100px;
}

Try it yourself!View all demos

Description

This property sets the maximum content height of a block or a replaced element. This maximum height does not include padding, borders, or margins—see The CSS Box Model.

An element that has max-height applied will never be taller than the value specified, even if the height property is set to something larger. There is an exception to this rule, however: if min-height is specified with a value that’s greater than that of max-height, the container’s height will be the largest value, which, in this case, means that the min-height value will in fact be the one that’s applied.

max-height is usually used in conjunction with min-height to produce a height range for the element concerned.

Combining max-height and height

Note that max-height and height should not be applied to the same element using the same unit, as one will override the other. For example, if the height is set to 150px and the max-height set to 60px, the actual height of the element is 60px, and the height declaration becomes redundant:

#example {
  max-height: 60px;
  height: 150px;
}

In the above example, the height of the element will be fixed at 60px.

However, it is acceptable (although it may not be entirely useful) to set max-height and height when the values are different units:

#example {
  max-height: 10em;
  height: 138px;
}

The height in the above example will be whichever is the smaller of the values.

Since the max-height declaration is based on em units, at some stage (due to text resizing) the em height may be smaller than the 138px height we’ve set. In cases such as these, the element will be allowed to shrink from the 138px height, thus keeping track with the em-based text. See the entry on min-height for the reverse of this scenario.

If the contents of a block require more vertical space than is afforded by the limits that have been set, their behavior is defined by the overflow property.

Value

The property takes a CSS length (px, pt, em, and so on), a percentage, or the keyword none. Negative length values are illegal.

Percentage values refer to the height of the containing block. If the height of the containing block is not specified explicitly (that is, it depends on content height), and this element is not absolutely positioned, the percentage value is treated as none.

Compatibility

IE5.5None
6.0None
7.0Full
Firefox1.0Full
1.5Full
2.0Full
Safari1.3Partial
2.0Full
3.0Full
Opera9.2Full
9.5Full

Safari’s support for this property, when applied to positioned elements, is limited to versions 2.0.2 or above.

Internet Explorer for Windows versions up to and including 7 don’t support the value inherit.

User-contributed notes

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters