Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floated Child Elements: overflow:hidden or clear:both?

As a web developer I frequently will have two floated (child) divs inside of another (parent) div. Actually I do this all day long.

<style type="text/css">     #left {float:left;}     #right {float:right;} </style> <div id="parent">     <div id="left" class="child">&nbsp;</div>     <div id="right" class="child">&nbsp;</div> </div> 

This doesn't work without an extra bit of css/html because the parent doesn't automatically grow to fit floated children. There are two popular ways of overcoming that:
1) Add overflow:hidden to the parent's css.
2) Add a 3rd "clearing" child <br style="clear:both;" />.

I know there's a few other similar questions about such things, but my question is:

Which method is better and why? What are the pros and cons of each?

like image 672
tybro0103 Avatar asked Apr 15 '10 19:04

tybro0103


People also ask

Why does overflow hidden clear floats?

The reason why the wrapper doesn't stretch to contain its floats by default is because the floats are taken out of the normal flow of the wrapper, so the wrapper acts as if they were never there. If there is no other content in the wrapper, that means the wrapper won't have any height.

How do you clear a floated element?

Clearing Floats The footer should sit below both the sidebar and main content. To clear a float, add the clear property to the element that needs to clear the float. This is usually the element after the floated element. The clear property can take the values of left , right , and both .

What are two valid techniques used to clear floats?

What are two valid techniques used to clear floats? Use the "clearfix hack" on the floated element and add a float to the parent element. Use the overflow property on the floated element or the "clearfix hack" on either the floated or parent element.

What does floating an element do in CSS How do you float an element?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).


1 Answers

  1. Hidden overflow - pretty solid method. The main disadvantage is if you set a height on the parent element, any overflow will be...well, hidden. I found this when creating a menu with floated list items - the submenus would not appear.

  2. Clearing element - rather than a line break, I would use a div with height: 0; clear: both; since it won't create a gap below. This is a more solid method, the only disadvantage being an extra element in the markup.

  3. Float the parent - in my experience there are too many situations where you don't want to float the parent element, so I would avoid it.

  4. You can also use the generated content method:

    #parent:after {   content: ".";   visibility: hidden;   clear: both; } 

    This saves the need for an extra element in the markup, but it won't work in IE7 and below.

  5. Use inline blocks - just remembered this method. Instead of floating the two columns, set them to display: inline-block and they will appear side-by-side:

    .child {   display: inline-block;   vertical-align: top; } 

    Only thing you must remember with this method is if there is any whitespace between the close tag of one block and the opening tag of another, a space will appear between the columns (the size of which depends on the font so it difficult to gauge). As long as you do ...</div><div id=... then this method works fine and is superior to floating elements IMO.

like image 192
DisgruntledGoat Avatar answered Sep 23 '22 10:09

DisgruntledGoat