skip to content rich footer

stevenclark.com.au

subscibe to the StevenClark.com.au rss feed

Contain Floats Without Adding Structure

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.

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! I hope this is helpful to those who obviously need a more elegant solution than that one. Why it works I have not the slightest idea.

One Response to “Contain Floats Without Adding Structure”

  1. steven clark

    [...] a very early suspicion I had) to the display: table used in the stylesheets on some containers. The three ways to get your floated elements to be properly contained are to [...]

Social Networking

Keep an eye out for me on Twitter

About the Author

Steven Clark Steven Clark - the stand up guy on this site

My name is Steven Clark (aka nortypig) and my passions are business, web development, photography and writing. I have an MBA (Specialisation) and a Bachelor of Computing from the University of Tasmania. I am working as a business management consultant.

Photography

My photography is at Steven Clark Studio and my regular photo blog presents an ongoing stream of latest images at Walk a Mile in my Shoes and I'm working on a long-term photography project called the King Island Project.

Recently Reviewed Books

Site Supporters

Hosted by Brett Drinkwater at Tashosting who is always there at the other end of my every inconvenient question and technical crisis. Brett's local community support for us over the last five years is greatly appreciated.

skip to top of page

Currently Reading

Ansel Adams: The Camera

As the first of three parts of Ansel Adams Photography Series, Ansel Adams: The Camera begins by discussing the idea of visualisation in relation to photography. Ansel Adams is a master of his craft; this series has sat on my backburner for some time. Book 2 in this series is The Negative and it's followed up by The Print. In them Ansel outlines his philosophy of photography rather than trying to lay down a set of rules. This first instalment is a technical book that explains the good old fashion film camera.