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


Overflow: hidden will also clip css3 box-shadow as I demonstrated in this blog post http://fordinteractive.com/2009/12/goodbye-overflow-clearing-hack/
Nice tip, handles the text perfectly
Cool stuff what I’m looking for is how two have on left float and one right float having the same height even if one has more content then the other anyone knows how to do that thanks
Nice article! Just one correction needed though – your code snippets for word-wrap and max-width are the wrong way round! ;)
“overflow: hidden;” is the best of them, “auto” can generate scroll-bars in some browsers.
I like the overflow auto technique. If I have scroll bar issues I find it easy to fix by throwing in some padding to the container in question.
I needed this… And as I suspected it came in verry handy. Thanx
Hadn’t really seen this way of clearing floats before. I’ll have to remember this one as it could prove very handy!
Very useful tip and explained very well infact the drawbacks you have mention also describring and solving the issue at same time.Thanks for sharing
is not easy to put on weight
http://webdesignersbest.com/
online project auction for clients and web designers
or buy services on site. web design, graphics etc.
Nice article, just one thing, IE 9 doesn’t render overflow: hidden correctly, it totally removes all content from view.
I’ve noticed that content won’t print properly using this methodology.
Hey this solution is good but why not use “float:left” instead?
This is very cool.
I wonder why using a “clear” fix is so popular. This technique is much cleaner, no extra elements.
Any cross browser drawbacks?
Thanks for this very useful tip.
I think the code for “Max-Width” and “Word-wrap” are in the wrong places…
Why would anyone use “clearfix” if this has been an option all along? This is great, thanks again Web Designer Wall Man!
I am a web designer at Creare Communications and use overflow:hidden in the majority of the websites I have created. One of the main benefits I find it brings is the ability to use the same styles on different pages, as this allows you to use a container which can expand or shrink to fit to the content within it.
Overflow:hidden is also helpful when viewing a website in all browsers. I have found that in IE your text renders differently to Safari or Firefox. This can be an issue if you have a height on your container which can then cause the text to be cut short. By simply taking the height off and replacing it with overflow:hidden it enables the div to expand to the size of the content within it.
Great article.
Your code-snippets for max-width and word-wrap are switched.
Nice article!
oldschool but practical; seriously awesome.