One of the common problems we face when coding with float based layouts is that the wrapper container doesn't expand to the height of the child floating elements. The typical solution to fix this is by adding an element with clear float after the floating elements or adding a clearfix to the wrapper. But did you know you can also use the overflow property to fix this problem? It's not a new CSS trick either. It's been documented before long long ago. Today I would like to revisit the topic along with a few tips.
Demo 1:
The demo below shows the issue with floating child elements where the parent container doesn't expand. To fix this problem, you can simply add the CSS property overflow:auto (or overflow:hidden) to the wrapper container. This is perhaps the simplest way to clear floats.
.container {
overflow: auto;
}
Demo 2:
Overflow:auto can also be used to prevent content from wrapping around the floated element. Let's say you are designing a comment list. You would, most likely, have an avatar floating on the left and the comment to the right. To prevent the comment from wrapping around the avatar just add overflow:hidden to the comment container. The advantage of using overflow here is that I don't have to set a float or width to the comment container. The container automatically aligns after the floated avatar image.
.image {
float: left;
}
.text {
overflow: hidden;
}
Drawbacks (see demo)
Although it is a nice trick to clear floats there are some drawbacks:
- Using overflow:auto will cause a scrollbar if your content is extending the boundary of the container. For example, if you have a long unbreaking text (ie. long url text) or a large image that is bigger then the container a scrollbar will show.
- To avoid a scrollbar from showing you should use overflow:hidden. Again, there is a drawback to this method as well. Using overflow:hidden will hide any content that exceeds the boundry of the container.
Word-wrap
To solve the long unbreaking text issue, simply add word-wrap:break-word to the container and it will force the text to wrap onto a new line.
.container img {
max-width: 100%;
height: auto;
}
Max-width
To prevent large images from extending the boundar, add max-width:100% and it will resize the image to the max width of the container.
.container {
word-wrap: break-word;
}


Okay this seems to be a revealing. I never knew it was possible to sort of ‘clear’ floats with the overflow auto!
So all those times i’ve been using and extra div for the clear it could and can also be done just by using the overflow auto on the container/wrapper of the floating elements?
I’ve tested it with a simple layout:
enough text
enough text
FOOTER
with the styles
div { margin:0; padding:0; }
.wrap {
margin:auto;
width:900px;
height:auto;
background-color:#9F3;
overflow:auto;
}
.footer {
margin:auto;
width:900px;
height:120px;
background-color:#0CF;
}
.left {
width:400px;
height:auto;
float:left;
background-color:#CC0;
}
.right {
width:400px;
overflow:auto;
background-color:#693;
}
And it works like a charm! not even need to flow the right column and the footer’s nicely pushed away.
I’ve tested this on all browsers except explorer. Could someone aknowledge it also works in explorer?
Great post!
Oh it seems that in my previous comment the html code won’t show properly.
I used that too some time ago. But this method has some problems with the page break when printing the website… at least in Firefox.
Using the clearfix instead:
.clearfix:after{content:”.”;display:block;height:0;clear:both;visibility:hidden;}
.clearfix{display:inline-block;}
* html .clearfix{height:1%;}
.clearfix{display:block;}
Works in IE6 but the class is needed in the markup.
@niko_lang
you mean when actualy printing out the website? Well since a site isn’t for printing it doesn’t realy matter. And if things on a site need to be printed just create an print-friendly page of an article or so to print.
Thanks for the tip. Putting in that extra DIV has always annoyed me.
I used to clear floats with overflow: hidden; bur now i prefer using the :after method described in this article : http://perishablepress.com/press/2009/12/06/new-clearfix-hack/
/* new clearfix */
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
* html .clearfix { zoom: 1; } /* IE6 */
*:first-child+html .clearfix { zoom: 1; } /* IE7 */
Nice reminder – I used to know this, but almost forgot about it … As chance would have it, I have a situation right now where I can use this! ;)
Very handy technique… helped me a lot recently. Good idea to post it ;)
Re: IE6 workaround
I used to always use this technique but tend to use the clearfix technique more now. The overflow technique is still handy though in cases where you don’t want to add an additional class to a div. However, if you need to support IE6, using overflow: hidden alone doesn’t work.
Solution
You need to either add height: 1% or zoom: 1
It seems you can add height:1% to the parent div without it effecting IE7, IE8, Chrome or Firefox. However to be a little more semantic, you should probably set up an IE conditional stylesheet specific to IE6. Alternately, if you aren’t against hacks in your css you could just use *height: 1%, which will only be rendered by IE6 and IE7.
Personally I go with an IE6 conditional stylesheet or zoom:1 (depending how lazy i am).
Unfortunately, at the moment I can only test this with IEtester so it may not be 100% reliable information. Maybe if someone can confirm with an authentic IE6 install.
Give it a go :)
I’ve been using this technique for years. It’s the best way to clear floats with no need to muck up the mark up.
There is a situation where the overflow method will cause problems. If you want to hang an element outside of a container, margin-left:-15px for example, then using overflow will get in your way.
I’ve found that using “display: table;” can also work. I haven’t used it extensively so I’m not really too sure of any problems it could present. Anyone know anything else about using it and any problems it could bring up?
Awesome technique, but watch out for elements styled with box shadows. If the parent container is using overflow:hidden, the drop shadow will be cut off. For such cases, I prefer the float everything method or the new clearfix method.
@Boolian
Are you for real? Man, forget about IE6!
Haha i ain’t paying attention just for that mistake called IE6 :)
This very thing kills me, having to add unwanted div’s and wrappers to my HTML. Thumbs up for this!
@Alex
I agree with you completely about IE6 :-) Most projects you can forget about it… However some of us work with corporate environment clients who still run IE6. Alot of these companies have core systems in place that only work in IE6 and are unable to upgrade… some just have lazy IT departments. It’s a sucky situation but what can you do. IE6 isn’t dead yet unfortunately :-(
IE6:height:1%;
Other:overflow:hidden;
I made similar before and it works ;)
Nice one Nick! Thanks ;)
Thanks for this tip. It would come handy, hope there are no cross browser issues with this trick.
Also wanted to point out that there are 2 mistakes in the post:
- The illustration for Comments Block says overflow:auto when it should be overflow:hidden
- Images for ‘Max-Width’ and ‘Word-Wrap’ need to be interchanged.
i m looking for like this tip. thank you