skip to content rich footer

stevenclark.com.au

subscibe to the StevenClark.com.au rss feed

Containing Floats (Redux)

Nothing about this post is new or unknown to many people much smarter than myself. I don’t usually post CSS solutions here because there are fantastic resources out there already. This CSS problem is simple - an outer div contains floats. The floats pop out the bottom of the div and this is shown best by putting a simple border on the div to show its got no height. This is exactly what’s meant to happen. Although, in Internet Explorer the float is contained.

But if we’ve got a situation where we want floated content to stay wrapped within the div what do we do?

Problem: Uncontained float is not inside the div

I’m posting this because nearly every time I see advice on this question the answer put forward is to add an extra div into your markup at the end of the initial enclosing div (after the floated content) and setting the CSS to clear that element. That’s simply clear: both;.

The problem with that situation is that you’re adding markup to appease the problem rather than keeping it in the stylesheets. After all, this is a presentation problem and not a content problem.

I don’t know what other people do with this one but here’s my solution that’s kept me on the straight and narrow for the last few years at least. I have three options which I can apply to the container div to make the floats stay within its borders.

  1. overflow: auto;
  2. overflow: hidden;, or
  3. display: table;

Solution: Contained float using some CSS on the containing div

I mainly use the overflow: auto solution and haven’t had any unhealthy side effects so far - if you know of any down side kindly comment and put me straight. I just thought it might be useful posting this solution simply because so many professional forums and websites insist that you need to add another div to your markup - god forbid!

Update: 3/6/08
For those who develop only in Firefox then fix Internet Explorer later you might want to look at this in both browsers. I tend to develop across both rather than doing one then addressing the other at the end. With this particular issue, Internet Explorer wrongly contains the float while Firefox and other browsers require this fix. So to answer someone’s recent question, my understanding is that display: table; fixes the containing issue in Firefox (and others) and is fine because Internet Explorer (not having table) should just ignore it. Everyone’s happy. So its important to check across browsers when fixing bugs.

By the way, I’ve had several particularly buggy behaviours from display: table;, depending on where it fell in the layout, so its the third alternative I turn to. I hope this helps.

Articles are licenced under a Creative Commons Licence but copyright of images is retained by © Steven Clark 2007 - 2008

Leave a Reply

NOTE: fields marked with an asterisk * are required.





skip to top of page

Currently Reading

Information and Data Modelling (Second Edition) by David Benyon (Cover)With an eye toward implementing another web interface database solution from the ground up I'm casually revisiting David Benyon's Information and Data Modelling (Second Edition). Its critical to have a solid understanding of conceptual data modelling and knowing how to identify various things like fan traps and three way traps very early in the process. To that end, while its fine to have a basic understanding of third normal form and general ideas about relations (that which relational databases rely on), its also a great idea to spend time exploring the theory and case studies that lead to a higher understanding.

Often people I deal with just snuff their nose and say they can design a database - but often its a very naive approach. Having read this book about four years ago its time for a quick refresher over my holiday period. No, I doubt few will envy me.