How I learned to stop floating and love the inline-block
Posted by Angelo Simeoni
Jul 02
Floating columns is a typical approach to defining page layout. The obvious issue with floating columns is clearing said floats. When you start dealing with the details of auto-clearing elements and browser inconsistencies, you might start to desire an alternate approach.
Clearing issues & browser inconsistencies aside, floating is not the natural way to define layout. Floating is intended to be used to allow inline elements to wrap around floated elements.
When floats are used for layout, things can break if the contents of one column is greater than that of another.
Inline-block to the rescue. Inline-block is another display type that offers several advantages to floating layouts.
- simple, maintainable grid-based system of layout
- vertical-align columns (top, bottom & middle)
- no clearing floats
However, inline-block has its own set of quirks.
- markup is whitespace sensitive
- display of child elements in firefox 2 can be tricky

That being said, here’s how to define inline-block for understanding browsers.
1 2 3 |
.inline-block {
display: inline-block;
}
|
1 2 3 4 5 6 |
<!--[if IE]>
.inline-block {
zoom: 1;
display: inline;
}
<![endif]-->
|
zoom: 1; causes a state proprietary to IE called hasLayout to have a value of true. hasLayout is a shallow, fickle beast, but suffice to say that the preceding two rules will cause the affected boxes to behave as inline blocks in IE6 & IE7. Why? It kind of just does. There are reasons why, but it’s much easier just to accept this one.
Caveat: Inline elements are white-space sensitive. To avoid this issue in IE, make this:1 2 3 4 5 6 |
<div class="inline-block"> <p>some text</p> </div> <div class="inline-block"> <p>some text</p> </div> |
1 2 3 4 5 |
<div class="inline-block"> <p>some text</p> </div><div class="inline-block"> <p>some text</p> </div> |
1 2 3 4 |
.inline-block {
display: -moz-inline-box;
-moz-box-orient: vertical;
}
|
(-moz-box-orient determines the orientation of children elements.)
Final CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
.inline-block {
display: inline-block;
display: -moz-inline-box;
-moz-box-orient: vertical;
vertical-align: top;
}
<!--[if IE]>
.inline-block {
zoom: 1;
display: inline;
}
<![endif]-->
|
vertical-align defines how the boxes line up – either at the top, middle, or bottom of the tallest inline-block box. Inline-block example.

This technique works in IE6, IE7, Firefox 2, Firefox 3, and Safari 3.
Comments on this post
Jul 02
Oleg said,
So now there’s IE and even Mozilla specific css parameters (and more of them), white space sensitive markup (I guess you can force HAML spew out html in a single line somehow).
Clearing floats is not that difficult. Unless you really want to be able to vertically align stuff, inline blocks seem way too clunky.
How do Safari, Opera, KHTML browsers feel about it anyway?
Jul 02
Will said,
Wouldn’t you want to target IE less than or equal to 7? This way if IE8 supports inline-block and breaks hasLayout you don’t have to fix it later.
Jul 02
rubylicious said,
Interesting.. I’ve had the exact same problems with floats that mess up. Too bad with all the workaround to get it to work with all browsers..
Jul 02
Chris Lloyd said,
I find that using floats and clearing them with
overflow: autois very easy and works in every browser (except for IE5 and an old version of Opera, I think, don’t quote me on that). It doesn’t have all the extra CSS markup with is required with inline-block and is an extremely small and easy hack.Jul 03
Damien said,
Works nicely in Opera 9.5. Don’t have an older versio nanymore so can’t say for Opera 9.
Jul 03
Eric Anderson said,
Great article. I’ve always wanted to use inline-block but resisted because of browser compatibility. Like others I feel there are too many “workarounds” here to use on a regular basis. Too difficult for others to understand what is going on. But I imagine there are a few circumstances where this will be just the ticket to do what I need it to do.
Jul 03
Angelo Simeoni said,
@Will, you’re right – the conditional comment should target ie7 and below.
I will be making a follow-up post to show how inline-block can be especially useful.
Jul 03
Clayton said,
Seems like the whitespace issue would trip me up and cause a lot of tearing-out-my-hair type problems. But, once I got used to it, probably not so bad.
I’m going to give this a shot with an existing float layout I’ve got and see how it works. Thanks!
Jul 05
Ryan McGeary said,
I think the best part of this discussion is understanding how inline-block can be properly simulated across browsers. I definitely learned something, but the whitespace dilemma makes it a no-go for me. It smells too brittle. I feel it would cause unneeded stress when the source is in a different format from the output (haml, markdown, textile, helper methods, etc.). However, I agree that floated layouts are far from an elegant solution.
Jul 06
Ken Collins said,
Nice to see you guys talking about XHTML/CSS. It is good to have an article like this that is akin to ALA. Great topic too!
Sep 18
Jason Robb said,
Very intriguing, I’ll be sure to give this a shot some day soon. Thanks, Angelo!
Sorry, comments are closed for this article.
© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots
Come “ride the toad” on Hoptoad, the app error app.
Thunder Thimble: Brand monitoring for social media.
Widgetfinger: Simple content management for simple websites.
Tee-Bot, funny shirts your friends won't understand!
Umbrella Today: “It’s like totally the simplest weather report ever, Julie.”
Thoughtbot
thoughtbot is a technology consulting firm that provides web application development and design services. We focus on building modern systems, embracing good ideas and delivering elegant solutions.
Interested in learning Rails?
Sign up for our beginning or advanced training.
Archives