Tillitsdone
down Scroll to discover

CSS Max-Height Control Element Height for Better Design

The CSS max-height property sets the maximum height of an element.

It helps manage layouts and prevent overflow, with options like length, percentage, and keywords.
thumbnail

Formal Definition

The max-height property in CSS sets the maximum height of an element, preventing it from exceeding a specified value. This property is essential for managing layouts and preventing content from overflowing uncontrollably.

Values

  • Length Values: Set the max-height using absolute lengths like pixels (px) or ems (em).

    max-height: 3.5em;
    max-height: 200px;
  • Percentage Values: Set the max-height as a percentage of the containing block’s height.

    max-height: 75%;
  • Keyword Values: Use keywords like none, max-content, min-content, fit-content, or stretch.

    max-height: none;
    max-height: max-content;
    max-height: fit-content;
    max-height: stretch;
  • Global Values: Use inherit, initial, revert, revert-layer, or unset.

    max-height: inherit;
    max-height: initial;
    max-height: revert;
  • Calculated Values: Use the calc() function for dynamic and flexible layouts.

    max-height: calc(100% - 50px);

Examples

Here are some examples of how to use the max-height property:

/* Setting max-height using a length value */
.element {
max-height: 300px;
}
/* Setting max-height using a percentage value */
.container {
max-height: 50%;
}
/* Setting max-height using a keyword value */
.content {
max-height: none;
}
/* Setting max-height using a calculated value */
.dynamic-element {
max-height: calc(100% - 20px);
}

Accessibility

When using the max-height property, consider accessibility to ensure your design is usable for all users:

  • Ensure Content Visibility: Use overflow: auto; to add scrollbars if content exceeds the max-height.
  • Zoom Support: Ensure that elements with a max-height do not obscure content when the page is zoomed.
  • Keyboard Navigation: Make sure elements with a max-height and scrollbars are keyboard-navigable.
  • Screen Reader Compatibility: Use ARIA attributes to enhance accessibility.

Example

.accessible-element {
max-height: 300px;
overflow: auto;
border: 1px solid #ccc;
}
.accessible-element:focus {
outline: 2px solid blue;
}
<div class="accessible-element" tabindex="0" aria-label="Scrollable content">
<p>This is a paragraph with a lot of text that may exceed the maximum height. The overflow property ensures that users can scroll to see all the content.</p>
<!-- More content here -->
</div>

Additional Resources

  • [MDN Understanding WCAG, Guideline 1.4 explanations]WebsiteUrl: Detailed explanations of WCAG guidelines related to visual content.
  • [Understanding Success Criterion 1.4.4 | W3C Understanding WCAG 2.0]WebsiteUrl: Insights into ensuring content is resizable and scalable.

Initial Value

  • Value: none
  • Description: By default, the max-height property is set to none, meaning there is no limit on the element’s height.

Applies To

  • Elements: All elements except non-replaced inline elements, table columns, and column groups.
  • Description: The max-height property can be applied to most block-level and replaced elements but not to non-replaced inline elements or table structures.

Inherited

  • Value: No
  • Description: The max-height property is not inherited from the parent element. Each element must have its own max-height value specified directly.

Percentages

  • Calculation: The percentage is calculated with respect to the height of the containing block.
  • Exception: If the height of the containing block is not specified explicitly (i.e., it depends on content height), and the element is not absolutely positioned, the percentage value is treated as none.

Computed Value

  • Value: The computed value is the specified percentage or the absolute length value, or none if no value is specified.
  • Description: The browser calculates the actual maximum height based on the specified value and the context of the element.

Animation Type

  • Type: The max-height property can be animated using lengths, percentages, or the calc() function.
  • Description: This allows for smooth transitions and animations, enhancing the dynamic capabilities of the property in responsive designs.

Formal Syntax

The formal syntax for the max-height property is defined as follows:

max-height =
none |
<length-percentage> |
min-content |
max-content |
fit-content( <length-percentage> ) |
<calc-size()> |
<anchor-size()>

Components

  • none: No limit on the size of the box.
  • <length-percentage>: Defines the max-height as an absolute value or a percentage of the containing block’s height.
  • min-content: The intrinsic minimum max-height.
  • max-content: The intrinsic preferred max-height.
  • fit-content: Uses the available space, but not more than max-content, i.e., min(max-content, max(min-content, stretch)).
  • fit-content(<length-percentage>): Uses the fit-content formula with the available space replaced by the specified argument.
  • stretch: Limits the maximum height of the element’s margin box to the height of its containing block.

Example Syntax

/* Length value */
max-height: 3.5em;
/* Percentage value */
max-height: 75%;
/* Keyword values */
max-height: none;
max-height: max-content;
max-height: fit-content;
/* Calculated value */
max-height: calc(100% - 50px);

Examples

Setting max-height Using Percentage and Keyword Values

table {
max-height: 75%;
border: 1px solid black;
overflow: auto;
}
form {
max-height: none;
border: 1px solid black;
}

Using max-height with Length Values

p {
max-height: 35px;
border: 1px solid black;
overflow: auto;
}

Using max-height with Calculated Values

div {
max-height: calc(100% - 20px);
border: 1px solid blue;
overflow: auto;
}

Using max-height with Keyword Values

div {
max-height: max-content;
border: 1px solid green;
overflow: auto;
}

Using max-height with Initial Value

div {
max-height: initial;
border: 1px solid red;
overflow: auto;
}

These examples demonstrate how to use the max-height property with different values to control the maximum height of elements in your web designs. This property is essential for creating flexible and responsive layouts that adapt to different content and screen sizes.

Browser Compatibility

The max-height property is well-supported across modern web browsers. Here is an overview of the browser compatibility:

  • Google Chrome: Supported since version 1.0
  • Edge: Supported since version 12.0
  • Internet Explorer: Supported since version 7.0
  • Firefox: Supported since version 1.0
  • Opera: Supported since version 7.0
  • Safari: Supported since version 1.3

Browser Compatibility Data

For the most accurate and up-to-date information on browser compatibility, you can refer to the Browser Compatibility Data (BCD) tables provided by Mozilla Developer Network (MDN). These tables are dynamically loaded and require JavaScript to be enabled in the browser for viewing.

Notes

  • Browser-Specific Implementations: Some browsers may have specific aliases or implementation details for the stretch value. Always check the latest browser compatibility information for the most accurate details.

By ensuring compatibility across different browsers, you can create web designs that are consistent and functional for all users, regardless of their browser choice.

See Also

For further learning and related topics, you may also be interested in the following resources:

  • The Box Model: Learn more about the CSS box model and how it affects element sizing.
  • box-sizing: Understand how the box-sizing property can be used to control the size of elements.
  • height: Explore the height property and how it interacts with max-height.
  • min-height: Learn about the min-height property and its relationship with max-height.
  • Mapped Logical Properties: Discover how to use logical properties like max-inline-size and max-block-size for more flexible layouts.

By exploring these related topics, you can gain a deeper understanding of CSS sizing properties and how to use them effectively in your web development projects.

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.