Stop Using Float for Layouts!

This is an example of how to properly use inline-block in place of the old float property.

by Cerebrl

HTML

<!--[if lte IE 7 ]><style>

    // This forces IE7 and IE6 to follow suit
    
    exampleList {
        display: inline; // creates an inline layout
        zoom: 1; // triggers hasLayout for block treatment
    }

</style><![endif]--> 
<section class="wrapper">
    <hgroup>
        <h1>Stop Using <code>Float</code> for Layouts!</h1>
        <h2>This is a Public Service Announcement</h2>
    </hgroup>
    <p>It's 2011, and all major browsers properly follow <code>display: inline-block</code> the way it was designed (for browsers that don't, like IE 6 &amp; 7, see the HTML comment above). <code>float</code> was originally designed to "float" elements within text and have the content "flow around" the element. It was NOT meant for designing layouts!</p>
    
    <p>We used it long ago because it was all we had. That is no longer the case, so stop using it, unless it the layout demands something that inline-block cannot provide.</p>
    
    <p>Below is an example of how to use <code>inline-block</code> correctly:</p>
    
    <ul class="exampleList">
        <li>List Item One</li>
        <li>List Item Two</li>
        <li>List Item Three</li>
    </ul>
    
    <p>If you don't want whitespace between elements? <strong>Don't hack the stylesheet!</strong> Don't worry you have a couple of options. Option One: Just don't place any whitespace between the elements.</p>
    
    <ul class="exampleList">
        <li>List Item One</li><li>List Item Two</li><li>List Item Three</li>
    </ul>
    
    <p>Or, Option Two: Comment out the whitespace. This is fine for slamming together a couple of sections of a website, but less optimal for a dynamic list of items.</p>
    
    <ul class="exampleList">
        <li>List Item One</li><!--
     --><li>List Item Two</li><!--
     --><li>List Item Three</li>
    </ul>
    
    <p>Or, Option Three: Fancy HTML style. I'm not a huge fan of this, but it works none-the-less.</p>
    
    <ul class="exampleList">
        <li>
           ...

CSS

/* Pertinent CSS */

.exampleList {
    text-align: center; /* Not necessary: allows centering of the group. Left or right works too. */
}

.exampleList li {
    display: inline-block; /* Necessary: triggers line elements */
    width: 30%; /* Necessary: for most situations */
    vertical-align: top; /* Not necessary: provides controls how the blocks line up vertically. Bottom, middle, baseline are also options. */
}

/* Just for style */

.wrapper {
    max-width: 30em;
    min-width: 20em;
    margin: 0 auto;
    padding: 0 2em;
    border-left: 1px solid lightgray;
    border-right: 1px solid lightgray;
}
h1 {
    font: 900 2.2em/1.5 Georgia, Times, serif;
    letter-spacing: -0.05em;
}
h2 {
    font: 900 1.2em/1 sans-serif;
    letter-spacing: -0.05em;
    text-transform: uppercase;
    color: #555;
}
p {
    font: 100 1em/1.2 Georgia, Times, serif;
    margin: 1.2em 0;
}
code {
    font-size: 1.2em;
    color: darkorange;
}
strong {
    font-weight: 700;
}
.exampleList li {    
    height: 40px;
    padding: 18px 0 0;
    background: lightgray;
    border-radius: 5px;
    text-align: center;
}
.exampleList li:hover {
    background: darkgray;
    color: white;
}
.note p {
    padding: 1em 0 0;
    border-top: 1px dashed lightgray;
    font: 400 1em sans-serif;
    color: darkred;
}