Our Mindfly Blog

Website Design and Development

Random creative design element

The New (Old) Way To Clear Floats

by Kyle Weems 16.January 2008 12:28

For a brief period this week, I felt like a crewmember of the ship of a modern day Bjarni Herjolfsson, part of a group of brave pioneers that was the first to discover something really new. Then, about ten minutes later, I realized I was instead a deluded, 500 years late Chris Columbus, whose only genuine claim to fame is to have confused the Bahamas for India... which I suppose is a natural mistake, as the two might vaguely resemble each other from a distance.

Late as I might be to the topic at hand (which contrary to the prior paragraph has nothing to do with the discovery of the new world), I've nonetheless decided to discuss something that I think is worth further dissemnation, as I've been literally breathing CSS for the past year (this involves a very unusual respirator that attaches directly to a monitor) and only just now learned about it. Let me try to explain what I'm actually talking about, with less metaphors.

When my friends are too lazy to move out of hearing range, I often tell them that the most common problem encountered in web design is creating a web site with two or more columns, followed by a footer (or any content, for that matter). The problem encountered is that to get two separate columns of items to appear on a web page, you usually have to do one of three things:

  1. Use tables (gasp!)
  2. Absolutely position the columns, and then push the footer beneath the columns using JavaScript.
  3. Float the columns, typically using something like the 'liquid column layout with negative margins' technique described quite nicely here by Ryan Brill.

There's several reasons why the quirky techniques mentioned are used. I'm really too lazy to sum it up myself, but trust me that straight forward solutions are never enough. Of those three techniques, the first is an abomination, the second is annoying as heck due to a number of browser/javascript issues, and the third is actually quite nice. In fact, I often use the last technique (or modified versions thereof) on a regular basis. However, it does present a small issue that gets many web designers in a tizzy.

Web designers are excitable like that. It might have something to do with the caffeine levels in our blood streams.

The issue is that in an ideal world, a web page should be separated into two discrete parts. All of the content of a page should be in the HTML code, and all of the presentation of a page should be the responsibility of the stylesheets. Furthermore, for the HTML to be truly clean and nice, elements should only be in the page if they actually contribute to the content. In the negative margin technique described by Mr. Brill, we break that rule by adding a small, mostly harmless div below our columns, with nothing in it. It exists only to hold a place for our CSS, which uses it to clear the floats with a "clear: both;" style. Without it, the whole thing doesn't work. This is all well and fine, but it means that we've got an element in our HTML that provides NOTHING to the content! 

Calamity!

This is, sadly, a corruption that most web designers, including myself, have allowed in order to make those liquid column layouts work. We'd hide our little divs in shame, secretly accepting their necessity while publicly smiling and pretending that nothing out of the ordinary is occuring. However, one day while trying to fix a problem in IE6 (which is, it seems, the primary source of my work), I stumbled across an interesting blog post at SitePoint, discussing the clearing of floats. It described, amongst other things, a solution to the classic problems faced by floating columns by simply adding either "overflow: auto;" or "overflow: hidden;" to the parent element holding the columns.

Intrigued and excited, I did some more searching. Sure enough, people were using this technique and talking about it. I did some tests, and not only did it work like a charm, but it helped clear up the particular IE6 difficulty I was having.

The technique is so simple to describe, I'm basically repeating myself by going into the following details. When you've got one or more floated elements (such as the floating columns I've been discussing above), and you want to wrap the parent element around them without adding dirty little clearing divs to your markup, you can instead add either the "overflow: auto" or "overflow: hidden" style to the columns' parent element. To make things stick with IE6, you'll also want to add an IE6 specific style for "height:1%" (see how to apply styles only to IE6 without causing CSS validation issues in my post 'Say Goodbye To The Stars'). There is the potential for you to have problems with scrollbars, but I find that usually one of the two values ('auto' or 'hidden') is clear of these. Some considerations regarding your design are also worth taking. Absolutely positioned elements that would normally appear visually outside of their parent will be hidden by the overflow style, so you need to be sure that you're not cutting anything off. Generally, though, I've found that this is a fantastic way to clean up my markup while still keeping those columns in check.

I was jazzed about this small CSS change in a way that pretty much results in me getting uninvited from any future parties with most of my peer group. Furthermore, I felt like I was part of something new and hip, that not a lot of designers new about. I was relevant!

Then I stopped to actually look at the posting dates of all those blog entries. They were from 2005. Early 2005.

*sigh*

Does discovering something three years after the fact still count as 'leading edge'?

No? Oh well. Back to the drawing board. But timing aside, I think this is a nifty weapon in a designer's arsenal that everyone should have. 

Comments

Patty Tudor

Patty Tudor said on January 17, 2008 (23:41)...

Another way to go about it is to define a class in your CSS that is {clear: both} and name it something like "clearboth".

Then no matter how many elements you are floating, to either side, you can clear the float status with a [br class="clearboth" \] after the floated elements.

(brackets above substituted for the correct braces, of course)


Kyle

Kyle said on January 18, 2008 (09:11)...

Yes, this does work. It's the clearing element technique, and is part of the technique discussed in the article by Ryan Brill. It's a very effective trick, and I've used it a lot. But in an ideal situation you want to separate content and presentation as much as humanly possible, and adding any HTML elements (including breaks) for the sole purpose of holding a clearing style disrupts that separation. So, when possible, using something like the technique I described here helps preserve the division, and thus is 'cleaner'.


Kat

Kat said on February 21, 2008 (03:12)...

I also only found the overflow clearing technique recently. I can't believe it's been around so long and I managed to not hear about it till now!

The problem you mentioned with Absolutely positioned elements:
"Absolutely positioned elements that would normally appear visually outside of their parent will be hidden by the overflow style"
actually only happens in Firefox, not IE. Setting overflow seems to create a new stacking context in Firefox. It's a bit odd and I don't know why it happens or if it is correct behaviour...
Does anyone know why this happens or how to work around it? I'd love to know if there's a work-around!


Kyle

Kyle said on February 21, 2008 (09:12)...

To my understanding, Firefox would be behaving correctly here. The concept of overflow:hidden is that it hides any children/content that goes outside of the borders of the element. I'm fairly positive that is meant to include any absolutely positioned children as well. Mind you, I'm not really the type to read W3C CSS specs (which are written in some sort of obscure browser-maker language), so I can't quite say for sure. But usually, when there's a rendering difference between IE and Firefox, 99.9% of the time Internet Explorer is the browser rendering the style incorrectly.


Kat

Kat said on May 3, 2008 (13:04)...

@Kyle I'm not really the type to read W3 spec either so I'm certainly willing to admit Firefox might have the more correct implementation. I've just been hoping vainly that Mozilla is wrong because the IE way is much more useful for what I've been trying to do!


Kyle

Kyle said on May 5, 2008 (09:19)...

From what I've seen since my last comment, Firefox is in fact doing the stacking context correctly, and not IE.

I really can't recommend ever relying on the IE rendering of a design instead of how another browser is showing it. Granted, you'll need it too look correct in all of them, but use the more standards-compliant browsers as your baseline and then fix to IE, instead of the other way around.


Add comment



(Will show your Gravatar icon)  









Live preview

said on May 15, 2008 (06:22)...


 

Powered by BlogEngine.NET 1.2.0.0. Original Design by Heather Alvis.
Sign in

Bellingham, Washington
Copyright © 2007 Mindfly Inc. All Rights Reserved.