Html Chrome 和 Firefox 中的高度渲染方式不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35532987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Heights rendering differently in Chrome and Firefox
提问by James Yang
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%
:
我发现如果我们设置一个块级元素,height:auto
无论height: 0~100%
是否使用明确的值设置父级的高度,并且它的块级子级具有底部边距,那么它在 Chrome 中计算高度的方式会有所不同,但在 Firefox 中则不然。对于设置的情况height: 1%
:
http://codepen.io/anon/pen/BjgKMR
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
?
div
块的高度将计算为margin-bottom + content height of p element
. 我很困惑为什么height: 1%
应该计算 ,auto
因为父元素(html
和body
标签)没有明确设置其高度,但具有不同的高度,因为我们只是直接将高度设置为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.
如果我们直接将其设置为height: auto
,它显然只是将高度设置为其子块级元素的高度,不包括其底部边距。
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.
我已阅读 CSS 2.1 规范并考虑我的问题可能涉及高度属性和边距折叠主题,但仍然无法理解为什么它在 Chrome 版本中表现不同。47.0.2526,虽然是 Firefox 版本。44.0.2 将显示相同值的高度。
Listed references:
https://www.w3.org/TR/CSS2/visudet.html#the-height-property
列出的参考文献: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 tovisible
.... 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 applicableof the following:
- the bottom edge of the last line box, if the box establishes a inline formatting context with one or more lines
- 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
- the bottom border edge of the last in-flow child whose top margin doesn't collapse with the element's bottom margin
- zero, otherwise
10.5:百分比
... 如果没有明确指定包含块的高度(即,它取决于内容高度),并且该元素不是绝对定位的,则该值计算为 'auto'。...
10.6.3:当
overflow
计算为时,正常流中的块级非替换元素visible
。...如果 'height' 是 'auto',则高度取决于元素是否有任何块级子元素以及它是否有内边距或边框:
元素的高度是从其顶部内容边缘到以下第一个适用的距离:
- 最后一个行框的底部边缘,如果该框用一行或多行建立了内联格式上下文
- 其最后一个流入子元素的底部(可能折叠)边距的底部边缘,如果子元素的底部边距不与元素的底部边距折叠
- 上边距不与元素的下边距折叠的最后一个流入子项的下边框边
- 零,否则
https://www.w3.org/TR/2011/REC-CSS2-20110607/box.html#collapsing-margins
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.
8.3.1 折叠边距。
如果元素没有顶部边框、顶部填充并且子元素没有间隙,则流入块元素的顶部边距与其第一个流入块级子元素的顶部边距折叠。
'高度'为'auto'且'min-height'为零的流入块框的底部边距与其最后一个流入块级子的底部边距折叠,如果框没有底部填充并且没有底部边框和子元素的底部边距不会与具有间隙的顶部边距一起折叠。
... 如果框的顶部和底部边距相邻,则边距可能会通过它折叠。在这种情况下,元素的位置取决于它与边缘被折叠的其他元素的关系。
- 如果元素的边距与其父元素的顶部边距折叠在一起,则框的顶部边框边缘被定义为与父元素的相同。
- 否则,要么元素的父元素不参与边距折叠,要么只涉及父元素的底部边距。元素的顶部边框边缘的位置与元素具有非零底部边框时的位置相同。
回答by Michael Benjamin
So first you have the W3C standards, which are a set of guidelines for browser makers.
所以首先你有 W3C 标准,这是一组浏览器制造商的指南。
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).
然后你有浏览器制造商,他们可以自由地做任何他们想做的事情(正如 Internet Explorer 的偏差历史所证明的那样)。
In particular, with CSS percentage heights, there are clear differences in behavior among browsers.
特别是,对于 CSS 百分比高度,浏览器之间的行为存在明显差异。
You've posted one example. Here's another:
您已经发布了一个示例。这是另一个:
Percentage Heights in Flexbox: Chrome/Safari vs Firefox/IE
Flexbox 中的百分比高度:Chrome/Safari 与 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.
使用 flexbox 时,Chrome 和 Safari 会根据父级height
属性的值解析 flex 项目上的百分比高度。Firefox 和 IE11/Edge 优先考虑父级的 flex 高度。
It appears Webkit browsers adhere to a more traditional interpretation of the spec:
看来 Webkit 浏览器坚持更传统的规范解释:
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.
百分比
指定百分比高度。该百分比是根据生成的框的包含块的高度计算的。如果未明确指定包含块的高度并且此元素不是绝对定位,则该值计算为“自动”。auto
高度取决于其他属性的值。
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.
这是规范的传统解释:术语“高度”意味着height
属性的价值。我自己的观点是,这种语言含糊不清,易于解释,但height
属性要求已成为主要实现。在处理百分比值时,我从未见过min-height
或max-height
处理过父级。
Recently, however, Firefox and IE have broadened their interpretation to accept flex heights, as well.
然而,最近,Firefox 和 IE 也扩大了他们的解释以接受 flex 高度。
Examples of Firefox and IE using a parent's flex height as reference for a child's percentage height:
Firefox 和 IE 使用父母的 flex 高度作为孩子百分比高度的参考的示例:
- Chrome ignoring flex-basis in column layout
- Chrome / Safari not filling 100% height of flex parent
- Height is not correct in flexbox items in Chrome
- Flexbox in Chrome--How to limit size of nested elements?
- Chrome 忽略列布局中的 flex-basis
- Chrome / Safari 未填充 100% 的 flex 父级高度
- Chrome 中 flexbox 项目的高度不正确
- Chrome 中的 Flexbox--如何限制嵌套元素的大小?
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.
随着对这部分定义的最后一次更新是在 1998 年 ( CSS2),以及诸如 flex height 等新高度形式的出现,更新似乎早就应该进行了。
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.
当希望子元素占据父元素的全高时,可以考虑以下两种选择。
Apply
display: flex
to the parent. This automatically setsalign-items: stretch
, which tells the child to expand the full available height of the parent.Apply
position: relative
on the parent andposition: absolute; height: 100%; width: 100%
on the child. With absolute positioning, a percentage height will work without a specified height on the parent.
display: flex
向家长申请。这会自动设置align-items: stretch
,它告诉孩子扩展父母的完整可用高度。适用
position: relative
于父母和position: absolute; height: 100%; width: 100%
孩子。使用绝对定位,百分比高度将在父级上没有指定高度的情况下工作。