Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

max-height — maximum height

max-height limits an element's height without forcing it to reach that height. Learn how min-height takes priority, how box-sizing affects the limit, and how to handle overflowing content.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

max-height: none | <length-percentage [0,∞]> | min-content | max-content | fit-content(<length-percentage [0,∞]>);

max-height sets an upper height constraint, not a target height. Its initial value, none, means no limit; the property is not inherited. If min-height exceeds this limit, the minimum constraint wins.

Lengths and percentages must be nonnegative. A percentage refers to the containing block's height; content-dependent heights involve special layout rules. box-sizing determines whether the limit measures the content box or the box including padding and borders. The displayed syntax comes from the specification, not a guarantee of browser support for every keyword.

max-height alone does not provide scrolling or clip content. Use overflow: auto to make overflowing content scrollable. In the example, height: 160px is constrained by max-height: 80px; with box-sizing: border-box, that limit includes the borders.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Capping the height of a scrollable panel</title>
  <style>
    #panel {
      box-sizing: border-box;
      width: 12rem;
      height: 160px;
      max-height: 80px;
      overflow: auto;
      border: 2px solid;
    }
    .content { height: 140px; background: lightblue; }
  </style>
</head>
<body>
  <h1 id="panel-heading">Scrollable panel</h1>
  <div id="panel" role="region" aria-labelledby="panel-heading" tabindex="0">
    <div class="content">This content is taller than the panel.</div>
  </div>
  <script>
    const panel = document.getElementById('panel');
    console.log(`Panel border-box height: ${panel.getBoundingClientRect().height}px`);
    panel.scrollTop = 1;
    console.log(`Can scroll vertically: ${panel.scrollHeight > panel.clientHeight && panel.scrollTop > 0}`);
    panel.scrollTop = 0;
  </script>
</body>
</html>

Result

Panel border-box height: 80px
Can scroll vertically: true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources