CSS Reference ยท Box Model and Sizing
The height property
Reference for the CSS height property: lengths, percentages, auto, content keywords and viewport units, why percentage heights fail, and how to avoid clipping.
The height property sets the preferred height of an element's box. With the default auto, the box is as tall as its content needs, which is almost always what you want for text. Fixed heights are useful for media, bars and decorative boxes, but they are a common source of overflowing and clipped content when text grows.
As with width, the value applies to the content box unless box-sizing is border-box.
.banner { height: 200px; } .page { min-height: 100vh; }
Try it
Syntax
height: auto | <length-percentage [0,โ]> | min-content | max-content | fit-content | fit-content(<length-percentage>) | stretch;
- Initial value:
auto - Applies to: all elements except non-replaced inline elements, table columns and column groups
- Inherited: no
- Animatable: yes, as a length, percentage or calc(). Animating to or from
autoneedsinterpolate-size: allow-keywordsorcalc-size(), which have limited support. - Computed value: a percentage or
autoas specified, otherwise an absolute length
Values
| Value | Description |
|---|---|
auto |
Height is determined by the content and layout mode. |
<length> |
A fixed size such as 300px, 20rem or 50vh. |
<percentage> |
A percentage of the containing block's height, but only if that height is explicitly set; otherwise it behaves like auto. |
min-content, max-content, fit-content |
Content-based keywords. In horizontal writing modes they usually equal auto. |
stretch |
Fill the available height of the container. Newer, with limited support. |
Usage notes
- Percentage heights are the classic trap.
height: 100%does nothing unless every ancestor up to the root has a definite height. Usemin-height: 100vh,100dvhor a flex or grid layout instead. - Prefer
min-heighttoheightfor containers with text. The box can grow when the content, translation or font size requires. - Fixed heights cause overflow. If you must fix a height, decide what happens to extra content:
overflow: auto,hidden, or a design that cannot overflow. 100vhon mobile. The classic viewport can be taller than the visible area when browser toolbars show.100dvhfollows the visible area,100svhalways fits the smallest.- Images. Set only
widthand useheight: auto, or setaspect-ratio, to avoid distortion and layout shift. - Writing modes.
heightis physical. Useblock-sizefor a size that follows the writing direction.
Browser support
Supported in every browser. Dynamic viewport units (dvh, svh, lvh) are supported in all current browsers.
Related
widthโ the horizontal counterpartmin-heightandmax-heightโ limitsblock-sizeโ the logical equivalentaspect-ratioโ keep proportions- Lesson: Width and Height
- Lesson: Viewport Units