The Blog

IE7 float clearing

Remember this?

CSS:
  1. * html SELECTOR { height: 1% }

This is one of the ways you can force IE6 to clear the floats in a container. And it partially works in IE7 too. The reason height:1% works is that IE7 internal engine is same as IE6, just with a lot less bugs in it. Why is float clearing a problem then? Because this part of the selector: * html does not work anymore — that bug is fixed.

However, IE7 still incorrectly interprets height:1% rule and will automatically expand the height of the box in question if its floated content can’t fit. Thus, that trick is still a viable solution to float clearing, especially since IE7 does not support :after selector yet, thus the solution working in all other modern browsers is not usable:

CSS:
  1. SELECTOR:after {
  2. content: ".";
  3. height: 0;
  4. display: block;
  5. visibility: hidden;
  6. overflow: hidden;
  7. clear: both;
  8. }

The way forward

The * html will not match anything in IE7 and will have no effect at all. This also is an ugly hack: we use one IE bug applied through another IE bug. Ugly. Such hacks should be used only as last resort, when no other solutions are available. And IE has perfect solution available, called conditional comments.

Thus, using conditional comments the height:1% is still a fine solution for float clearing. However, there is a better one. IE7 now supports min-height rule (among others) and that rule also triggers the infamous hasLayout property in IE7 internal engine and thus it is usable for float clear.

CSS:
  1. SELECTOR { min-height: 0; }

This is simple to use (just put it at the start of your CSS file) and enumerate the elements which content should be cleared — the same list you use for IE6. And all is fine, because this can sit in your main .css file and no modern browser would choke on it.

IE7 has good enough support for the CSS2.1 that you should very rarely need to write IE7-only hacks, thus have little use for separate file containing IE7 tweaks. Actually, the float clear is the only thing I needed to change in all the sites I created in the last few years to make them working in IE7.
What a welcome change.

Banca

Banca

Beautiful and functional currency converter, supports just about any currency in the world.

Go Couch to 5k

Go Couch to 5k

The most popular starter running program in beautiful feature-rich app (GPS tracking, charts, detailed history etc)

Quickie to do

Quickie to do

The fastest short-term task-list / check-list app on the App Store. Really.

Guerrilla Cardio

Guerrilla Cardio

The most challenging high-impulse interval training in the world.

Run Mate

Run Mate

A versatile running coach app, with unlimited number of running programs. Perfect for casual runners.

10 Comments

Feel free to chime in, looking forward to it. Leave a Comment

  1. Tim Connor says:

    Welcome back to the web dev blogging world, Alek!

    Have you seen this, then: http://www.zeldman.com/2006/10/27/ie7showandtell/ ?

  2. Tim Connor says:

    Oh, and since you have moderation on you might consider some user feedback — like “Your comment has been submitted” and hiding the form/submit button.

  3. Aleksandar says:

    Thanks Tim. Yes, I have, 4th comment on Zeldman’s thread is mine. :)

    Thanks for the suggestion, I will see about doing something like that, should not be hard, although a lot of time went since I dived through WP code.

  4. Laurens says:

    What about the zoom-property to force hasLayout?

    This article tells it’s compatible to all versions of IE since IE4!?

    http://www.stuffandnonsense.co.uk/archives/clearing_floats_without_structural_markup_in_ie7.html

  5. Aleksandar says:

    zoom is fine for the job, but since it is non-standard property, you would need to put it hidden under IE conditional comments. min-height is standard CSS rules and thus can go into your main CSS file for the site. That makes a big difference to me.

  6. Fuji says:

    hi i did have the same problem with floating a table inside a div where it leaves a higher gap just below the div container.

    so it used this code

    float: left !important; /* Mozilla & Firefox */
    float:none; /* Ie 7*/

  7. Thanks a lot-you saved me a lot of time and nerves.
    Hvala jos jednom :)

  8. evan says:

    OK, so I’m completely perplexed as to why Mozilla correctly displays my site only 9 out of 10 times. I have a two separate div’s for the text nav and the content image, each with clears and floats, respectively positioning them to the left and right. When clicking on several links on the left (use the Industrial Design link at the top for the page with the most links) the image content will occasionally be positioned below the bottom of the text nav. But, when you reload the page, all is well again. What’s going on, and how do I fix it??

    Thanks!!!

  9. Aleksandar says:

    Evan, I thin that you may have hit some strange Firefox bug. However, I think that you can resolve this by removing some of the style rules there.

    For instance, since you have content fixed at 500px width, then you don’t even need overflow, clear not float. Left column is fixed and floated and there is more than 500px on the right so that you don’t need all those stuff — it will sit nicely on the right.

  10. Marc Watts says:

    I got sick of adding clear float solutions using class names such as “clearfix” to elements.

    So now I just add it to most block level elements except for p and footer and a few others. I wrote an article about automatic clear float solution and it has been working really well for me.

Leave a Comment

Add your comment here, just please be civilized and stay on topic.