I am available for freelance work! Click here to email me.

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.

  • 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.
  • 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.
  • Oleg
    overflow: hidden
    _height: 1%

    works 99% of the time just as well.
  • 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.
blog comments powered by Disqus
Purify - Issue Tracker

Jim Neath is a 26 year old Freelance Ruby on Rails developer from Manchester, UK.

Recommend Me

Categories