Does including the "max-height" property within a table result in a height of zero?

Check out the code snippet below (you can also view a demo on JsFiddle):

#outer {
    display: table;
    height: 200px;
    width: 100%;
    background: gray;
}

#inner {
    width: 100%;
    background: blue;
    max-height: 100%;
}
<div id="outer">
    <img id="inner" src="http://jsfiddle.net/img/logo.png">
</div>

The parent element is using display:table (set by a third-party framework), and I have set max-height:100% in the child element for constraint purposes.

However, in the demo above (tested in Chrome 46.0), the height of the child element seems to become "ZERO."

After removing the display:table attributes from the parent element, everything works correctly again.

According to the MDN document on max-height, when specifying a percentage value for max-height:

The percentage is calculated relative to the height of the generated box's containing block. If the height of the containing block is not explicitly defined (i.e., it depends on content height) and this element is not absolutely positioned, the percentage value is treated as none.

In my understanding based on the demo, the #inner element's containing block is explicitly set to #outer, so the max-height:100% attribute in #inner should be equivalent to height: 200px. Is this correct? Any thoughts on where I might have made a mistake?

Thank you!

Answer №1

When dealing with a parent element set to display: table and a child element, the child element is automatically styled as if its display property was set to table-cell. This might seem confusing, but essentially the child element behaves as if it were inside an anonymous table-cell box.

Trying to apply max-height to a table cell leads to unpredictable results, according to CSS 2.1 specifications:

The impact of 'min-height' and 'max-height' on tables, inline tables, table cells, table rows, and row groups is not clearly defined in CSS 2.1.

It's worth noting that this issue seems to only affect Chrome, while Firefox and IE render the image correctly. It's hard to determine which browser behavior is correct in this case.

In my demonstration, I've explicitly set the containing block for the #inner element to be #outer, so setting max-height: 100% on #inner should be equivalent to setting height: 200px. Am I correct in understanding this? Any insights on where I may have gone wrong?

Contrary to your assumption, the containing block for a table cell is not the table itself, but rather the containing block of the table. Referencing section 10.1 of the CSS specification clarifies this:

  1. For other elements, if the element's position is 'relative' or 'static', the containing block is formed by the content edge of the nearest block container ancestor box.

According to references like MDN and the spec itself regarding max-height, the specified max-height value should be interpreted as none. However, Chrome seems to enforce a height restriction of zero instead. Whether this behavior is intentional to highlight undefined behavior or simply a browser quirk remains uncertain. Exploring alternative solutions within your framework would be advisable to work around the limitations imposed by display: table.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Center align `div 1` with CSS and right align `div 2`

Take a look at this code snippet: http://jsfiddle.net/zygnz/1/ <div class="container"> <div class="left"> LEFT </div> <div class="right"> RIGHT </div> </div> Is i ...

Tips for transforming a date into a time ago representation

