Valentin Agachi

freelance web developer

request quote!

Clearing floats into rows

Color paletteWhen you have a lot of floats which you wish to style in a table-like manner, you will run into some trouble. We all know you shouldn't use floats to display tabular data, but if you want to create a palette of colors or some other kind of widget which needs this kind of styling, you will be tempted to use floats.

The issue

When you have a lot of floats and wish to break them in rows without any extra markup, you can do the following. Let's say you want to have only N columns and the width of the container is larger than the width of N columns. For every float which is the (multiple-of-N)th you will style it with: clear: left. You will think that it will be perfect. But as we've seen in the world of browsers' bugs, it ain't.

The browsers are split up in 2 parts:

Firefox 1.0.4, Opera 8, Netscape 7.02
the ones which display what you expect
Win IE 6, Win IE 5.x, Opera 7.23
and the ones which don't, and display as intended only the first row of cells and the cells to which you apply clear: left, the other cells just try to fill up the previous rows as long as parent's box width allows.

Check the test page.

Can anyone tell for sure which of these browsers obey the CSS specs? I have read the floats section of the CSS 2.1 specifications, and I tend to believe that Firefox and all the Mozilla-based browsers plus Opera are handling this issue correctly, whereas IE misinterprets the specs. You can tell that this is the right way to behave from Opera's behavior. Up until 7.23 Opera gives the same error as IE, but in the 8 version (which is a very recent build) we can see it render as Firefox, which shows us that the Opera developers have came on the right path.

Solutions

Fortunately, there are workarounds to this issue.

For example, you can insert all N cells that would make up a row into a smaller row-container.

Or, you can set the width of the container to somewhere around N * (width_of_cell + margin_of_cell)

 

So in conclusion, IE is again showing us what it wants in its own wrong way as always.

Comments

at 14:45 on 24/Apr/2006

Akira

Comment by Akira

Very good example of this bug... very useful... congratulations!

at 01:36 on 11/May/2006

Dave Bacher

Comment by Dave Bacher

"The outer top of a floating box may not be higher than the outer top of any block or floated box generated by an element earlier in the source document. " -- CSS 2.1

"A floating element's top may not be higher than the top of any earlier floating or block-level element. " -- CSS 1.0

FireFox is correct.

at 22:10 on 09/Jul/2006

Akira

Comment by Akira

You can create an empty DIV between the rows...

Who's this guy?

Hello! I am Valentin Agachi, an ambitious web developer, and you are viewing my site and weblog.

If you care to find more details about me, you can check out my CV page, or you can contact me.

Valentin Agachi's profile on LinkedIn Download vCard

About this entry

You are reading a post entitled "Clearing floats into rows".

It was posted on 09 June 2005 and tagged with Bugs, CSS.

Technorati tags: Bugs CSS

Post this:

© 2003-2010 Valentin Agachi. All rights reserved.