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


I think the css code examples need to be swapped. The max width css example is actually for the word wrap. The word wrap css example is actually for the max width.
I actually prefer display:inline-block… works almost the same way…
Recent surveys, children of depressed mothers’ negative patterns of activity occurring in different brain reveals. This is for children of mothers who take more risks in the future is going to have depression.
“word-wrap:break-word” only suits IE isn’t it ? So how to deal with the problem in other browser ?
… i made a mistake. I know the “word-wrap” should match the setting of “overflow:hidden”.
ewvwvverrevrvr
;l;;;;;;;;;;;;;;;;;;;;;;;;;;;;yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyyyyyyyyyyyklfffffffffffffff
really is very useful and very important point of view.
really good useful information I usually use clear float which I am not too happy with but will def try this out, btw love the new site, miss the old flowery one though..
i’m new learner . and i’m always confused by the overfloat code . i don’t know where should i coding it , or even when the problem appeased , i don’t know how to deal with it . your post inspirate me . thanks .
One difference between the overflow:hidden and clearfix methods is that overflow can interfere with horizontal alignment. I had an element centred with {width: 960px; margin: 0 auto;}. Adding overflow:hidden to the element pushed it hard left in Firefox and Opera (it rendered fine in Chrome). I ended up reverting to clearfix as it worked across all three browsers. I also know how it works whereas I never understood why overflow cleared floats.
Thanks dear….
this is really an awesome solution. i try it and it works well
What is friendship?Mont Blanc PenWhy do we call a person our friend? When do we call someone a very good friend?
I would just like to say, what a beautiful header and footer you have here Web Designer Wall!
it is a great post
Useful posts…easily understadable about floating div’s. Word wrap and Max width examples swaped.
Awesome post! Very helpfull! Thanks for sharing!
This is verry good.
I enjoyed this great, simple and important article, however, I noticed that there is a small mistake at the end.
It seems that you’ve put the code for word wrap under the max-width title and vice versa.
thanks,
elmalak
Nice! That is the good solution. Thanks for sharing it..
The information provided by you is very useful for me and thanks for sharing it.
a good article!
mont blanc pens
And then he handed you the thirty-five 45