Can someone help me with converting a date field into a "timeago" format using jquery.timeago.js? $("time.timeago").timeago(); var userSpan = document.createElement("span"); userSpan.setAttribute("class", "text-muted"); userSpan.appendChild(document.crea ...

I am in search of a clean and efficient method to modify the class of a link that triggers an HTMX request in Django. Perhaps something like "auto-refresh" or a similar solution would be ideal

I've encountered an issue with HTMX in Django. The page consists of two main components: a list of categories and the content that is displayed when a category is clicked. Initially, everything was working smoothly with standard htmx functionality. H ...

What unique capabilities does canvas offer that are not available with SVG?

Currently tackling a project that requires choosing between SVG and Canvas. After extensive research, it seems like SVG outperforms Canvas in many aspects. Is there anything that Canvas can do but SVG cannot? I'd love to hear your insights. ...

Using JavaScript, aim for a specific element by its anchor headline

I'm looking to make some changes to my navigation menu, specifically hiding the home menu item unless the mobile navigation menu is toggled. Is there a way for me to target the "home" anchor title and apply the active class to it when toggled, similar ...

Styling the content area to fill the entire window in CSS will

<div id="content"> </div> #content{ height:100%; z-index:2; position:relative; top:40px } The content within this DIV is positioned 40px from the top of the page and will scroll under a navigation bar that is 40px high. I am trying to figur ...

The function 'find' cannot be invoked on an undefined object

I'm currently working on implementing objects in my jQuery code. So far, I have the following: var options = { ul: $(this).find('.carousel'), li: options.ul.find('li') } The li property is causing an error - Cannot call meth ...

"Covering the basics of HTML with this set of

Seeking a solution for incorporating common HTML elements into web pages? Look no further. In this scenario, the "Footer Section" and "Bulletin" are recurring components across all site pages. While it may seem minor, the goal is to consolidate these state ...

Press one button to activate another button

I am looking to activate a button's class by clicking on another button. The button I have is: <button class="clear-cart banner-btn">clear cart</button> This button is used for clearing a cart. On the order confirmation page, I click on ...

Concealing anchor and span elements using CSS in Internet Explorer 7

I decided to simplify things by creating a style within the document to address my specific issue. The problem I encountered involves a row of 4 links that are designed to resemble buttons. I have chosen to hide the Next link (the 3rd item) using CSS. Whil ...

Updating the email header for responding to New Order and shipped email notifications in Woocommerce

Looking for a solution to customize the reply-to email addresses specifically for New Order and Shipped emails sent to customers. I attempted to implement a suggested fix from this resource Change "reply to" email address in all Woocommerce email ...

Stacking issue - elements not properly aligned

Is there a way to move my H1 text below the blue category button instead of beside it? Thank you in advance! http://jsfiddle.net/TDBzN/ <div id="article" class="animated2 fadeInLeftBig"> <div class="article-close"></div> <div c ...

Is there a way to search for text and highlight it within an HTML string using Ionic

Welcome everyone! I am currently utilizing Ionic to load an article from a local HTML string. <ion-content padding> <p [innerHTML]="url"></p> </ion-content> The 'url' variable contains the local HTML string. My goal ...

I am on a quest to locate the xpath for a day that is divided by the HTML line break element,

In my HTML structure, I have the following code snippet: <td class="FormLabel" valign="top"> <span class="DataLabel">Consists of:</span> </td> <td class="FormData"> <span class="BodyText"> Sunday<br> M ...

Angular 4 provides the capability to reset a radio group that has been dynamically generated within a table

I am facing an issue with a dynamically created radio group inside a table in my component. I am using ngFor to generate the radio buttons, and there is a clear button outside the ngFor that should reset the radio group when clicked. However, my current im ...

The issue I'm facing involves Materialize CSS interfering with the rendering of input fields from PrimeNG. Does anyone have a solution to resolve

The code I am working with is as follows: <div class="container" style="width:100%;"> <div class="ui-widget-header" style="padding:4px 10px;border-bottom: 0 none"> <i class="fa fa-search" style="margin:4px 4px 0 0"></i> < ...

Conceal overflow in SVG after stroke is applied to top rectangle border

Is there a way to conceal the overflowing red color in this SVG? I attempted repositioning it before the rect with the stroke, but that didn't solve the issue. Check out the code and screenshot below. <br><br> <svg _ngcontent-fdl-c1 ...

Customize the background color of highlighted text using HTML and jQuery

Recently, I modified an existing code to divide plain text into four classes by selecting a portion of the text and coloring it. Afterwards, I extracted the text of each class and stored it in my database. While this code works well, I am looking for a way ...

Quick question about utilizing Ajax with spans

<span name = "menu"> <!-- javascript here --> <!-- content loaded via ajax --> </span> <span name = "content"> <!-- content loaded via ajax --> <!-- updated by buttons from the menu--> </span> Seeking a ...

Ways to set control values with AngularJS

After retrieving a single record from the database for my detail view, I need to assign data to controls. Take a look at my code snippet below: var app = angular.module("MyProductApp", []); app.controller("ProductsController", function ($scope, ...