CSS height Property
The CSS height property sets the preferred height of an element, controlling the vertical size of its content or border box depending on the box-sizing model.
It supports fixed lengths, percentages, viewport-relative units, intrinsic sizing keywords, and functions such as calc() and anchor-size(). The final height can also be affected by content, minimum and maximum height constraints, and the element's layout context.
height Syntax
The height property accepts a non-negative length, percentage, sizing keyword, or supported sizing function.
Basic Syntax
selector {
height: auto;
}
Fixed Height
.box {
height: 200px;
}
Percentage Height
.box {
height: 50%;
}
Viewport Height
.section {
height: 100vh;
}
Intrinsic Height
.box {
height: max-content;
}
Calculated Height
.panel {
height: calc(100vh - 80px);
}
Fit-Content Height
.box {
height: fit-content;
}
Stretch Height
.box {
height: stretch;
}
Anchor-Based Height
.anchored-panel {
height: anchor-size(height);
}
CSS-Wide Keywords
.box {
height: initial;
}
.box {
height: inherit;
}
.box {
height: unset;
}
.box {
height: revert;
}
.box {
height: revert-layer;
}
Negative lengths and percentages are invalid. Some newer sizing keywords and functions have limited browser support and should be used with appropriate fallbacks.
Property Values
The following values control how the preferred height of an element is calculated.
| Value | Description |
|---|---|
auto |
Allows the browser to determine the height according to content and layout rules. |
<length> |
Sets a height using a length such as px, rem, em, or a viewport unit. |
<percentage> |
Sets a height relative to the containing block's height when the percentage can be resolved. |
min-content |
Uses the element's minimum intrinsic height. |
max-content |
Uses the element's maximum intrinsic height. |
fit-content |
Uses an intrinsic size constrained by the available space. |
fit-content() |
Uses a specified limit when supported by the applicable sizing syntax and browser. |
stretch |
Sizes the element's margin box to fill the available height of its containing block. |
calc() |
Calculates a height from compatible CSS values. |
anchor-size() |
Uses a dimension of an anchor element when anchor sizing is available. |
initial |
Resets the property to auto. |
inherit |
Inherits the computed height value from the parent element. |
unset |
Resets this non-inherited property to auto. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | No |
| Applies to | Elements that accept width and height sizing, excluding non-replaced inline elements and certain table elements |
| Computed value | As specified, with applicable relative lengths converted to absolute lengths |
| Animation type | Length, percentage, or calculated-value interpolation when compatible |
| Percentages | Refer to the containing block's height |
| Related properties | min-height, max-height, width, box-sizing, block-size |
The final used height may differ from the specified value because of minimum and maximum constraints, box sizing, aspect ratios, or layout-specific sizing rules.
How It Works
The height property provides a preferred vertical size. The browser calculates the final used height according to the element's formatting context, box model, and applicable sizing constraints.
Set a Fixed Height
.box {
height: 150px;
}
With the default content-box sizing model, this sets the content box to 150 pixels tall. Padding and borders increase the total rendered height.
Allow Content to Determine Height
.box {
height: auto;
}
For a normal block element, the height typically grows to accommodate its in-flow content.
Set Height and Width Together
.box {
width: 250px;
height: 150px;
}
Apply Height to an Inline Element
.label {
display: inline-block;
height: 40px;
}
Ordinary non-replaced inline elements, such as a typical <span>, do not respond to height in the same way as block or inline-block elements.
Important Consideration
A declared height does not guarantee that content will fit. If the content exceeds the available space, overflow behavior determines whether it remains visible, is clipped, or becomes scrollable.
Fixed Heights
Fixed heights are useful for elements that require predictable vertical dimensions, such as icons, image containers, and interface controls.
Pixel Height
.box {
height: 200px;
}
Relative Font-Based Height
.box {
height: 12rem;
}
Element Font-Based Height
.box {
height: 10em;
}
The em unit is relative to the element's computed font size, while rem is relative to the root element's font size.
Height with Padding
.box {
height: 200px;
padding: 20px;
border: 2px solid #333333;
}
With box-sizing: content-box, the total border-box height becomes 244 pixels.
Important Consideration
Fixed heights can cause content to overflow when text wraps, font sizes increase, or additional content is added. Use min-height instead when the element should be allowed to grow.
Percentage Heights
A percentage height is generally calculated relative to the height of the element's containing block.
Parent with a Definite Height
.parent {
height: 400px;
}
.child {
height: 50%;
}
The child receives a height of 200 pixels when its containing block is the 400-pixel-tall parent.
Full-Height Child
.parent {
height: 300px;
}
.child {
height: 100%;
}
Percentage Height with an Automatic Parent
.parent {
height: auto;
}
.child {
height: 50%;
}
In normal block layout, the child's percentage height generally cannot resolve against an indefinite parent height and behaves as auto. Flexbox, Grid, and positioned layout have additional sizing rules.
Full-Height Page
html,
body {
height: 100%;
}
.page {
min-height: 100%;
}
Setting the root and body heights can provide a definite sizing chain for percentage-based descendants in appropriate layouts.
Important Consideration
A percentage height does not automatically mean a percentage of the viewport. It depends on the containing block and the rules used to resolve its height.
Viewport-Relative Heights
Viewport-relative units size elements using the dimensions of the browser's viewport rather than a parent element's height.
Full Viewport Height
.hero {
height: 100vh;
}
Half Viewport Height
.panel {
height: 50vh;
}
Dynamic Viewport Height
.hero {
min-height: 100dvh;
}
The dvh unit follows the dynamic viewport height, which can change as mobile browser interface controls appear or disappear.
Small Viewport Height
.hero {
min-height: 100svh;
}
The svh unit uses the small viewport size, helping avoid content being obscured by expanded mobile browser controls.
Large Viewport Height
.hero {
min-height: 100lvh;
}
Fallback for Dynamic Viewport Units
.hero {
min-height: 100vh;
min-height: 100dvh;
}
Browsers that do not support dvh can retain the earlier vh declaration.
Important Consideration
Use min-height for full-screen content sections that may need to grow beyond the viewport. A strict height: 100vh can cause overflow when the content is taller than the available space.
Automatic Height
The default auto value allows the browser to calculate an element's height according to its layout and content.
Content-Driven Height
.content-box {
height: auto;
}
Flexible Content Section
.content-section {
height: auto;
padding: 24px;
}
For a normal block element, adding more in-flow content generally increases the element's height.
Reset a Previously Fixed Height
.panel {
height: 300px;
}
@media (max-width: 600px) {
.panel {
height: auto;
}
}
Important Consideration
The meaning of auto depends on the layout context. An automatically sized Flexbox or Grid item may stretch, while an absolutely positioned element follows different sizing rules.
Intrinsic Sizing
Intrinsic sizing keywords allow the browser to determine height using the element's content and the constraints of its layout.
Minimum Intrinsic Height
.box {
height: min-content;
}
Maximum Intrinsic Height
.box {
height: max-content;
}
Fit-Content Height
.box {
height: fit-content;
}
The fit-content keyword uses intrinsic sizing while considering the available space.
Stretch Height
.box {
height: stretch;
}
The stretch keyword attempts to fill the available containing-block height using the element's margin box, rather than sizing only its content box to that height.
Important Consideration
Intrinsic height values can produce similar results for ordinary text blocks. Differences become more apparent with replaced elements, constrained layouts, and complex formatting contexts.
The stretch keyword is newer than traditional height values and may require a fallback for browsers without support.
Height Functions
CSS sizing functions provide more flexible ways to calculate element heights.
Using calc()
.content {
height: calc(100vh - 100px);
}
This subtracts 100 pixels from the viewport height.
Using min()
.panel {
height: min(400px, 60vh);
}
The browser uses the smaller of the two calculated heights.
Using max()
.panel {
height: max(200px, 30vh);
}
The browser uses the larger of the two values.
Using clamp()
.panel {
height: clamp(200px, 40vh, 500px);
}
The preferred height is 40% of the viewport height, constrained between 200 and 500 pixels.
Using anchor-size()
.reference-box {
anchor-name: --reference;
}
.anchored-panel {
position: absolute;
position-anchor: --reference;
height: anchor-size(height);
}
When supported, anchor-size() can use the height of an associated anchor element. This is an advanced feature with separate compatibility requirements.
Important Consideration
Calculated heights still follow the normal sizing and overflow rules. Functions do not automatically prevent content from exceeding the resulting dimensions.
Height and box-sizing
The box-sizing property determines whether a declared height applies to the content box or includes padding and borders.
Content-Box Sizing
.box {
box-sizing: content-box;
height: 200px;
padding: 20px;
border: 2px solid #333333;
}
The content box is 200 pixels tall. The total border-box height is 244 pixels.
Border-Box Sizing
.box {
box-sizing: border-box;
height: 200px;
padding: 20px;
border: 2px solid #333333;
}
The declared 200-pixel height includes the content, padding, and borders. The content box receives the remaining space.
Global Border-Box Rule
*,
*::before,
*::after {
box-sizing: border-box;
}
This is a common layout convention because it makes element dimensions easier to calculate.
Important Consideration
Margins remain outside the border box in both sizing models. A declared height does not include margins.
min-height and max-height
The min-height and max-height properties constrain the final used height of an element.
Minimum Height
.panel {
min-height: 200px;
}
The panel is at least 200 pixels tall but can grow when its content requires additional space.
Maximum Height
.panel {
max-height: 400px;
overflow-y: auto;
}
The panel can grow until it reaches 400 pixels. Additional content becomes scrollable when necessary.
Combine All Three Properties
.panel {
min-height: 150px;
height: 250px;
max-height: 400px;
}
Minimum Height Overrides a Smaller Height
.panel {
min-height: 200px;
height: 100px;
}
The minimum height constraint prevents the used height from being less than 200 pixels.
Maximum Height Overrides a Larger Height
.panel {
height: 500px;
max-height: 300px;
}
The maximum height constraint limits the used height to 300 pixels.
Important Consideration
If min-height exceeds max-height, the minimum constraint takes precedence.
Height in Flexbox
Flexbox uses its own sizing rules, so a flex item's height may be influenced by the container's direction, alignment, and available space.
Row Flex Container
.container {
display: flex;
align-items: stretch;
}
.item {
height: auto;
}
In a row-oriented flex container, items with an automatic cross size can stretch to the container's cross size when the alignment rules permit it.
Fixed-Height Flex Container
.container {
display: flex;
height: 300px;
align-items: center;
}
.item {
height: 100px;
}
Column Flex Container
.container {
display: flex;
flex-direction: column;
height: 400px;
}
.content {
flex: 1;
min-height: 0;
}
The content item can grow to occupy available main-axis space. The min-height: 0 declaration allows it to shrink below its automatic content-based minimum when needed.
Important Consideration
In a column flex container, height participates in main-axis sizing and may be adjusted by flex growth or shrinkage. Use flex, flex-basis, and minimum size constraints when appropriate.
Height in CSS Grid
CSS Grid determines track sizes and item placement independently of an item's declared height.
Grid Container Height
.container {
display: grid;
height: 400px;
grid-template-rows: 100px minmax(0, 1fr);
}
Fixed-Height Grid Item
.item {
height: 120px;
}
The item receives a preferred height of 120 pixels rather than automatically stretching to fill its grid area.
Stretch an Automatically Sized Item
.container {
display: grid;
grid-template-rows: 150px;
align-items: stretch;
}
.item {
height: auto;
}
An automatically sized grid item can stretch to fill the available height of its grid area when alignment rules permit it.
Important Consideration
The grid-template-rows property controls row track sizes. The height property controls an individual element's preferred height.
Height and aspect-ratio
The aspect-ratio property defines a preferred width-to-height ratio that can influence an element's automatically calculated dimensions.
Automatic Height from Width
.media-box {
width: 320px;
height: auto;
aspect-ratio: 16 / 9;
}
For a suitable non-replaced element, the preferred ratio can produce a height of 180 pixels.
Fixed Height with Automatic Width
.media-box {
width: auto;
height: 180px;
aspect-ratio: 16 / 9;
}
The ratio may influence the automatic width, depending on the element's layout context.
Both Dimensions Explicitly Defined
.media-box {
width: 300px;
height: 200px;
aspect-ratio: 16 / 9;
}
When both preferred dimensions are definite, the aspect ratio generally does not override those dimensions.
Responsive Image
img {
max-width: 100%;
height: auto;
}
Using height: auto helps preserve the intrinsic aspect ratio of an image when its width changes.
Important Consideration
Aspect ratios participate in preferred sizing but remain subject to minimum and maximum size constraints.
Overflow Behavior
When an element has a constrained height, its content may exceed the available space.
Default Overflow
.box {
height: 100px;
overflow: visible;
}
Content that exceeds the box may remain visible outside its boundaries.
Hide Overflowing Content
.box {
height: 100px;
overflow: hidden;
}
Scrollable Content
.box {
height: 150px;
overflow-y: auto;
}
Clip Overflowing Content
.box {
height: 150px;
overflow: clip;
}
Prefer a Minimum Height for Variable Content
.box {
min-height: 150px;
height: auto;
}
This allows the element to grow beyond 150 pixels when necessary.
Important Consideration
Do not hide important content merely to maintain a fixed height. For interactive regions, ensure that users can reach all content and controls.
Logical Sizing
The height property controls the physical vertical dimension. CSS also provides logical sizing properties that follow the writing mode.
Physical Height
.box {
height: 200px;
}
Logical Block Size
.box {
block-size: 200px;
}
In a typical horizontal writing mode, block-size corresponds to physical height.
Vertical Writing Mode
.box {
writing-mode: vertical-rl;
block-size: 200px;
}
In a vertical writing mode, the block axis is horizontal, so block-size corresponds to a physical width rather than a physical height.
Logical Minimum and Maximum Sizes
.box {
min-block-size: 100px;
max-block-size: 400px;
}
Important Consideration
Use logical sizing properties when designing components that must adapt to different writing modes. Use height when the physical vertical dimension is intended.
Inheritance
The height property is not inherited. A child element does not automatically receive the parent's declared height.
Parent Height
.parent {
height: 300px;
}
Child with Automatic Height
.child {
height: auto;
}
Explicit Inheritance
.child {
height: inherit;
}
The inherit keyword copies the parent's computed height value. This is different from setting a percentage height relative to the containing block.
Reset to the Initial Value
.child {
height: initial;
}
Reset with unset
.child {
height: unset;
}
Because height is not inherited, unset resets it to auto.
height Example
This example compares a fixed-height box with an automatically sized box. Both contain the same text, but the first maintains a height of 100 pixels while the second expands according to its content.
HTML Example
<div class="height-demo">
<div class="demo-panel">
<h3>Fixed Height: 100px</h3>
<div class="demo-box fixed-height">
<p>This box has a fixed height. Its content does not change the declared height.</p>
</div>
</div>
<div class="demo-panel">
<h3>Automatic Height</h3>
<div class="demo-box auto-height">
<p>This box has an automatic height. Its content determines how tall the box becomes.</p>
</div>
</div>
</div>
CSS Example
.height-demo {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 16px;
}
.height-demo .demo-panel {
min-width: 0;
}
.height-demo h3 {
margin: 0 0 10px;
font-size: 1rem;
}
.height-demo .demo-box {
box-sizing: border-box;
padding: 12px;
background-color: #e8eef8;
border: 2px solid #2456a6;
border-radius: 4px;
}
.height-demo .demo-box p {
margin: 0;
}
.height-demo .fixed-height {
height: 100px;
}
.height-demo .auto-height {
height: auto;
}
@media (max-width: 575px) {
.height-demo {
grid-template-columns: 1fr;
}
}
Fixed Height: 100px
This box has a fixed height. Its content does not change the declared height.
Automatic Height
This box has an automatic height. Its content determines how tall the box becomes.
The fixed-height box remains 100 pixels tall, including its padding and borders. The automatically sized box adjusts to the amount of content.
Change to a Minimum Height
.height-demo .fixed-height {
height: auto;
min-height: 100px;
}
This variation preserves a minimum height of 100 pixels while allowing the box to grow when necessary.
Change to a Maximum Height
.height-demo .auto-height {
max-height: 120px;
overflow-y: auto;
}
The box can grow up to 120 pixels. Additional content becomes scrollable if it exceeds the available height.
Common Mistakes
Using a Percentage Without a Definite Containing Block Height
.parent {
height: auto;
}
.child {
height: 100%;
}
In normal block layout, the percentage height generally cannot resolve against an indefinite parent height. Use a definite containing block height or another appropriate sizing approach.
Forgetting About Padding and Borders
With the default content-box model, padding and borders increase the rendered height beyond the declared value.
Applying Height to a Non-Replaced Inline Element
span {
height: 100px;
}
A normal inline <span> does not use the height property to establish its line box height. Change its display type when a specified box height is required.
Using Negative Heights
.box {
height: -100px;
}
Negative height values are invalid.
Using Fixed Height for Variable Content
A fixed height can cause text or controls to overflow. Use min-height for content areas that need room to expand.
Confusing height with min-height
The height property sets a preferred height. The min-height property establishes a lower limit without preventing additional growth.
Assuming 100vh Always Matches the Visible Mobile Screen
Mobile browser controls can affect the visible viewport. Consider svh or dvh when their behavior better matches the layout requirement.
Expecting height to Override All Layout Constraints
Minimum and maximum sizes, flex sizing, grid sizing, and aspect ratios can influence the final used dimensions.
Important Considerations
Keep the following points in mind when using height:
- The initial value is
auto. - The property is not inherited.
- The declared height is a preferred size and may be constrained by other sizing rules.
- Negative height values are invalid.
- Percentage heights depend on the containing block's height and its definiteness.
- Viewport units provide sizing relative to viewport dimensions.
- The
box-sizingproperty determines whether padding and borders are included in the declared height. - The
min-heightandmax-heightproperties constrain the used height. - The
autovalue often allows normal block elements to grow with their content. - Fixed heights can cause content overflow.
- Flexbox and Grid apply additional sizing and alignment rules.
- The
aspect-ratioproperty can influence automatically calculated dimensions. - Intrinsic sizing keywords use content-based size calculations.
- Modern sizing functions provide flexible alternatives to fixed dimensions.
- The
block-sizeproperty provides a writing-mode-aware alternative to physical height. - For content-heavy regions,
min-heightis often preferable to a fixedheight.
Browser Support
The CSS height property is widely available across modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
Traditional values such as auto, lengths, and percentages have broad support. Newer features such as stretch and anchor-size() have separate compatibility requirements.
For current compatibility information, visit Can I Use? and search for height.
Check individual feature compatibility when using newer viewport units, intrinsic sizing keywords, or anchor-based sizing.
Specifications
The height property is defined in CSS sizing specifications, with its traditional behavior also described in CSS 2.1.
CSS Box Sizing Module Level 3: Preferred Size Properties
For additional documentation and examples, consult MDN Web Docs.
