Design insights & tutorials.

Clear: Float

I like the irony in this… after my mention yesterday of pushing html around for almost a decade, I hit an obstacle laying out a footer in the new world of Table-free markup. I wrote and manipulated a pile of javascript and css to try and push a footer down beneath two “float:left” divs … if you’re familiar with the Float property, it has a strange way of causing its parent to stop inheriting its contents height and width … the purpose of float:left (or right) is to allow following (or previous in the case of right float) content to wrap around it (similar to when you align-left an image inside a paragraph). This is useful when you’re creating columns of content. The annoyance comes in when you’d like that “next” piece of content to stop wrapping up around the floated element. This is where the “clear float” css class comes in. After spending probably 2+ hours going around and around with trying to layout my footer, it was a simple css style, “clear:left” that did the trick I was looking for. I had seen the ‘clear’ property forever, but it never clicked for me. I haven’t been using the ‘float’ property very long either, so that may be why I was unfamiliar with its powerful counterpart … in essence the “clear” property breaks the chain of the float property to rather start its own new line.

About the Author, Wilson Revehl

Wilson Revehl is the co-founder and CTO of Go Media. He is a website & rich internet application designer & developer specializing in HTML, CSS, Javascript & PHP. Go Media is a Cleveland website design and development firm specializing in custom Wordpress websites, creative visual communications and resources for the graphic design community. Mr. Revehl runs the Fort Myers Technology Studio.
Discover More by Wilson Revehl

Discussion

