Max-height and max-width - CSS property.

max-height
max-width
max-inline-size
max-block-size

Summary of characteristics of the max-height property

Quick description
Sets a limit for the maximum height of the element.
Status
Standard
Usable for
HTML
Predefined values
none | auto
Percentages
Calculated relative to the height of the parent block, if the latter is specified.
Initial value
none
Inherited by default
No.
Animation type
Computed value : during an animation, the max-height property gradually changes from one value to another.
W3C Module
CSS Box Sizing Module
References (W3C)
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: REC (Recommendation)

Description of the properties.

The physical properties max-width and max-height.

The two properties max-height and max-width set the maximum limits for the dimensions of an element. They take precedence over the width and height properties.

These two properties are interesting to combine with dimensions expressed in percentages. The result is an element that adapts to the size of the screen but remains within acceptable limits.

See also the min-height and min-width properties.

The logical properties max-block-size and max-inline-size.

Both of these properties act according to the way the element's language is written. For Latin languages, max-inline-size is equivalent to max-width and max-block-size is equivalent to max-height.

The and properties also exist.

Support for writing mode and language.

The logical properties max-inline-size and max-block-size have the same role as the physical properties max-width and max-height, except that they take into account the direction and mode of writing. This gives the following equivalences, depending on the language.

   

<——— max-width ———>
The direction and writing mode of this text adapt to the chosen language.
<——— max-height ———>

Values for the max-width and max-height properties.

The logical properties max-block-size and max-inline-size have identical syntax. They are not mentioned in the examples below.

  • max-height: auto; max-width: auto;

    The maximum dimensions are determined by the browser based on the values assigned to width and height.

  • max-height: none; max-width: none;

    There is no limit on the height or width of the element.

  • max-height: 400px; max-width: 400px;

    A limit is set for the dimensions of the element. The value shown must be a positive value, of the dimension type. See CSS dimension units.

    If they are percentages, they are calculated in relation to the dimensions of the parent, with the following conventions:

    1. max-width is calculated relative to the width of the parent element.
    2. max-height is calculated relative to the height of the parent element, provided that this is specified, otherwise the percentages are assimilated to auto.
  • max-width: min-content; max-width: max-content; max-width: fit-content;

    These three presets allow you to adjust the minimum width of an element based on its content:

    • min-content : the smallest dimension, which does not require you to cut words but by inserting as many line breaks as necessary.
    • max-content : the largest dimension, i.e. by inserting any line breaks that would not be imposed in the document.
    • fit-content : the widest dimension that is compatible with the available space.

    For examples of how they work and more detailed explanations, refer to the description of the min-content values, max-content and fit-content.

  • max-height: stretch; max-width: stretch;

    Awaiting definition.

  • max-height: initial; (none) max-height: inherit; max-height: revert; max-height: revertLayer; max-height: unset;
    max-width: initial; (none) max-width: inherit; max-width: revert; max-width: revertLayer; max-width: unset;

    Common values ​​are presented on these pages: initial, inherit, revert, revert-layer, unset.

Animation of the max-width property.

The example is written for the max-width property, but would of course work the same way for one of the max-height, max-block-size, or max-inline-size.

It is quite rare to animate these properties because their effect may very well be invisible on the screen if the actual dimensions of the element do not exceed the maximum values.

Manipulating the max-height property programmatically.

Change the value of max-height with Javascript.

There are two Javascript syntaxes that allow you to change the value of max-height. The first uses the usual notation in CSS: kebab-case for the name of the property (a hyphen to separate words), and the second uses the more common notation in Javascript, in (a capital letter to separate words).

Javascript
let el = document.getElementById('id'); el.style['max-height'] = '1rem'; // or let el = document.getElementById('id'); el.style.maxHeight = '1rem';

Read in Javascript the value of max-height.

This code retrieves the value of max-height when it has been initialized in the style attribute of the element (i.e. in the HTML code). The value assigned via a CSS selector is not taken into account.

The value is returned in the same unit that is used to define it, in the style attribute.

