Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS child clearing parent float

I have a two-column layout, sidebar on the left, content on the right.

So I float the sidebar left, and give the content on the right a margin. All is fine and well...

Until, a child element of the content has a clear applied to it, it jumps below the sidebar.

Is there a way to make children element only float and clear within it's parent container?

As a picture is worth 1000 words, here's a JSFiddle:

http://jsfiddle.net/qRYYm/1/

like image 391
ahren Avatar asked Aug 28 '12 22:08

ahren


People also ask

How do you clear a float in CSS?

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 . Usually you'll want to use both.

How can we fix the problem with float left so that the parent div element doesn't collapse to height OPX?

Method 1 (Using Overflow Property): We can use the overflow property of CSS to prevent the parents from collapsing. Set the value of the overflow property as “auto” for the parent and it will not collapse any more.

What does it mean to clear a float in CSS?

The clear CSS property sets whether an element must be moved below (cleared) floating elements that precede it. The clear property applies to floating and non-floating elements.

When two children of a parent div are floated the parent will collapse this is solved by adding what to the parent?

Answer: Use the CSS clear property When float property applied to the element inside the non floated parent, the parent element doesn't stretch up automatically to accommodate the floated elements. This behavior is typically known as collapsing parent.


1 Answers

If you add overflow:auto; to .content that should see you right.

like image 179
Billy Moat Avatar answered Oct 06 '22 00:10

Billy Moat