Understanding CSS Floats

Using css floats effectively can be confusing and it’s probably one of the things that trips up most people when they’re first learning css. However once you learn to control floated elements it opens up a whole new world of possibilities in your design and makes developing layouts much simpler.

And best of all floats really aren’t that hard to work with once you understand a few key points.

What is a Float?

A float is a box that is shifted to the left or right on the current line. The most interesting characteristic of a float (or “floated” or “floating” box) is that content may flow along its side (or be prohibited from doing so by the ‘clear’ property).
w3.org

The float property has 3 values; none, which is the default, left, and right. As much as you might wish it so there is no center value. Horizontal centering in css is accomplished through margins.

Floats sit as far left or as far right as possible within the immediate containing div. Depending upon the available width where a float is declared, other elements can sit next to a floated element or flow around it.

The Difference Between Floats and Positioning

In css there are 3 types of positioning schemes. From w3.org

  1. Normal flow – In CSS 2.1, normal flow includes block formatting of block boxes, inline formatting of inline boxes, relative positioning of block or inline boxes, and positioning of run-in boxes.
  2. Floats – In the float model, a box is first laid out according to the normal flow, then taken out of the flow and shifted to the left or right as far as possible. Content may flow along the side of a float.
  3. Absolute positioning – In the absolute positioning model, a box is removed from the normal flow entirely (it has no impact on later siblings) and assigned a position with respect to a containing block.

Relative positioning shifts elements relative to their position in the normal document flow. Other elements are not affected and lay where they would had no positioning been applied. This leads to the possibility that elements with relative positioning may overlap surrounding elements on the page.

Note that floats are taken out of the normal document flow, but floats do affect the position of elements around them.

How to Effectively Use Floats

There are two main ways floats are used.

Text Flowing Around An Image

Images are the most common, but you can substitute a different element. The idea is that text will wrap around some element.

wrapping-float.png

This was the original intention of the floated element. You float an element left or right and allow other elements, usually text to flow or wrap around the element. When using floats in this fashion you want to have both the floated element and the wrapping text inside the same containing element.

{code type=html}


Lots of text here. Enough to wrap around the image.

{/code}

{code type=css}
img.alignleft {float: left; margin: 0 10px 10px 0}
{/code}

In the above code both the floated element and the wrapping text are contained within the same paragraph.

Notice that in the css I’ve added a right and bottom margin. It’s good practice when floating an image to add a little space between it and the wrapping text. Left floated images get a right margin and right floated images get a left margin. Both have a margin added to the bottom. 10px is arbitrary and you could use padding instead of margins if you choose.

Column Layouts

Because floats allow two block level elements (such as divs) to sit up against each other, it’s easy to use them to create columns.

{code type=html}

{/code}

{code type=css}
#left-column {float: left}
#right-column {float: left}
{/code}

Float both column divs above to the left and you have two columns. You would also need to make sure the combined widths of the two column divs can fit inside the width of the wrapper div. Alternately you can float the right column to the right, but I usually prefer floating both in the same direction.

Add a header div above the two columns and a footer (with the clear property applied) below and you have a nice and simple 2 column css layout.

2-column-layout.png

One last tip when floating an element. Always set a width on the floated element. Not setting one could have unintended consequences.

Clearing Floats

Often with floats you’ll want to ensure an element that follows one or more floated elements sits below those floated elements. The solution is the css clear property.

Clear takes the values left, right, both, none (default), and inherit. In practice you’ll only use the first 3 and most of the time you’ll use clear: both.

Perhaps the most common use is to clear your footer div so it sits below your 2 or 3 floated columns.

cleared.png

not-cleared.png

Containing Floats

The first thing to consider when containing floats is to make sure the containing element is wide enough to hold the floated elements you want to sit next to each other. If you float two elements left in order to create a two column layout and the combined with of the floats is greater than the width of the containing element, one of the floated elements will drop to the next line.

float-drop-left.png

float-drop-right.png

I mentioned above that floated elements are taken out of the normal document flow. This leads to an interesting and confusing issue when all the elements inside a containing element are floated.

A common place you might see this is when you have a header div and the only elements inside is a logo image which you float left. You also apply a css background-image to the header, but it doesn’t show up.

The issue is that the header div contains the single floated image and and because that image is no longer in the normal document flow, the header div considers itself to be empty and has no height. The containing div has essentially collapsed on itself. There are a few ways to deal with this problem.

You could explicitly set a height on the container div. This would work fine in the header example above, but may not be feasible in all cases of collapsed containers. Sometimes you’ll want to add an empty div that you clear below the floated elements.

{code type=html}

{/code}

This works well if it’s ok to have a little extra space below the floated elements. Some browsers may apply a default height, margin, padding, etc. to the empty div.