We want to hear what you have to say. Do you agree? Do you have a better way to approach the topic? Let the community know by joining the discussion.

  • http://rlmansheim.com Rob

    Wow. I feel your pain. I new to the “float” property. Thanks for the tip.

  • http://rlmansheim.com Rob

    Wow. I feel your pain. I new to the “float” property. Thanks for the tip.

  • http://www.oastler.ca JamieO

    The following is a very useful css declaration / “cheat” that from discussions I’ve had with other designers is fairly common.

    .clear {
    clear:both;
    }

    So if you are writing content pieces and your particular div containers need to be used in float and un-floated situations, you can put a above and be assured that it will work. The same also ensures that if you have multiple floated elements inside a larger container that the background / border for the container will extend all the way around the floated contents.

    Note: Putting in theory is identical, but has been known to not have the same effect in Firefox and Safari.

  • http://www.oastler.ca JamieO

    The following is a very useful css declaration / “cheat” that from discussions I’ve had with other designers is fairly common.

    .clear {
    clear:both;
    }

    So if you are writing content pieces and your particular div containers need to be used in float and un-floated situations, you can put a above and be assured that it will work. The same also ensures that if you have multiple floated elements inside a larger container that the background / border for the container will extend all the way around the floated contents.

    Note: Putting in theory is identical, but has been known to not have the same effect in Firefox and Safari.

  • http://www.oastler.ca JamieO

    The second paragraph of text should have &ltdiv class=”clear”></div> in it. Forgot to cleanse my response of actual html that would be interpreted as css by this page.

  • http://www.oastler.ca JamieO

    The second paragraph of text should have &ltdiv class=”clear”></div> in it. Forgot to cleanse my response of actual html that would be interpreted as css by this page.

  • http://www.oastler.ca JamieO

    <div class=”clear”></div>

    Read what I meant please, not what I said? :P

    P.S. The Wordpress AJAX Edit Comments plugin (http://wordpress.org/extend/plugins/wp-ajax-edit-comments/) would have saved your readers the pain of reading my typo corrections as separate posts.

  • http://www.oastler.ca JamieO

    <div class=”clear”></div>

    Read what I meant please, not what I said? :P

    P.S. The Wordpress AJAX Edit Comments plugin (http://wordpress.org/extend/plugins/wp-ajax-edit-comments/) would have saved your readers the pain of reading my typo corrections as separate posts.

  • http://cssgirl.com Lindsey

    I wouldn’t classify it as a cheat exactly, more of a “trick”. I think cheat is too close to “hack” which most people want to stay away from.

    It is quite a commonly used trick that thankfully works 99% of the time (cross browser). It’s also good practice when you are using floats and you don’t want something to float to it’s right/left you clear the specific side, or clear both right in the css for that element.

    I think the stacking and overlapping of floats is a common issue that stumps many CSS beginners, and this simple thing can fix many of the problems.

  • http://cssgirl.com Lindsey

    I wouldn’t classify it as a cheat exactly, more of a “trick”. I think cheat is too close to “hack” which most people want to stay away from.

    It is quite a commonly used trick that thankfully works 99% of the time (cross browser). It’s also good practice when you are using floats and you don’t want something to float to it’s right/left you clear the specific side, or clear both right in the css for that element.

    I think the stacking and overlapping of floats is a common issue that stumps many CSS beginners, and this simple thing can fix many of the problems.

  • http://atrickdesign.com Pat

    There are instances where clear:both is not entirely sufficient (namely, with some older browsers).

    I’ve found this to be helpful when trying to ensure those floats work properly across platforms:

    .clear {
    height: 1px;
    line-height: 1px;
    font-size: 1px;
    margin-top: -1px;
    clear: both;
    }

    Hope that helps.

  • http://atrickdesign.com Pat

    There are instances where clear:both is not entirely sufficient (namely, with some older browsers).

    I’ve found this to be helpful when trying to ensure those floats work properly across platforms:

    .clear {
    height: 1px;
    line-height: 1px;
    font-size: 1px;
    margin-top: -1px;
    clear: both;
    }

    Hope that helps.

  • http://www.gomedia.us Wilson

    Thanks for the rewording Lindsey! Cheers to the “CSS Girl”!!

  • http://www.gomedia.us Wilson

    Thanks for the rewording Lindsey! Cheers to the “CSS Girl”!!

  • andre

    I rather like the “clearfix”

    http://www.positioniseverything.net/easyclearing.html

    you would copy/paste this into your CSS, and then add a “clearfix” class to any element containing a float like that.

    so I think it would go like this:

  • andre

    I rather like the “clearfix”

    http://www.positioniseverything.net/easyclearing.html

    you would copy/paste this into your CSS, and then add a “clearfix” class to any element containing a float like that.

    so I think it would go like this:

  • andre

    ah. I’m not sure how to post code. oh well, the site is pretty clear.

  • andre

    ah. I’m not sure how to post code. oh well, the site is pretty clear.

  • Nicole

    Another prime example of issues with floats is determining parent container sizing with backgrounds. A parent container’s background image will stop where ever the static content ends – ignoring the floated content height, which can cause your background image to be cut off or not aligned properly. The fix for this is:

    *html .parentContainer {height: 1%; display: block;}
    .parentContainer:after {
    clear:both;
    content:” “;
    display:block;
    height:0pt;
    visibility:hidden;
    }

    The first case with *html takes care of IE, and the :after selector will take care of most other browsers. This is a CSS alternative to adding in empty elements with “clear: both” styles.

  • Nicole

    Another prime example of issues with floats is determining parent container sizing with backgrounds. A parent container’s background image will stop where ever the static content ends – ignoring the floated content height, which can cause your background image to be cut off or not aligned properly. The fix for this is:

    *html .parentContainer {height: 1%; display: block;}
    .parentContainer:after {
    clear:both;
    content:” “;
    display:block;
    height:0pt;
    visibility:hidden;
    }

    The first case with *html takes care of IE, and the :after selector will take care of most other browsers. This is a CSS alternative to adding in empty elements with “clear: both” styles.

  • joel

    Yes, the float issue is a bugger!
    If you’re doing a double-float for a multi-column layout (float: right & float:left) and then slapping a footer beneath it, the best way to go about it is to put a div in there with
    “clear:both;” between the main content & footer. :)
    always tricky, for sure.

  • joel

    Yes, the float issue is a bugger!
    If you’re doing a double-float for a multi-column layout (float: right & float:left) and then slapping a footer beneath it, the best way to go about it is to put a div in there with
    “clear:both;” between the main content & footer. :)
    always tricky, for sure.

  • Duluoz

    You can also use overflow on the parent element to trigger correct inheritance. You then have no need for a clearing element.

    Example:

    #foo { overflow: auto }

  • Duluoz

    You can also use overflow on the parent element to trigger correct inheritance. You then have no need for a clearing element.

    Example:

    #foo { overflow: auto }

  • Jon Thomas

    Hi Chris. You’ll still find that you run into some problems down the road with floats. You’ll be tempted to put an empty div below floated items to clear them as a spacer in some instances as well. Read this article, it works in most cases. I’ve had a lot of success with it.

    http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/

    Also, if you haven’t used display:inline to your advantage, check out item 1 on this page:

    http://www.sitepoint.com/article/top-ten-css-tricks

    After typing this, I just saw Duluoz’s comment in which he summarizes the method overflow method. The first article link I gave describes that method.

  • Jon Thomas

    Hi Chris. You’ll still find that you run into some problems down the road with floats. You’ll be tempted to put an empty div below floated items to clear them as a spacer in some instances as well. Read this article, it works in most cases. I’ve had a lot of success with it.

    http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/

    Also, if you haven’t used display:inline to your advantage, check out item 1 on this page:

    http://www.sitepoint.com/article/top-ten-css-tricks

    After typing this, I just saw Duluoz’s comment in which he summarizes the method overflow method. The first article link I gave describes that method.

  • http://o3b.com Guillo

    i recently switch to CSS websites… and i have a little gadget i carry to every website:

    ——————-
    .clearfloat {
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
    }

    the fellows of adobe included that in the dreamweaver templates… I hate templates but i wanted to see how they were building their floats

    Regards

  • http://o3b.com Guillo

    i recently switch to CSS websites… and i have a little gadget i carry to every website:

    ——————-
    .clearfloat {
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
    }

    the fellows of adobe included that in the dreamweaver templates… I hate templates but i wanted to see how they were building their floats

    Regards

  • Heinnakix

    Hello! hgh enhancers

  • Heinnakix

    Hello! hgh enhancers

  • lynguyen

    There's got to be a more semantic approach to '<div style=”clear:both”></div>'. The div has no meaning once styles are stripped. The 'separator' tag would have been nice if XHTML2 didn't die on us.

  • lynguyen

    There's got to be a more semantic approach to '<div style=”clear:both”></div>'. The div has no meaning once styles are stripped. The 'separator' tag would have been nice if XHTML2 didn't die on us.

  • http://www.airwalk-design.com/ Web & Graphic Design

    The float style is common yet tricky, maybe they should invent a new method :p

  • Matt

    Thanks for this nice work!
    dizi izle

  • louis vuitton handbags

    Holding you in my arms and falling asleep just like holding Major J-Notturno. My breath is full of sapidity of yours. How deep your dream is as if your will never wake up again and it is called fate.
    http://www.gomediazine.com/

  • http://www.usa-basketball-shoes.com/Vince-Carter-shoes.html Vince Carter Shoes

    Here elaborates the matter not only extensively but also detailly .I support the write’s

    unique point.It is useful and benefit to your daily life.You can go those Wisdominthemind.com Elegance-is-Edgy-Dress.com

    sits to know more relate things.They are strongly recommended by friends.Personally

  • louis vuitton handbags

    I belive in fairy tales, the prince and the princess were living in the golden castle blissfully and guarding their love. Elfs in the world was watching us form the darkness, I standed in the rye, violent sneezed, golden poppled.exceptfor.net

  • louis vuitton handbags

    I belive in fairy tales, the prince and the princess were living in the golden castle blissfully and guarding their love. Elfs in the world was watching us form the darkness, I standed in the rye, violent sneezed, golden poppled.exceptfor.net

  • http://vibram4u.com/ moncler outlet store

    Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here <a href=” http://music-ofthesun.com/ ” >wslmart.net you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as xxxxxxxx that worth you to see. Believe me these websites won’t let you down.

  • http://vibram4u.com/ moncler outlet store

    Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here <a href=” http://music-ofthesun.com/ ” >wslmart.net you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as xxxxxxxx that worth you to see. Believe me these websites won’t let you down.

  • http://www.air-jordan-12.com/ air jordan 12

    Here elaborates the matter not only extensively but also detailly .I support the
    write's unique point.It is useful and benefit to your daily life.You can go those
    sits to know more relate things.They are strongly recommended by friends.Personally!
    http://kite8.net/

  • http://www.moncler-down-jackets.com moncler down jackets

    Here elaborates the matter not only extensively but also detailly .I support the write's unique point.It is useful and benefit to your daily life.You can go those sits to know more relate exceptfor.net things.They are strongly recommended by friends.Personally I feel quite well.

  • http://www.moncler-down-jackets.com moncler down jackets

    Here elaborates the matter not only extensively but also detailly .I support the write's unique point.It is useful and benefit to your daily life.You can go those sits to know more relate exceptfor.net things.They are strongly recommended by friends.Personally I feel quite well.

  • http://vibramshoesonline.com/ vibram fivefingers shoes

    I really like this type of Merchandise too, can you help me look at which one has higher price point? http://vibramshoesonline.com/

  • http://vibramshoesonline.com/ vibram fivefingers shoes

    I really like this type of Merchandise too, can you help me look at which one has higher price point? http://vibramshoesonline.com/

  • l1528557

    Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here <a href=” http://music-ofthesun.com/ ” >wslmart.net you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as xxxxxxxx that worth you to see. Believe me these websites won’t let you down.

  • gucci web

    You’ll still find that you run into some problems down the road with floats. You’ll be tempted to put an empty div below floated items to clear them as a spacer in some instances as well.

  • ladiesweb

    You can also use overflow on the parent element to trigger correct inheritance. You then have no need for a clearing element.

  • Xylude Xaalud

    I use this exclusively, works all the way down to IE6.

    .floatWrap {
    width:100%;
    overflow:auto; /* If you get scrollbars try setting this to hidden *
    }

    Now just wrap that around your floats and you are good to go!

  • http://www.atheistunderworld.com/ BenTegland

    Thanks for the help! This came in handy for me today!