Tillitsdone
down Scroll to discover

CSS Height Understanding and Using the Height Property

Learn about CSS height property, its use case, and the various options available for setting the height of elements.

Understand how to use pixels, percentages, and keywords like auto, max-content, and min-content.
thumbnail

CSS height Property

The height property in CSS sets the height of an element, controlling the vertical space it takes up. You can set it using various units like pixels (px), percentages (%), and viewport units (vh).

Syntax

/* Length values */
height: 120px;
height: 10em;
height: 100vh;
/* Percentage value */
height: 75%;
/* Keyword values */
height: max-content;
height: min-content;
height: fit-content;
height: fit-content(20em);
height: auto;
height: stretch;
/* Global values */
height: inherit;
height: initial;
height: revert;
height: unset;

Examples

Setting Height Using Pixels and Percentages

HTML
<div id="taller">I'm 50 pixels tall.</div>
<div id="shorter">I'm 25 pixels tall.</div>
<div id="parent">
<div id="child">I'm half the height of my parent.</div>
</div>
CSS
div {
width: 250px;
margin-bottom: 5px;
border: 2px solid blue;
}
#taller {
height: 50px;
}
#shorter {
height: 25px;
}
#parent {
height: 100px;
}
#child {
height: 50%;
width: 75%;
}
Result

The first div will be 50 pixels tall, the second div will be 25 pixels tall, and the child div inside the parent div will be half the height of its parent.

Using Keyword Values

HTML
<div id="auto-height">I'm auto height.</div>
<div id="max-content-height">I'm max-content height.</div>
<div id="min-content-height">I'm min-content height.</div>
<div id="fit-content-height">I'm fit-content height.</div>
CSS
div {
width: 250px;
margin-bottom: 5px;
border: 2px solid blue;
}
#auto-height {
height: auto;
}
#max-content-height {
height: max-content;
}
#min-content-height {
height: min-content;
}
#fit-content-height {
height: fit-content;
}
Result

Each div will have its height set according to the specified keyword value: auto, max-content, min-content, and fit-content.

Values and Keywords

Length (<length>)

  • Pixels (px): Fixed pixel values, e.g., height: 100px;.
  • Ems (em): Relative to the font size of the element, e.g., height: 2em;.
  • Viewport Height (vh): Relative to the viewport height, e.g., height: 100vh;.

Percentage (<percentage>)

  • Percentage Value: Defines the height as a percentage of the containing block’s height, e.g., height: 75%;.

Keyword Values

  • auto: The browser calculates and selects a height for the specified element.
  • max-content: The intrinsic preferred height.
  • min-content: The intrinsic minimum height.
  • fit-content: Uses the available space, but not more than max-content.
  • fit-content(<length-percentage>): Uses the fit-content formula with the available space replaced by the specified argument.
  • stretch: Sets the height of the element’s margin box to the height of its containing block.

Global Values

  • inherit: Inherits the height from the parent element.
  • initial: Sets the height to its default value.
  • revert: Reverts the height to the browser’s default.
  • unset: Resets the height to its natural value.

Box Sizing and Border Area

The height property can interact with the box-sizing property to determine how the height is calculated.

Content Area vs. Border Area

  • Content Area: When box-sizing is set to its default value (content-box), the height property defines the height of the element’s content area.
  • Border Area: When box-sizing is set to border-box, the height property defines the height of the element’s border area.

Example

HTML
<div class="content-box">Content Box</div>
<div class="border-box">Border Box</div>
CSS
.content-box {
width: 200px;
height: 100px;
padding: 20px;
border: 10px solid blue;
box-sizing: content-box; /* Default behavior */
background-color: lightblue;
margin-bottom: 20px;
}
.border-box {
width: 200px;
height: 100px;
padding: 20px;
border: 10px solid green;
box-sizing: border-box;
background-color: lightgreen;
}
Result
  • The .content-box div will have a total height of 160px because the padding and border are added outside the specified height.
  • The .border-box div will have a total height of 100px, including the padding and border.

