Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does overflow: hidden have the unexpected side-effect of growing in height to contain floated elements?

This question is best explained by this fiddle, with the following HTML:

<div class="outer">     <div class="inner-left"></div>     <div class="inner-right"></div> </div> 

CSS:

.outer {     width: 100px;     border: solid 5px #000; } .inner-left {     float: left;     height: 200px;     width: 50px;     background: #f00; } .inner-right {     float: right;     height: 200px;     width: 50px;     background: #0f0; } 

Basically, I'm wondering why does overflow: hidden cause the outer element to grow in height, encompassing the two floated elements?

like image 240
Felix Avatar asked Oct 08 '12 13:10

Felix


People also ask

What does overflow hidden mean in CSS?

overflow:hidden prevents scrollbars from showing up, even when they're necessary. Explanation of your CSS: margin: 0 auto horizontally aligns the element at the center. overflow:hidden prevents scrollbars from appearing.

What does overflow overlay do?

overflow:overlay stops the scroll bar having that effect, in Chrome-based browsers.

What is the purpose of overflow in CSS?

The CSS overflow property controls what happens to content that is too big to fit into an area. This text is really long and the height of its container is only 100 pixels. Therefore, a scrollbar is added to help the reader to scroll the content.

How to set overflow height in CSS?

In order for overflow to have an effect, the block-level container must have either a set height ( height or max-height ) or white-space set to nowrap . Setting one axis to visible (the default) while setting the other to a different value results in visible behaving as auto . The JavaScript Element.


1 Answers

To put it most simply, it is because a block box with an overflow that isn't visible (the default) creates a new block formatting context.

Boxes that create new block formatting contexts are defined to stretch to contain their floats by height if they themselves do not have a specified height, defaulting to auto (the spec calls these boxes block formatting context roots):

10.6.7 'Auto' heights for block formatting context roots

In certain cases (see, e.g., sections 10.6.4 and 10.6.6 above), the height of an element that establishes a block formatting context is computed as follows:

[...]

In addition, if the element has any floating descendants whose bottom margin edge is below the element's bottom content edge, then the height is increased to include those edges. Only floats that participate in this block formatting context are taken into account, e.g., floats inside absolutely positioned descendants or other floats are not.

This was not the case in the original CSS2 spec, but was introduced as a change in CSS2.1 in response to a different (and much more pressing) issue. This answer offers an explanation for the changes. For this reason, it seems quite apt to call it a side effect, although the changes were very much intentional.

Also note that this is not the same thing as clearing floats (clearance). Clearance of floats only happens when you use the clear property and there are preceding floats to be cleared:

  • If you have an element with clear: both that's a sibling of the outer element in your example, the floats will be cleared but the outer element will not stretch.

  • If you have a similar element (or pseudo-element) as the last child of the outer element instead (making it a following sibling of the floats), the outer element will stretch downward in order to contain the bottom edge of that element, and for a zero-height element that essentially means the bottommost edge of the floats. This technique is known as "clearfix" because the element (or pseudo-element) has no other purpose than to force the outer element to contain the floats by way of clearance within it.

like image 169
BoltClock Avatar answered Nov 11 '22 21:11

BoltClock