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;
}


in this case instead of using the ‘div’ a simple ” after the element also addresses… not?
in this case instead of using the ‘div’ a simple ” after the element also addresses… not?
in this case instead of using the ‘div’ a simple br w/ clear=”all” after the element also addresses… not?
I used overflow:hidden; in most of the cases and really works fine.
thanks for tip about image
.container img {
max-width: 100%;
height: auto;
}
very nice tip! thanks!
Hi I like the comments section portion of your website that is nice and neatly designed. Can you please refer me how can I get this section to embuid in one of my website.
It fulfills my need
Top writeup! Floats really annoy me because when you havent floated everything right, it overflows. Thanks for the tip.
Fantastically displayed. really nice post to read about. Thanks for sharing. Our inhouse web designers would be happy to learn about.
I find myself using overflow:hidden frequently as in my opinion it is one of the more robust methods of clearing floats. There are occasions however where it’s not suited. For example on my homepage: http://rydzdesign.co.uk I have three screenshots of recent projects. Each has a subtle box shadow applied to it which if I used overflow:hidden would be clipped so I used clearfix instead. It’s yet again a case of right tool for the right job.
Thanks yaar, solved my problem for which i was searching stuff from the past six months. The thing which was useful for me was and one thing i had a error was i made parent tag relative and child tag position absolute which was the error from those days. I removed the position tag from them and added the clear div. Its working fine now. Thank you.
Great, it’s always good to know some tricks to make the page look as it should! Thank you
For those of you complaining that this doesn’t work in IE6, you need to trigger hasLayout on the container for it to clear. This can be done in a number of ways, but the easiest way would be to add width: 100%; to the .container class.
If adding a width messes up your design, you could add zoom: 1; instead which will also make it work in IE6.
woohoo thanks, solved my ie6 problem
Thanks for sharing such a nice article!!!!!!!!!!!!!!!!!
Interesting way to go about solving the clearing float problem. I always used the clearfix method. I guess it’s good to have a few other options. Adding another div to clear floats should definitely be avoided as it adds useless markup.
Exacly what i was looking for…Great Redesign by the way !
You have showed great perseverance behind the blog. It’s been enriched since the beginning. I love to share to with my friends. I spent almost half an hour daily here. Carry on.
Great tip I hate using div’s to clear my floats so this will come in useful for sure :-)
I tried this method but wasn’t able to continue with it because I had a number of tooltip-type flyouts that appear on hover and need to appear over/outside the border of the overflow:hidden/auto’d element. With this fix, the element showed scrollbars or just hid the popped up content. Any suggestion on how to make that work? Great redesign. Inspiring.
Nice tips. Looks like word-wrap and image width code are mixed up.