Min-height and Max-height Override

The height property can be overridden by the min-height and max-height properties.

Example

HTML
<div class="min-height-example">Min-height Example</div>
<div class="max-height-example">Max-height Example</div>
<div class="combined-example">Combined Example</div>
CSS
.min-height-example {
width: 200px;
min-height: 100px;
background-color: lightblue;
margin-bottom: 20px;
}
.max-height-example {
width: 200px;
max-height: 150px;
background-color: lightgreen;
margin-bottom: 20px;
overflow: auto; /* Adds a scrollbar if content exceeds max-height */
}
.combined-example {
width: 200px;
min-height: 100px;
max-height: 150px;
background-color: lightcoral;
margin-bottom: 20px;
overflow: auto; /* Adds a scrollbar if content exceeds max-height */
}
Result
  • The .min-height-example div will have a minimum height of 100px.
  • The .max-height-example div will have a maximum height of 150px, with a scrollbar if the content exceeds this height.
  • The .combined-example div will have both a minimum height of 100px and a maximum height of 150px, with a scrollbar if the content exceeds the maximum height.

Understanding min-height, max-height, and height in CSS

Key Points

  • .min-height-example: This div will have a minimum height of 100 pixels, even if its content is smaller.
  • .max-height-example: This div will have a maximum height of 150 pixels. If the content exceeds this height, a scrollbar will appear.
  • .combined-example: This div combines both min-height and max-height, ensuring its height stays between 100 and 150 pixels. A scrollbar will appear if the content exceeds 150 pixels.

Practical Applications

  • Responsive Design: Use min-height and max-height to create adaptable designs that work with different content sizes and screen dimensions.
  • Consistent Layouts: These properties help keep element heights consistent, which is essential for maintaining a clean layout.
  • User Experience: Setting a maximum height and using overflow: auto prevents content overflow and ensures a better user experience with scrollbars when needed.

Best Practices

  • Use Together: Combining min-height and max-height lets you define a flexible height range for elements.
  • Consider Content: When using max-height, always consider potential content overflow and use overflow: auto to handle it.
  • Test Across Devices: Make sure your designs work well on different devices and screen sizes by testing the behavior of min-height and max-height.

SVG Elements Support

The height property in CSS also applies to SVG (Scalable Vector Graphics) elements, giving you control over the dimensions of graphical content.

Supported SVG Elements

The height property can be used with these SVG elements:

  • <svg>: The root element for SVG graphics.
  • <rect>: Defines a rectangle.
  • <image>: Embeds an image.
  • <foreignObject>: Allows for non-SVG content within an SVG.

Behavior with SVG Elements

  • Auto Value: Setting height to auto resolves to 0 for SVG elements, so it won’t have a specified height unless defined.
  • Percentage Values: For <rect> elements, percentage values are relative to the SVG viewport height, making designs responsive.
  • CSS Override: The CSS height property overrides any SVG height attribute, ensuring CSS styles take precedence.

Example

<svg id="svg-example" width="200px" height="200px">
<rect id="rect-example" width="100%" height="50%" fill="blue" />
<image id="image-example" x="50" y="50" width="100" height="100" href="image.png" />
<foreignObject id="foreign-object-example" width="150" height="100">
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Foreign Object Content</p>
</div>
</foreignObject>
</svg>
#svg-example {
border: 1px solid black;
}
#rect-example {
height: 50%; /* Height relative to the SVG viewport */
}
#image-example {
height: 75px; /* Fixed height for the image */
}
#foreign-object-example {
height: 100px; /* Fixed height for the foreign object */
}

Practical Applications

  • Responsive SVG Graphics: Use percentage values for responsive SVG graphics that scale with the viewport.
  • Consistent Styling: Overriding SVG attributes with CSS ensures consistent styling across HTML and SVG content.
  • Interactive Elements: Combine SVG elements with CSS for interactive and dynamic graphics.

