为什么CSS2.1定义溢出值而不是“可见”建立一个新的块格式化上下文? [英] Why does CSS2.1 define overflow values other than "visible" to establish a new block formatting context?
问题描述
CSS2.1规范。
这是很好的,因为内容永远不会被滚动,但是当 overflow
设置为除 visible
之外的其他内容,这将导致内容不仅被框的边界裁剪,变得可滚动。如果第二个框有 overflow:auto
,这是浏览器实现了原来CSS2规范的样子:
由于浮动,滚动内容将导致浏览器重新包装它,所以它不会被浮动(以及应该发生什么应该发生在滚动出顶部边缘?)。在滚动到底部时,它可能看起来像这样:
这里的秘诀是浏览器每次在滚动时重写内容时都必须重写内容。对于能够基于像素的平滑滚动的浏览器 - 也就是说,所有的 - 我可以看到为什么会是一个性能灾难! (和用户体验一样。)
但是这是当用户可以滚动内容,对吗?这对于 overflow:auto
和 overflow:scroll
是有意义的,但是 overflow:隐藏?
好吧,一个常见的误解是一个容器 overflow:hidden
只是通过剪切隐藏内容,无法滚动。 。现在如果你应用溢出
值而不是可见
只到第二个框,浏览器做的是push 整个框,以便为浮动方式创造条件,因为现在该框创建了一个包含其内容的新的块格式化上下文,而不是围绕浮动。此特定行为在以下,in a element with height:auto
生成一个新的块格式化上下文(即一个块格式化上下文根)将垂直伸展到其浮动的高度,而不仅仅是足够包含其流内容不像常规框。更改记录在此处和此处。导致收缩框的副作用,使其不与浮点相交的变化记录在这里。
在这两种情况下,无论你对第二个框做什么,它都不会受到float的影响,它已被其容器的边界限制。
The CSS2.1 spec mandates that overflow
other than visible
establish a new "block formatting context". This strikes me as odd, that a property whose obvious purpose is to hide overflow without affecting layout, actually does affect layout in a major way.
It seems like overflow values other than visible
combine two completely unrelated features: whether a BFC is created and whether the overflow is hidden. It’s not like "overflow:hidden" is completely meaningless without a BFC, because floats historically can overflow their parent element, hiding the overflow without changing the layout seems sensible.
What are the reasons behind this decision, assuming they are known? Have the people who worked on the spec described why this was decided to be the case?
I asked about this on the mailing list on your behalf; the thread can be found here. In summary, this has to do with scrolling content for the most part:
Fundamentally, because if the spec didn't say this, then having floats intersect with something that's scrollable would require the browser to rewrap (around intruding floats) the contents of the scrollable element every time it scrolls. This is technically what CSS 2.0 required, but it was never implemented, and it would have been a huge problem for speed of scrolling.
-David
Most likely, it refers to scrollable content in a box that may occur outside of the float's parent but would intersect with the float. I don't think this is related to rewrapping content around a float within a scrollable container, as that already happens naturally, plus the float would clip into the container and scroll along with the rest of its content anyway.
Finally this makes sense to me. In fact, I'm going to provide an example here so hopefully it makes sense to you and anyone else who may be wondering. Consider a scenario involving two boxes with the same fixed height and overflow: visible
(the default), of which the first contains a float that stretches beyond its parent's height:
<div>
<p>...</p>
</div>
<div>
<p>...</p>
<p>...</p>
</div>
/* Presentational properties omitted */
div {
height: 80px;
}
div:first-child:before {
float: left;
height: 100px;
margin: 10px;
content: 'Float';
}
Notice the similarity to one of the examples given in section 9.5. The second box here is simply shown to have overflowing content for the purposes of this answer.
This is fine since the content will never be scrolled, but when overflow
is set to something other than visible
, that causes the content to not only be clipped by the bounds of the box, but also to become scrollable. If the second box has overflow: auto
, this is what it would look like had a browser implemented the original CSS2 spec:
Because of the float, attempting to scroll the content would cause the browser to have to rewrap it so it doesn't become obscured by the float (and what should happen to the part that scrolls out of the top edge?). It would probably look something like this when scrolled to the bottom:
The catch here is that the browser has to rewrap the content every time it repaints it during scrolling. For browsers that are capable of pixel-based smooth scrolling — which is to say, all of them — I can see why it would be a performance disaster! (And a user experience one, too.)
But that's for when the user can scroll the content, right? This would make sense for overflow: auto
and overflow: scroll
, but what about overflow: hidden
?
Well, a common misconception is that a container with overflow: hidden
simply hides content by clipping and cannot be scrolled. This is not completely true:
While scrolling UI is not provided, the content is still scrollable programmatically, and a number of pages perform just such scrolling (e.g. by setting
scrollTop
on the relevant element).-Boris
Indeed, this is what it'd look like if the second box was set to overflow: hidden
and then scrolled to the bottom with the following JavaScript:
var div = document.getElementsByTagName('div')[1];
div.scrollTop = div.scrollHeight;
Again, notice that the content would have to be rewrapped to avoid being obscured by the float.
Even though this wouldn't be as painful for performance as had scrolling UI been available, my best guess is that they made boxes with any overflow
value other than visible
generate a new BFC mainly for the sake of consistency.
And so, this change was brought about in CSS2.1, documented here. Now if you apply an overflow
value other than visible
only to the second box, what a browser does is push the entire box aside to make way for the float, because the box now creates a new block formatting context that encloses its contents, instead of flowing around the float. This particular behavior is specified in the following paragraph:
The border box of a table, a block-level replaced element, or an element in the normal flow that establishes a new block formatting context (such as an element with 'overflow' other than 'visible') must not overlap the margin box of any floats in the same block formatting context as the element itself. If necessary, implementations should clear the said element by placing it below any preceding floats, but may place it adjacent to such floats if there is sufficient space. They may even make the border box of said element narrower than defined by section 10.3.3. CSS2 does not define when a UA may put said element next to the float or by how much said element may become narrower.
Here's what it looks like with overflow: auto
for example:
Note that there is no clearance; if the second box had clear: left
or clear: both
it would be pushed down, not to the side, regardless of whether it established its own BFC.
If you apply overflow: auto
to the first box instead, the float is clipped into its containing box with the rest of the content due to its fixed height, which is set to 80px
in the example code given above:
If you revert the first box to height: auto
(the default value), either by overriding or removing the height: 80px
declaration from above, it then stretches to the height of the float:
This happens to be new in CSS2.1 as well, in that an element with height: auto
that generates a new block formatting context (i.e. a block formatting context root) will stretch vertically to the height of its floats, and not just enough to contain its in-flow content unlike a regular box. The changes are documented here and here. The change leading to the side-effect of shrinking the box so that it does not intersect the float is documented here.
In both of these cases, no matter what you do to the second box, it will never be affected by the float because it has been restricted by the bounds of its container.
这篇关于为什么CSS2.1定义溢出值而不是“可见”建立一个新的块格式化上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!