The last method is to use the overflow property on the containing element. You’ll generally use overflow: hidden, but depending on your situation you may prefer to use overflow: auto or use overflow-y instead of using it in both directions. Overflow prevents the containing element from collapsing.

Issues With Floats

In addition to collapsing containers there are a few issues with floats to be aware of.

Older versions of IE have a double margin bug. If you float an element in one direction and also add a margin on the element in the same direction (left/left or right/right), IE doubles the margin.

A simple solution is to set display: inline on the floated element. Alternatively you can use conditional comments to set a different margin for IE or for a specific version of IE.

Another common issue is adding elements inside a floated element whose width is wider than the width of the floated element. You might for example add an image inside a floated div and the image is wider than the div itself.

Depending on the browser this could lead to your floated element being wider than you intended or it could lead to content overlapping your image. Make sure elements inside a floated element aren’t wider than the floated element.

One last issue to look out for is that of the disappearing bottom margin. The bottom margin of a floated element inside another floated elements might be ignored. The simple solution is to use padding instead of margin when that happens.

Summary

When people first learn css they tend to gravitate toward absolute and relative positioning for layouts, since both seem simpler on the surface. It’s generally better practice to use floats and if you remember a few key points you should be able to understand and to control floated elements in your layout.

  • Floats are laid out in normal document flow and then shifted as far left or right within containing elements and removed from normal document flow.
  • Elements can flow around floated elements as long as there is enough horizontal space to contain them. If there’s not enough space the elements will display below the floated element
  • Floats and positioned elements are different and behave in different ways. You can’t apply both to the same element.
  • Use floats when you want text to flow around an element or to create columns in a layout
  • Remember to clear elements that you want to fall below floated elements and to keep containing element from collapsing
  • Find solutions you’re comfortable with to a few common float issues in Internet Explorer

I hope the above helps clear up some of the confusion you might have using css floats. If you have more questions about them please ask and I’ll do my best to answer. Here are a few other good posts about floats that you may find helpful.

« »

Download a free sample from my book, Design Fundamentals.

20 comments

  1. Great post!
    Question beside to the section “Containing Floats”. Isn’t the seconds image wrong? Shouldn’t there be another text in the lower float saying: “Sidebar floated right and too wide…” ?

  2. Best tutorial about floats ever ….THANKS …. and I have been reading them all 🙁 🙁 🙁
    What I like, as in your Positionning explanations, it is the clarity, the absence of fat, the images you use (both physical and verbal) ….
    Only thing missing, but it is always the case, is how do I come from my awesome and sophisticated, circonvoluted layout in Photoshop, to the same design in HTML/CSS ???
    In the old times it was a piece of cake with the tables ….
    And nowdays the only solution as convenient is the use of absolutely positionned elements, then centered in the browser thanks to a relatively positionned container … Which of course gives an overdose of in the code and a non-semantic mark-up….
    But nowhere did I found a tutorial or explanation on how to convert the Photoshop layout in a float composition …. Only a 2 or 3 columns solution …
    Can you maybe point towards it …?

    • Thanks for the kind words Pavitra. Floats confused me when I first started learning CSS and I tried to remember that confusion when I was writing this post. I guess it worked to help make things clearer for others.

      It’s funny you mention going from HTML to CSS. I’m working on a post for another site on just that topic. I’ll post something here when that post is published. It’s still a couple weeks away.

      Eventually I want to write some posts here that will be similar and serves as case studies for moving a design from a PSD file to a developed website.

  3. Helpful. But I’ve found another issue (observed so far in FF, Chrome and Safari):

    When a floated element follows a heading style, the bottom margin on the heading and the top margin on the following block element are collapsed.

    E.g., a heading with a bottom margin of 2px, followed by a paragraph floated to the right, then a paragraph without float. Any bottom margin on the heading or top margin on the following paragraph will be ignored, as will any bottom or top padding.

    The problem cannot be addressed by increasing the padding on the heading: The padding area simply overlaps the top of the following paragraph.

    Have not yet tested this in IE.

    • Strange. Do you mind sending me a link to an example or posting one here? I’ll be happy to take a look and see if I can figure out what’s going on.

      I would have thought setting the padding would work, though it sounds like it’s not.

  4. “moving a design from a PSD file to a developed website”

    Regarding your response to Pavitra. Have you written about this topic as yet?

  5. A very good post. Thanks, it helped me to create a div with three floats and I used your tips to keep it looking good even when the window is resized.

  6. Hi,

    Your site is really helpful for me.

    But i have small suggestion for the site.

    Can u pls insert search option for the site because easily find required topics .

    Any thing wrong i am saying pls for give me.

    Thanks

    Gobinath M

Leave a Reply

Your email address will not be published. Required fields are marked *