Self Clearing Floats in CSS

November 15th, 2008

This is a quickie mainly to remind myself. My friend and fellow Fudge developer, Mike “1312” Byrne showed me a CSS trick to have divs clear themselves.

div#container
{
  height: 1%;
}

div#container:after
{
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}

Brilliant. This works in Safari, Firefox and IE6+ as far as I know.

(Possibly) Related Posts

  • No related posts
Recommend Me

If you found this post or anything else on this site of any use, then please take the time to recommend me on Working with Rails.

You can follow any responses to this entry through the RSS 2.0 feed. Trackback from your own site.

4 Responses to “Self Clearing Floats in CSS”

  1. November 16th, 2008 at 12:22 pm - Christian Says:

    This is the clear-fix solution. However, for most uses it really is outdated, using overflow: auto; will do the trick most of the time.


  2. November 17th, 2008 at 4:43 pm - Oleg Says:

    overflow: hidden
    _height: 1%

    works 99% of the time just as well.


  3. November 27th, 2008 at 11:05 pm - Aleksey Gureiev Says:

    I would say, just “overflow: hidden” works 99% of the time. :)

    For IE6 you may want to specify the width of the container explicitly, but again, only if the overflow rule doesn’t work, which is extremely rare.


  4. December 17th, 2008 at 4:21 pm - John Nunemaker Says:

    Most people don’t know that adding overflow:auto on the parent of a floated element will automatically make the parent contain the floated child. Also, you can float the parent (float nearly everything) and that will fix the issue.


Leave a Reply

Jim Neath is a 24 year old Ruby on Rails developer from Manchester, UK. Contact Jim Neath

Recommend Me

Categories

Stalk Me