By James Yang


2016-02-21 06:21:07 8 Comments

I found if we set a block level element with height:auto or height: 0~100% without set up parent's height with explicitly value, and its block level child has bottom margin, then it will calculate height differently in Chrome, but not in Firefox. For the case which set height: 1%:

http://codepen.io/anon/pen/BjgKMR

html {
  background: pink;
}

body {
  background: yellow;
}

div {
  height: 1%;
}

inner {
  margin-bottom: 30px;
  margin-top: 20px;
}
<div>
  <p class="inner">block level element</p>
</div>

The height of div block will be calculated as the margin-bottom + content height of p element. I am confused about why the height: 1% should be computed as to auto because the parent elements(html and body tag) not set its height explicitly, but has different height as we just directly set the height to auto?

If we directly set it to height: auto, it will clearly just set the height as its child block-level element's height, which is not include its bottom margin.

html {
  background: pink;
}

body {
  background: yellow;
}

div {
  height: auto;
}

inner {
  margin-bottom: 30px;
  margin-top: 20px;
}
<div><p class="inner">block level element</p></div>

I have read the CSS 2.1 spec and think about my question might covered with the height property and margin collapse topic, but still cannot understand why it behaves different in Chrome ver. 47.0.2526, though Firefox ver. 44.0.2 will display the height with same value.

Listed references:
https://www.w3.org/TR/CSS2/visudet.html#the-height-property

  • 10.5: percentage

    ... If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'. ...

  • 10.6.3: Block-level non-replaced elements in normal flow when overflow computes to visible.

    ... if 'height' is 'auto', the height depends on whether the element has any block-level children and whether it has padding or borders:

    The element's height is the distance from its top content edge to the first applicable of the following:

    1. the bottom edge of the last line box, if the box establishes a inline formatting context with one or more lines
    2. the bottom edge of the bottom (possibly collapsed) margin of its last in-flow child, if the child's bottom margin does not collapse with the element's bottom margin
    3. the bottom border edge of the last in-flow child whose top margin doesn't collapse with the element's bottom margin
    4. zero, otherwise

https://www.w3.org/TR/2011/REC-CSS2-20110607/box.html#collapsing-margins

  • 8.3.1 collapsing margins.

    The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.

    The bottom margin of an in-flow block box with a 'height' of 'auto' and a 'min-height' of zero collapses with its last in-flow block-level child's bottom margin if the box has no bottom padding and no bottom border and the child's bottom margin does not collapse with a top margin that has clearance.

    ... If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it. In this case, the position of the element depends on its relationship with the other elements whose margins are being collapsed.

    • If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.
    • Otherwise, either the element's parent is not taking part in the margin collapsing, or only the parent's bottom margin is involved. The position of the element's top border edge is the same as it would have been if the element had a non-zero bottom border.

1 comments

@Michael_B 2016-02-21 14:43:05

So first you have the W3C standards, which are a set of guidelines for browser makers.

And then you have the browser makers, who are free to do whatever they want (as evidenced by a history of deviations by Internet Explorer).

In particular, with CSS percentage heights, there are clear differences in behavior among browsers.

You've posted one example. Here's another:

Percentage Heights in Flexbox: Chrome/Safari vs Firefox/IE

When working with flexbox, Chrome and Safari resolve percentage heights on flex items based on the value of the parent's height property. Firefox and IE11/Edge prioritize the parent's flex height.

It appears Webkit browsers adhere to a more traditional interpretation of the spec:

CSS height property

percentage
Specifies a percentage height. The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly and this element is not absolutely positioned, the value computes to "auto".

auto
The height depends on the values of other properties.

In other words, for percentage height to work on an in-flow child, the parent must have a set height.

That is the traditional interpretation of the spec: The term "height" means the value of the height property. My own view is that this language is vague and open to interpretation, but the height property requirement has become the predominant implementation. I've never seen min-height or max-height work on a parent when dealing with percentage values.

Recently, however, Firefox and IE have broadened their interpretation to accept flex heights, as well.

Examples of Firefox and IE using a parent's flex height as reference for a child's percentage height:

Knowing which browsers are in compliance with the spec is a bit difficult because, as I mentioned before, the spec language seems vague and open to interpretation.

With the last update to this part of the definition being in 1998 (CSS2), and the advent of new forms of height such as flex height, an update seems long overdue.

I think it's fair to say that when it comes to percentage heights, until the spec definition gets an update, you can expect rendering differences among browsers.


Alternative Solutions

Here are two alternatives to consider when wanting a child element to take the parent's full height.

  1. Apply display: flex to the parent. This automatically sets align-items: stretch, which tells the child to expand the full available height of the parent.

  2. Apply position: relative on the parent and position: absolute; height: 100%; width: 100% on the child. With absolute positioning, a percentage height will work without a specified height on the parent.

@James Yang 2016-02-23 11:56:50

Thanks for your reply, other resource also made me feel like either follow the spec, or unexpected behaviors occurred is really depends on the browser providers and should avoid it.

Related Questions

Sponsored Content

45 Answered Questions

[SOLVED] How can I transition height: 0; to height: auto; using CSS?

31 Answered Questions

[SOLVED] Make a div fill the height of the remaining screen space

  • 2008-09-18 05:06:17
  • Vincent McNabb
  • 793558 View
  • 1529 Score
  • 31 Answer
  • Tags:   html css html-table

12 Answered Questions

[SOLVED] What's the difference between SCSS and Sass?

  • 2011-04-13 19:23:09
  • bruno077
  • 628597 View
  • 1522 Score
  • 12 Answer
  • Tags:   css sass

31 Answered Questions

[SOLVED] How to make a div 100% height of the browser window?

  • 2009-10-15 21:18:43
  • mike
  • 1697122 View
  • 1869 Score
  • 31 Answer
  • Tags:   html css css3 height

0 Answered Questions

what is the height of block box's containing block?

  • 2018-11-27 14:49:41
  • xianshenglu
  • 15 View
  • 0 Score
  • 0 Answer
  • Tags:   css

14 Answered Questions

[SOLVED] Margin on child element moves parent element

  • 2009-11-19 11:02:47
  • Robert Koritnik
  • 141508 View
  • 354 Score
  • 14 Answer
  • Tags:   css xhtml margin nested

1 Answered Questions

[SOLVED] Why the margin collapses when clearance exists?

  • 2017-03-03 06:58:43
  • weapon.he
  • 83 View
  • 3 Score
  • 1 Answer
  • Tags:   css margin

2 Answered Questions

[SOLVED] Collapsed margin in CSS

  • 2017-02-20 14:18:21
  • Breaking Benjamin
  • 231 View
  • 1 Score
  • 2 Answer
  • Tags:   css

2 Answered Questions

[SOLVED] Margins collapse when clear:both exists

  • 2015-05-06 02:24:33
  • Joy
  • 106 View
  • 2 Score
  • 2 Answer
  • Tags:   css

Sponsored Content