CSS height Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout & Sizing

What You’ll Learn

The height property controls how tall an element is on the page. You will learn fixed sizes, percentage sizing, viewport units, and when to let the browser calculate height automatically.

01

Vertical Size

Control box height.

02

auto

Default behavior.

03

Pixels

Fixed heights.

04

Percentages

Relative sizing.

05

Viewport

vh units.

06

Responsive

Flexible layouts.

Introduction

The height property in CSS is used to set the height of an element. It lets you define how tall a box should be in absolute terms (such as pixels) or relative terms (such as percentages or viewport units).

Proper use of height is crucial for creating responsive and well-structured web layouts. You will use it on cards, heroes, sidebars, images, and any element where vertical space matters.

Definition and Usage

Apply height to block-level elements, inline-block elements, replaced elements, and flex or grid items. The syntax is simple: choose a length, percentage, keyword, or viewport unit.

💡
Beginner Tip

Start with height: auto unless you have a clear reason to fix the size. Fixed heights can cause overflow when content grows on smaller screens.

📝 Syntax

The syntax for the height property is simple and versatile:

syntax.css
element {
  height: value;
}

Here, value can be any valid length, percentage, keyword, or viewport unit.

Basic Example

height-basic.css
.box {
  width: 100px;
  height: 200px;
  background-color: lightblue;
}
height: auto; height: 200px; height: 50%; height: 100vh;

Default Value

The default value of the height property is auto, which means the browser calculates height based on content, padding, borders, and other applied styles.

Syntax Rules

  • Works on block-level, inline-block, and replaced elements.
  • Accepts length units (px, rem, em), percentages, and viewport units (vh, svh).
  • The property is not inherited.
  • Percentage heights require a parent with an explicit height.
  • Pair fixed heights with overflow when content may exceed the box.

⚡ Quick Reference

QuestionAnswer
Initial valueauto
Applies toAll elements except non-replaced inline elements, table columns, and column groups
InheritedNo
AnimatableYes, as a length
Common useCards, heroes, sidebars, images, and layout containers

💎 Property Values

ValueDescription
autoThe browser calculates the height from content and other styles.
lengthDefines a fixed height in units such as px, em, rem, etc.
percentageSets the height as a percentage of the containing block’s height.
max-contentThe intrinsic height of the content.
min-contentThe minimum intrinsic height of the content.
fit-contentClamps the height between min-content and max-content.

Understanding Height Units

Choosing the right unit makes layouts easier to maintain. Here is when each type works best:

  • px — Best for small UI pieces with a predictable size, such as icons or thumbnail boxes.
  • rem / em — Scales with font size, useful for components that should grow with text settings.
  • % — Fills part of a parent container. The parent must have a defined height.
  • vh — Relative to the viewport height. Common for full-screen hero sections.
  • auto — Lets content determine height. Usually the safest default for text blocks.
Percentage Height Rule

If height: 50% seems to do nothing, check the parent. Percentage height only works when the containing block has an explicit height set.

👀 Live Preview

Three boxes with different fixed heights in pixels:

80px
120px
160px

Examples Gallery

Set fixed pixel heights, percentage heights, full viewport sections, and compare auto against a fixed size.

🔢 Fixed & Relative Heights

Start with the reference example — a div with a fixed height of 200 pixels.

Example 1 — Fixed Height in Pixels

Set a box to exactly 200 pixels tall, regardless of content.

height-fixed.html
<style>
  .box {
    width: 100px;
    height: 200px;
    background-color: lightblue;
  }
</style>

<div class="box"></div>
Try It Yourself

How It Works

height: 200px gives the element a fixed vertical size. The box stays 200 pixels tall even when empty.

Example 2 — Percentage Height

Make a child element half the height of its parent container.

height-percent.css
.parent {
  height: 300px;
}

.child {
  height: 50%;
  background-color: #93c5fd;
}
Try It Yourself

How It Works

The parent has height: 300px, so the child’s 50% resolves to 150 pixels.

📈 Responsive Layouts

Use viewport units and compare automatic sizing against fixed heights.

Example 3 — Full Viewport Height

Create a hero section that fills the entire browser window height.

height-vh.css
.hero {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
Try It Yourself

How It Works

100vh means 100% of the viewport height. One viewport height unit equals 1% of the visible browser window.

Example 4 — auto vs Fixed Height

Compare the default auto behavior with a fixed height on the same content.

height-compare.css
.auto { height: auto; }
.fixed {
  height: 120px;
  overflow: auto;
}
Try It Yourself

How It Works

With auto, the box grows with content. With a fixed height, overflow may need overflow: auto or hidden.

♿ Accessibility

  • Avoid clipping important content — Fixed heights can hide text when users zoom or increase font size.
  • Allow content to grow — Prefer min-height over rigid height for text-heavy areas.
  • Test keyboard and touch targets — Ensure buttons and links remain reachable inside fixed-height containers.
  • Do not rely on height alone for meaning — Visual size should not be the only indicator of importance.
  • Check mobile viewports — Full 100vh sections can feel too tall on small screens with browser chrome.

🧠 How height Works

1

You set a height value

Choose auto, a length like 200px, a percentage, or a viewport unit.

CSS rule
2

The browser resolves the size

Percentages look at the parent height. Viewport units look at the window. auto uses content size.

Calculation
3

The box is rendered

Padding and borders add to the total space the element occupies in layout, depending on box-sizing.

Layout
=

Controlled vertical space

Your layout gets predictable or flexible height depending on the value you chose.

🖥 Browser Compatibility

The height property is universally supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. Older versions of Internet Explorer also support basic height values.

Baseline · Universal support

Fundamental layout property

height has been part of CSS since the earliest layout models and works consistently across browsers.

99% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Opera All modern versions
Full support
height property 99% supported

Bottom line: You can rely on height in production layouts across all major browsers.

🎉 Conclusion

The height property is a fundamental CSS tool for controlling the vertical size of elements. By understanding units such as pixels, percentages, and viewport heights, you can create flexible, responsive, and visually appealing web designs.

For beginners, the main idea is simple: use auto by default, add fixed heights when you need predictable boxes, and remember that percentage heights depend on the parent container.

💡 Best Practices

✅ Do

  • Use height: auto for text content by default
  • Set an explicit parent height before using percentages
  • Use min-height when content may grow
  • Combine 100vh with flexbox for hero sections
  • Test layouts on mobile and with zoom enabled

❌ Don’t

  • Fix height on paragraphs with unpredictable text length
  • Assume height: 50% works without a parent height
  • Forget overflow when content exceeds a fixed box
  • Use only pixels on responsive layouts without breakpoints
  • Confuse height with total space including margins

Key Takeaways

Knowledge Unlocked

Five things to remember about height

Use these points when sizing elements in your layouts.

5
Core concepts
auto 02

Default auto

Content-based.

Default
px 03

Fixed Units

Predictable size.

Syntax
% 04

Relative

Parent-based.

Pattern
vh 05

Viewport

Full-screen.

Responsive

❓ Frequently Asked Questions

The height property sets the vertical size of an element. It controls how tall a box appears on the page.
The default is auto, which means the browser calculates height from content, padding, borders, and other styles.
Percentage height needs a parent with an explicit height. If the parent height is auto, the percentage has nothing reliable to calculate from.
height sets a fixed or preferred size. min-height sets a minimum size but allows the element to grow taller if content needs more space.
No. height is not inherited, but percentage values are calculated relative to the containing block height.

Practice in the Live Editor

Open the HTML editor, change height values, and compare fixed, percentage, and viewport sizing instantly.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful