Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does CSS 'overflow:hidden' work to force an element (containing floated elements) to wrap around floated elements?

Tags:

css

Anyone know why overflow:hidden forces an element with floated elements to wrap the elements?

I really want to understand the inner workings rather than just using it and trusting that 'it just works'.

I can understand how it works when the containing element is floated in the same direction as child elements that are floated, but overflow:hidden means to crop overflowing content (when used with position:absolute/relative).

Any info appreciated.

like image 349
Mark McDonnell Avatar asked Aug 03 '10 21:08

Mark McDonnell


2 Answers

Floats, absolutely positioned elements, inline-blocks, table-cells, table-captions, and elements with 'overflow' other than 'visible' (except when that value has been propagated to the viewport) establish new block formatting contexts.

In a block formatting context, each box's left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch). This is true even in the presence of floats (although a box's line boxes may shrink due to the floats), unless the box establishes a new block formatting context (in which case the box itself may become narrower due to the floats).

The block formatting context clears the floats. Source: http://www.w3.org/TR/CSS2/visuren.html#block-formatting

like image 99
meder omuraliev Avatar answered Oct 08 '22 05:10

meder omuraliev


Full explanation from the Visual formatting model, part 9.2 "Floats" of the CSS2 specs:

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 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.

like image 2
You Avatar answered Oct 08 '22 03:10

You