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


Very usefull tip. I didn’t know it, ant that cases have always been a pain for me. Thanks a lot for the help :D
I used it in a project a few days ago, it’s a good way to clear floats w/o clearfix. And I was getting a scrollbar. :) I’ve spend quite some time to figure everything out, as I’m just starting out and still learning many css topics mostly via trial and error.
On a side note, you should swap the code examples at the end of the article. :)
Thanks for the note, the end examples confused me for a few min too.
Thanks for the tip on the overflow hidden for comment text. Text wrapping around image always bothered me and I always had to come up with workarounds. Your tip will saved me lots of time.
I think there is a mistake:
In the text you are stating to give the text an “overflow:hidden” but the picture says “overflow:auto”. Or am I just confused?
I’ve always used overflow : auto as a means of clearing divs. The scrollbar issues have always been minor and tend to arise when you are working in a confined space, like a header with set height.
I had no idea about the overflow : hidden trick in your second example, that’s going to be put to use sometime soon.
There is a small mistake at the end: you’ve mixed the code examples of word-wrap and max-width fixes.
What? Huh? This is like a revelation? All this time I’ve been using the guillotine CSS to clear floats. If this is so easy and clean, howcome this hasn’t become mainstream? I’m surprised I never knew about this. Any catches? Browser compatibility?
Nice little post! You’re right – it’s a common issue with an easy solution that seems to have been forgotten. Very helpful, thanks.
You are right – I burrowed this solution deeply …
On quirksmode I read amending:
width or height required
The use of a width or height declaration is required to make the effect work in Explorer Windows and Opera. If you don’t include it Explorer Windows continues to show the border at the top of the columns, as if there were no overflow. Opera completely hides the content of the container.
A width: 100% is a neat starting point, although more complicated layouts may require other values.
Thanks for sharing this tip.
In terms of layout clears, I find this method pretty useful:
http://www.yuiblog.com/blog/2010/09/27/clearfix-reloaded-overflowhidden-demystified/
Cheers.
Useful, i’ve never heard about that :)
yes i know this tip it’s useful. it depend of design, whether use clear:both (e.g. footer) or, overflow:hidden. I always keep in mind both if i haven’t solutions i use overflow:hidden.
Been using overflow:hidden forever. But I question the design quite a bit on this. Why should we lose height properties of the container element when elements inside use float? That seems very unintuitive.
……. i only know hover
I use { display:table } to expand containers automatically.
Works like a charm, and still use margin, padding, etc…
Thanks for this I have been looking for such a solution!
can not work in IE6.0
Yeah, this method is incredibly handy. Although at this point when I do client work I generally avoid using overflow:hidden or auto. I can’t count how many times I’ve had to re-work the html/css of a layout to allow some tooltips or other overlay that would’ve been otherwise hidden by the container. Use with caution.
If you giive overflow:auto it will give more space which again you need to play with the width:
instead why not only give
.container{
background: none repeat scroll 0 0 #E6EEF2;
border: 1px solid #999999;
padding: 10px;
float: left; /*Add this Property*/
width: 94% /*Add this Property*/
}
Now if you check in any browser it will work similar No CSS hacks needed incase if you want to need this Container where there is no Float Needed and then make a class .noFloat with float:none and inherit it in Container.
Problem Solved
Thanks for this great article. It’s indeed an alternative to using additional clear elements.
But you mixed up the code examples for word-wrap and max-width. ;-)