CSS floats can present some elegant solutions… and some aggravating problems. Most of the problems relate to intelligently clearing a float. What follows are brief descriptions of a few methods for handling the problem. There is nothing new here. Everything here I got from somewhere else where it is described better and in more detail and with better illustrations. This page is just for my reference so I won’t forget and won’t have to keep looking this up because sometimes I go ages without having to deal with this, and then I forget.

If you want to go to the source on this, check out these articles:

Normal Floats

So normally, you want a float to have all subsequent text to flow around it. That’s exactly what you want.

Float Your Boat Gently Down the Stream: the case of normal floats. Merrily, merrily, merrily, floats are but a dream

To my right is a sort of title box like when magazines excerpt something. I’m not floated. Im’ just regular old text about floats.

Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream. Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream.Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream.Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream.Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream. Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream.Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream. Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream.Row row row your boat, gently down the stream, merrily merrily merrily life is but a dream.

Notice how the text wraps right around the box every so nicely?

Problem Floats

But what happens when the floated box is taller than the non-floated element?

I’m tall and floated left. Row row row your boat, gently down the stream. Row row row your boat, gently down the stream. Row row row your boat, gently down the stream (now doesn’t that make more sense than Lorem ispum in a post about floats?)

I’m not floated

And normally that pooches out the bottom and would even overrun this text here, except that this paragraph has clear:both. I could clear the tall grey paragraph and be done. That’s the normal, old fashioned way to do things. It works, but it’s inelegant and sometimes it doesn’t actually work that well. For example, here’s the problem that got me digging around again. If this looks right, you’re not using IE. You can see some funkiness in IE7, and even more in IE6 if I recall right. Here’s how it looks in your browser and a screenshot of how it looks in IE7:

This version is rendered by your browser. I’m tall and floated left. Row row row your boat, gently down the stream. Row row row your boat, gently down the stream. Row row row your boat, gently down the stream (now doesn’t that make more sense than Lorem ispum in a post about floats?)

I’m not floated. I’m not floated. I’m not floated. I’m not floated.

I’ve got clear:both set. Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum

And the screenshot:

Floated paragraph doesn't respect padding.

Floated paragraph doesn't respect padding.

Alternative Float Solutions

Sometimes, though, that’s an annoying solution and doesn’t really work. There are all kinds of hacks and what-not ranging from Shaun Inman’s absolute position and javascript solution to the clearfix:after solution. It’s giving me a headache, and the latter doesn’t work in the particular problem I encountered today where the containing element had a border, so forcing a clear is a problem and the Inman solution would work, but I really just didn’t want to add the javascript to the Wordpress theme I was fixing up (by the way, aside from that problem, it’s a nice theme called gardenz).

The Smarter, Better Way to Float

So then I stumbled on the slickest way to handle this. It’s an old method, but I had not seen it until recently and I just had to write it here not because it’s news, but so I wouldn’t forget it. It is described much better in the articles I mention in the credits, so if this doesn’t make sense in my disorganized sort of way, go there. Anyway, here it is: overflow: hidden a technique that dates back to at least 2005 and which, furthermore, appears to be based not on some hack, but on actually writing CSS the way the spec intended. The spec on height and ‘auto‘ (overflow being an attribute that effect height) states quite directly:

In addition, if the element has any floating descendants whose bottom margin edge is below the bottom, then the height is increased to include those edges.

That comes from a comment by Anne on the post over at 456 Berea. It’s so simple. You just apply this to the element that contains the floated element:


.floatedElement {overflow:hidden;
height:1%;}

Halleluja! Praise the Lord as only someone who has wasted countless hours on stupid float problems can praise something quite that geeky. Basically, it’s this simple, to take the example above.

A couple of notes: the reason overflow is hidden is so that you don’t get scroll bars in IE/Mac which otherwise adds scroll bars whether there is any overflow or not. You need to set a height or a width to get it to work in IE and Opera. I’ve opted to set a height, since a percentage height is meaningless in most circumstances and doesn’t affect the rest of the layout. It’s probably more robust across browsers if you set a width of 100%, but this creates a problem if you are using a standard box model and you have margins, padding and borders.

I’m tall and floated left. Row row row your boat, gently down the stream. Row row row your boat, gently down the stream. Row row row your boat, gently down the stream (now doesn’t that make more sense than Lorem ispum in a post about floats?)

I’m not floated.

And now this paragraph is not cleared, but it’s still safe! Hopefully anyway.

Tagged with:

Filed under: Uncategorized

Like this post? Subscribe to my RSS feed and get loads more!