Javascript
let el = document.getElementById('id'); let value = el.style['max-height']; // or let el = document.getElementById('id'); let value = el.style.maxHeight;

Read the computed value of max-height in Javascript.

The computed value is that which results from the evaluation of the relative units (in particular the percentage) and the resolution of the cascade of the inheritances. The computed value is always set: this can be a value assigned via the style attribute in the HTML, a value assigned via a CSS selector, an inherited value, or the initial value of the property.

In the case of max-height, the maximum height or width is returned in pixels, regardless of the unit used to define it (except percentages).

Javascript
let el = document.getElementById('id'); let value = window.getComputedStyle(el).getPropertyValue('max-height');

Change the value of the max-height property with JQuery.

JQuery

$('#id').css('max-height', '1rem');
// or
$('#id').css('maxHeight', '1rem');

Read the computed value of the max-height property with JQuery.

The property can be read with the following codes. As in Javascript, the maximum height and width are returned in pixels or percentages.

JQuery
let value = $('#id').css('max-height');

Demonstration.

Use the buttons below to view the value applied for the first button, and the calculated value for the second button. The calculated value is interesting to see how the value is stored (serialized). In particular, it can be seen that the values expressed in units other than pixels are converted to pixels, except for the percentage.

Browsers compatibility.

No compatibility issues with the max-height and max-width properties, or with equivalent logical properties. However, the stretch value is still poorly recognized (2024) and should be used with caution. The same is true for the fit-content() function. Firefox   allows you to activate this possibility by modifying a flag (access flags on Firefox).

Column 1
Support for max-width and max-height properties to set the maximum dimensions of an element.
Column 2
Support for max-inline-size and max-block-size logical properties (taking into account how the element's language is written).
Column 3
Support for the stretch value for the max-height or max-width properties.
Column 4
Support for the fit-content() function applied to the max-height property.

Notes:

(2) From CSS2.1, Firefox applies max-height to tables as well.

(1) Uses the non-standard -wekbit-fill-available value.

(3) Disabled by default. Can be activated with the layout.css.fit-content_function.enabled flag.

1
max-width
max-height

properties
2
max-inline-size
max-block-size

properties
3
stretch
value
4
fit-content()
function
Estimated overall support.
97%
96%
0%
0%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

KaiOS Browser

Opéra Mobile

Edge

Opéra

Safari

Safari sur IOS

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Samsung Internet

Firefox

Chrome

Baidu Browser

QQ Browser

UC Browser pour Androïd

Opéra mini

max-height property historic

See also, in the same module as max-height

The CSS specifications published by the W3C are organized into modules. The max-height and max-width properties are part of the CSS Box Sizing Module, as well as the logical max-inline-size and max-block-size. The following definitions are also described in this same module.

Properties:

aspect-ratio
Langue française
Sets the ratio of the width to the height of the element.
block-size
Defines the dimension of an element following the direction of the blocks.
box-sizing
Langue française
Defines how block dimensions are calculated: whether or not to include borders and padding.
contain-intrinsic-block-size
Langue française
Sets the dimension of the element in the direction of the blocks, when the element is confined.
contain-intrinsic-height
Langue française
Sets the intrinsic height of the element when it is confined.
contain-intrinsic-inline-size
Langue française
Sets the dimension of the element in the direction of the lines, when the element is confined.
contain-intrinsic-size
Langue française
Sets the dimensions of the element (blocks and rows) when the element is confined.
contain-intrinsic-width
Langue française
Sets the intrinsic width of the element when it is confined.
height
Sets the height of the element.
inline-size
Langue française
Sets the dimension of the element in the direction of the lines.
max-width
Sets the maximum width of the element.
min-height
Langue française
Sets the minimum height of the element.
min-width
Langue française
Sets the minimum width of the element.
width
Langue française
Sets the width of the element.

Functions:

fit-content()
Calculates a dimension based on content and boundaries.

Values:

fit-content
Calculates the size of an element based on its contents and available space.
max-content
Gives the dimension of a text without inserting line breaks.
min-content
Donne la dimension d'un texte après avoir inséré le plus possible de retours à la ligne.