Clearfix CSS Hack: Solving Stair Stepping Images

While I deal with this and other design issues I had with my site in the article WordPress Tips and Tricks - Template Files, Styles, and Themes, the issue keeps coming up. So I decided we needed to address it specifically.

What to do when images start shoving other images round?

Generally, we try to avoid browser hacks, but there was one in particular we couldn't avoid. It causes floats within floats, especially those with images floated within floating containers, to fail to recognize the height of the floated image within the container and stair step or wrap itself around neighboring containers. Does that make sense?

Let's spell it out.

One of the biggest problems I had is with floats within floats. A division with a float inside scrambles your layout in FireFox, though it looks great in MSIE. The inside floats don't line up and when they reach the virtual "end" of the parent container, they overlap past the end and into the next container. The problem is that the float doesn't "clear" or stop at the end of the container where it should. It needs to be told when to stop with the "clear" function, but unfortunately different browsers need different instructions to accommodate those instructions.

Example of the step stepping of image without using the clearfix hack

After playing around with this for days, here is my final fix from Positioniseverything's Easy Clearing.

For example, on the front page of our site, each post excerpt features a bar along the left side in green and often an image in the right side of the container with the text wrapping around the image. The image's position is controlled with a float selector. In Firefox, since a height was not established in the parent container, it ended when the text ended. If the text went below the image, then it wasn't a problem. But if it ended before the end of the image, the next container would then begin, stair-stepping into the container above it.

I needed to apply a "clearing" to the container so that the container below it would wait until the container above was finished, and then begin it's positioning.

The clearfix style is as follows:

.clearfix:after {content: ".";
     display: block; 
     height: 0; 
     clear: both; 
     visibility: hidden;}
.clearfix {display: inline-table;}
/* Hides from IE-mac \*/
* html .clearfix {height: 1%;}
/* End hide from IE-mac */
/* End clearfix */

In the index.php template where the excerpt or post content is displayed, I used this:

<div class="excerpt-post clearfix">
<h2 id="post-">......

The main styles of the excerpt area are controlled by excerpt-post but the addition of the clearfix style adds style instructions to the first style. You can learn more about using combination styles in our article on Understanding CSS Selectors and Attributes.

Example of the step stepping of image without using the clearfix hack

I also used this same technique in our Book Recommendations and Reviews. The ads and review section containers would begin to stair step their way as each container bumped up against the other, unable to determine the previous container's height. I simply added the clearfix style to their styles.

<div class="clearfix books">
<img scr="blah.jpg" title="Amazon books" class="bookads" height="260"  width="120">
<img scr="blah2.jpg" title="Amazon books" class="bookads" height="260" width="120">
<img scr="blah3.jpg" title="Amazon books" class="bookads" height="260" width="120">
</div>

The same technique would apply if the layout was like this:

<div class="clearfix books">Text here.
<div class="bookads">Book Ad here</div>
Text here.</div>

A lot of people blame Microsoft Internet Explorer for having the most bugs, but I did find some major bugs in other browsers.

random images, photography by Lorelle and Brent VanFossen - copyrighted

Authors, photographers, teachers, and public speakers, Lorelle and Brent VanFossen, travel extensively with their camera and pen in hand to bring you a variety of articles on nature and travel photography including basic nature photography and the photography business, writing, travel, recreational vehicles, web page design, and life on the road. All images, design, and content are copyrighted and protected by law.

You can find related articles to this topic in our Web Pages and CSS Tips and Tricks and Photographycategories. The previous post is Environment Cleanup: Manufacturing Plants Eliminating Garbage and the next post is Photographer’s Rights. Clearfix CSS Hack: Solving Stair Stepping Images, Issue Number 790, by Lorelle VanFossen, was updated January 31st, 2006.

You can follow comments through the RSS 2.0 feed and you can find more feeds on our Feeds List. Your comment is welcome, as are trackbacks from your site.

Related Articles

8 comments from people who had something to say

RSS feed for comments on this post. TrackBack URI

People are talking here

  1. Rasmus Østergaard Pedersen said:
    June 22, 2006

    I quote you: “The problem is that the float doesn’t “clear” or stop at the end of the container where it should.”

    This is wrong because it actually is the intended behavior.
    Ref. http://www.w3.org/TR/REC-CSS2/visuren.html#floats

  2. Lorelle VanFossen said:
    June 22, 2006

    It’s not “wrong” as I used the word “should”. The behavior of the floats is right, but the result isn’t the one expected. The technique worked without the hack in one browser but not in Firefox, thus this hack helped force the “expected behavior” to work within the floats and DIV.

    If there is a solution to make this work without hacks, please let me know as there are many who struggle with this in CSS and all the various browsers.

  3. Michiel said:
    September 6, 2006

    You can change the line
    .clearfix {display: inline-table;}
    into
    .clearfix {display: inline-block;}
    to fix new problems with ie7.

    Regards,
    Michiel

  4. Lorelle VanFossen said:
    September 6, 2006

    Thanks, Michiel, but how it that backwards compatible, especially since I think it will be a long time before a lot of people will be using IE7, especially those living outside of the US. And how does inline-block work with non-IE browsers?

  5. Cristian George Strat said:
    November 24, 2006

    “especially those living outside of the US”

    I’m sorry but I fail to understand this.
    Are there any statistics about IE7 early adoption outside US vs inside US?

  6. Lorelle VanFossen said:
    November 25, 2006

    Yes, there are extensive statistics about adoption of all “current” versions of all Internet programs, software, and operating systems. As for IE7, the info is just coming in since it is so new. In a few months, the statistics will be available.

    There are still a HUGE amount of international users using Windows98 and WindowsME, as well as 800×600 screens. Many non-US users are not eagerly embracing anything Microsoft, and many also do not have legal versions of Windows, which I believe may make upgrading to IE7 a bit…cumbersome.

    Search for Internet statistics or international Internet statistics and you should find a ton of research. Forrester, Netcraft, and Iconocast are good examples of such research and surveys, to help you get started.

  7. Anonymous said:
    January 24, 2007

    “Are there any statistics about IE7 early adoption outside US vs inside US?”

    no

  8. Roberto said:
    February 9, 2010

    I don’t agree. The standard is pretty clear, it should not behave that way, it is a bug in IE, not Firefox. Please read it, there is a diagram that explains how to properly do what you want and the solution is much easier than your hack:
    http://www.w3.org/TR/CSS21/visuren.html#floats

Have Something to Add?

All comments are moderated, so play nice.