Best Practices

  • Use Percentages for Responsive Design: Use percentage values for the height property to make SVG graphics responsive.
  • Override SVG Attributes with CSS: Override SVG attributes with CSS to maintain consistency.
  • Test Across Browsers: Ensure SVG elements and CSS styles work correctly across different browsers.

Accessibility Considerations

When working with the height property, ensure your web content is accessible to all users.

Ensure Content Visibility

  • Avoid Truncating Content: Make sure fixed heights don’t truncate content. Use overflow: auto for scrollbars when content exceeds the height.
  • Responsive Design: Use flexible units like percentages (%) and viewport units (vh) for responsive designs.

Use of min-height and max-height

  • Prevent Content Overflow: Use min-height and max-height to define a flexible height range and prevent content overflow.
  • Consider User Interactions: Ensure interactive elements within fixed-height containers are accessible.

Zoom and Text Size

  • Support Zooming: Make sure your design supports zooming and increased text sizes.
  • Avoid Fixed Heights for Text Containers: Be cautious with fixed heights for text containers to avoid text overflow.

Use of ARIA Attributes

  • ARIA Roles and Properties: For dynamic height elements, use ARIA roles and properties to provide context for screen readers.

Testing and Validation

  • Accessibility Testing: Regularly test your web pages using accessibility tools and screen readers.
  • User Feedback: Gather feedback from users with disabilities to understand their experiences.

Example

<div class="accessible-container">
<h2>Accessible Content</h2>
<p>This content is designed to be accessible to all users, including those with disabilities. The container has a flexible height to accommodate different text sizes and zoom levels.</p>
<button>Click Me</button>
</div>
.accessible-container {
width: 100%;
min-height: 200px;
max-height: 400px;
overflow: auto;
padding: 20px;
border: 1px solid #ccc;
background-color: #f9f9f9;
}
.accessible-container h2 {
font-size: 1.5em;
margin-bottom: 10px;
}
.accessible-container p {
font-size: 1em;
line-height: 1.5;
}
.accessible-container button {
margin-top: 20px;
padding: 10px 20px;
font-size: 1em;
}

Browser Compatibility

The height property in CSS is widely supported across all major browsers.

Compatibility Table

BrowserVersionSupported Since
Chrome1.0December 2008
Firefox1.0November 2004
Internet Explorer4.0September 1997
Edge12.0July 2015
Opera7.0January 2003
Safari1.0June 2003

Special Considerations

  • Box-Sizing Property: The box-sizing property can influence the behavior of the height property.
  • Min-Height and Max-Height: These properties are also widely supported but always test them in your specific use case.
  • SVG Elements: The height property’s behavior with SVG elements might differ slightly across browsers.

Best Practices

  • Cross-Browser Testing: Regularly test your web pages in different browsers.
  • Fallbacks and Polyfills: Use fallbacks or polyfills to ensure consistent behavior across all browsers.
  • Documentation: Keep up-to-date with the latest browser compatibility information by referring to official documentation and resources.

Example

<div class="browser-test">
<h2>Browser Compatibility Test</h2>
<p>This div has a fixed height and should appear consistently across different browsers.</p>
</div>
.browser-test {
width: 300px;
height: 200px;
padding: 20px;
border: 2px solid blue;
background-color: lightblue;
}

See Also

For further exploration of CSS properties and related concepts, consider the following resources:

  • The Box Model: Learn the basics of the CSS box model, including content, padding, border, and margin.
  • width: Understand how the width property sets the width of an element.
  • box-sizing: See how the box-sizing property affects element sizing, with values like border-box and content-box.
  • min-height and max-height: Learn about these properties, which set the minimum and maximum height of an element.
  • Logical Properties: Discover logical properties like block-size and inline-size for more flexible layouts.
  • anchor-size(): Understand the anchor-size() function for specifying sizes relative to anchor elements.
  • clamp(): Learn about the clamp() function, which limits a value between a minimum and maximum.
  • minmax(): Explore the minmax() function, which sets a range of acceptable values.

These resources will help you master CSS properties and boost your web development skills.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.