22 May 2009 2 pm eastern

Floats, clears, and color flashes

After viewing this site’s redesign in progress, Peter Petrus wrote:

[Y]ou’re using footer to clear floats (content + sidebar). This creates a large drawback, because before footer is downloaded and displayed in the browser, parent wrapper lacks any background.

It means that we’re staring on black text / orange background combo for a few moments. Well, few moments now, but should you put any unresponsive widgets in the sidebar, everything can render much slower. Having main content flash like this isn’t very pleasant – especially when you named the redesign “Designing from the content out”.

Solution is very simple – use ‘:after’ clearing for the main wrapper. Sure, it won’t work on IE, but 1) setting width to wrapper sets hasLayout and triggers clearing 2) #footer is still there, in any case.

I’d read about using “:after” clearing but hadn’t implemented it and thought I could solve the problem an easier way. This afternoon, with about five minutes’ work, I did so.

I solved the orange background flash by moving the faux column background image up in the CSS hierarchy.

  • In the original CSS, I used the faux column background image on the wrapper element only.
  • In the updated CSS, I’ve now added the same background image to the body element as well.

Doing so fixed the orange background flash problem, because the browser no longer has to wait for the footer to clear before showing the background image.

Compare body element on new z.css and previous z-bak.css. (Likewise, compare body on alt.css and alt-bak.css.)

[tags]css, workarounds, clear, float[/tags]

Filed under: CSS, development, zeldman.com

37 Responses to “Floats, clears, and color flashes”

  1. Tim Wright said on

    I usually ditch my wrapper div and use body:after{content:”.”;display:block;clear:both;}

    Works like a charm!

  2. Lexx said on

    Thanks Jeffrey! :)

  3. Floats, clears, and color flashes said on

    [...] Jeffrey Zeldman Presents : Floats, clears, and color flashes Floats, clears, and color flashes [...]

  4. Adam said on

    This reminds me of the good ol’ days of CSS discovery, where there was always someone finding a new way to push the boundaries of what the language could do.

    Although this time, we’re polishing the tools in our bag of tricks instead of adding new ones. Thanks for the post.

  5. Travis said on

    Why not use overflow:auto; on the wrapping element? One line of css, less obtrusive than :after

  6. Derek said on

    One problem with assigning a fixed-width, hard-edged background image to the body element on a horizontally-centered design is that 50% of the time it will be 1 pixel off. Try slowly resizing your new site design to see it in action. The right-hand border gets thicker and thinner.

    A minor point, but for the more pixel-anal, it’s good to know.

    (By the way, some of us have email addresses with four-character domain extensions, which your validation does not allow!)

  7. Dinis Correia said on

    I’m a pixel freak – and there’s a tiny 1px issue on some browsers when setting a centered background image on the body tag.

    From “One pixel background jog”: On odd pixel widths where the number of pixels isn’t exactly divisible by 2 the browser has to round up or down to arrive at its measurement and IE seems to round the elements placed on the body at a different rate to those placed on other elements.”

    Apparently, this happens in Safari as well.

    In your case, the right side of the container sometimes gets a 2px black line – because you are also setting the bckground on the container and the two images overlap.

    I’d say go with the :after fix – it’s acutally a decent way to clear floats.

  8. Ben said on

    Thanks Jeffrey! I like this solution, simple and effective.

  9. Jeffrey Zeldman said on

    (By the way, some of us have email addresses with four-character domain extensions, which your validation does not allow!)

    @Derek: I thought we’d fixed that. Sorry about that.

    the right side of the container sometimes gets a 2px black line – because you are also setting the bckground on the container and the two images overlap. I’d say go with the :after fix – it’s acutally a decent way to clear floats.

    Fair enough, @Dinis.

  10. Jeffrey Zeldman said on

    Travis said:

    Why not use overflow:auto; on the wrapping element? One line of css, less obtrusive than :after

    Travis wins! Test 3, with overflow avoids pixel jog in IE (and sometimes Safari) while removing orange flash.

    Brilliant, Travis. And so simple. Thanks.

  11. John Lascurettes said on

    Consider me amused that you hadn’t used :after before. You are the master that taught me (both through early ALA articles and the first ed of DWWS). Here is my rundown on my favorite methods for clearing floats x-browser.

  12. Jeffrey Zeldman said on

    It’s my pleasure to amuse you, sir. :D

    Thanks for the resource!

  13. Tim said on

    Can someone elaborate on what, if any, is the advantage of using overflow:auto instead of overflow:hidden ? I have always used hidden for clearing floats, or the :after method. If I remember correctly, overflow:auto can trigger scrollbars in FF1 (do we care? probably not much.), but I haven’t run into any bugs with using the overflow:hidden method.

  14. Cory Duncan said on

    @Tim Depending on the layout, overflow: hidden can clip text characters (particularly serif fonts).

  15. Michael said on

    Oh, heaven forbid, a page isn’t completely loaded until it is loaded. Who knows what will happen next? Video that isn’t buffered not playing? We’re all damned to 56k!

  16. Neal G said on

    The :after method is a pretty old, and outdated. I can’t believe people even recommend using it anymore. Anyway, props to Travis for pointing out overflow: auto, which is what I swear by now days.

    Jeffrey, another way to fix the 1px jog, (other than making your site an even numbered width, which in the end will save you grief) is to give your odd numbered DIV a 1px transparent border on the left or right like so: div { border-left: 1px solid transparent; } This won’t work in IE6 as it doesn’t support transparent but I have a feeling you’re stats for this site have IE6 around 3-5%

  17. Peter said on

    Using ‘overflow’ to clear floats is a clever way too, but it breaks text selection and creates problems in Firefox (especially ‘auto’).

    Thanks for addressing this issue, Jeffrey, looking forward to reading your articles in the new design. :)

    Oh, and sorry for my bad English.

  18. Gregarius » Jeffrey Zeldman Presents The Daily Report said on

    [...] Floats, clears, and color flashes [...]

  19. Matt said on

    There’s another, really ingenious layout technique for this that’s being pioneered at sites like Google and Flickr: they’re taking advantage of the table element’s native support for displaying content in columns. At first it’s a bit counter-intuitive to use a giant table to lay out your entire document, but the markup is fairly easy to read once you get your head around it, and it turns out that the rendering is very consistent across browsers, including the dreaded Safari.

    Another advantage of this new technique is that your layout can degrade much more gracefully when you have content that’s too wide for the new format (usually a large image or a particularly long URL). Since the visual elements are being applied directly to each column, background images and the like will actually grow with the content. This behavior is really pretty robust, and as far as I’ve seen, these sites are virtually free of the unsightly content overlaps and cutoffs that plague so many float-based layouts.

    Most importantly, the corresponding CSS is much more accessible to users with screen readers. Semantically, the concept of a floating element was introduced to describe optional, supporting information that appears alongside the main content. But most sites incorrectly use floating elements for every area of the page, which means that the screen reader doesn’t know which parts to emphasize and which parts it can safely skip past. In contrast, a table-based solution allows the screen reader to clearly communicate both the content and the layout of your site to your disabled audience.

  20. Ingo Chao said on

    overflow may contain floats effectively, but it introduces some errors. There is no easy floating without knowing all that clearing methods.

  21. Your page is now on StumbleUpon! said on

    [...] Your page is on StumbleUpon [...]

  22. Michael Kozakewich said on

    Neal G:”…but I have a feeling you’re stats for this site have IE6 around 3-5%”
    3-5% is far too high a number. I’m sure Chrome, Opera, and All-Other-Browsers each take up less than 10% of his visitors.

    I don’t seem to be well-versed in floats; I thought the :after trick was the newest clearing method.
    Is there any specific reason not to use that?
    Also, is there a specific resource somewhere which lays out every clearing method, with the pros and cons of each? It sounds like there are a tremendous number.

  23. Jeff said on

    Correct me if I’m wrong, after doesn’t work on IE 6 ?
    That would be the only drawback; whether we have reach reach the point where we can ignore ie 6 is another talk though…

  24. Olly said on

    @Jeff – You can trigger hasLayout in IE6, which has a similar effect. The easiest way I’ve found is to set a dimension on the box, e.g. height: 1%;

  25. AlanW said on

    @Jeffrey I’d love it if you’d weigh in on Matt’s comment regarding using tables for layout and the semantics of floats.

  26. Jeffrey Zeldman said on

    @AlanW: I thought it was funny. And clever. And it’s true that we’re making do with float, because CSS initially offered no means of designing multiple columns. In other words, float is a hack like table layouts were a hack. But I’ll take semantic markup and floating divs over fat pages of non-semantic cruft (i.e. table layouts) any day. As to the notion that table layouts are more accessible, poppycock. CSS layout offers a control of source order that is infinitely friendlier to users of tools like JAWS; and navigating via H2 and H3 headlines is the same to the JAWS user whether the page was laid out with DIV or TD.

  27. Alan Bristow said on

    I recently had flashing problems on a site. It was Textpatten based and my problem was mainly my reliance on db reads for the CSS when it can be made static via a simple plugin. Results were instant and the cure was complete. Plugin was, rvm_css (it’s in the Googles).

    Gonna look at :after too tho’.

  28. Evan said on

    I solve a lot of this using display: inline-block and margins instead of floats — I find it leaves me with cleaner CSS (mostly) and I don’t have to float everything, which I’ve always felt makes a design “fragile” and prone to breakage (and also as much a misappropriation of the functionality of floats as table-based layouts are of tables).

    It does, however, leave Firefox 2 users completely out in the cold.

  29. Adam Messinger said on

    @Alan Bristow: With Textpattern, getting CSS out of the db and onto the filesystem is an essential performance tweak. The db-based CSS only really exists as a legacy feature at this point, and using it isn’t recommended. The rvm_css plugin looks like an interesting approach, and I’ll have to give it a closer look. Thanks for the tip.

    As for float clearing, I tend to use overflow: hidden unless it causes clipping problems. Otherwise, floating the container also works. Neither is particularly elegant, but both are broadly compatible.

  30. Robert said on

    @Zeldman: Why not apply display: table, display: table-row, and display: table-cell for browsers that can handle it by using, say :nth-child? I know floats work, even if they are a table-for-presentation-like hack, and it would add more code to your CSS file to do this. It would, however, allow you to get around a lot of the other hackish techniques for dealing with faux-columns. Serve browsers that don’t do display: table, etc, their equivalent without the extra hacks, a la Progressive Enhancement.

  31. Amadeus said on

    I have to give +1 to :after for clearing floats, although with some small changes.

    1) content:’.’ should change to content:”. Why do we need the period in the first place? It has worked on every browser I have tested on.

    2) To trigger hasLayout in IE, I just use zoom:1; Sure it’s not valid CSS, but at least it’s competely inobtrusive. Furthermore, I would probably just place that in a IE specific stylesheet, which is full of tweaks anyways for IE. I don’t like to muck up standards based browsers with IE fixins.

  32. Quick scan of the net - wrapper counter « The Seventies said on

    [...] http://www.zeldman.com/2009/05/22/floats-clears-and-color-flashes/Solution is very simple – use ‘:after’ clearing for the main wrapper. Sure, it won’t work on IE, but 1) setting width to wrapper sets hasLayout and triggers clearing 2) #footer is still there, in any case. …. At first it’s a bit counter- intuitive to use a giant table to lay out your entire document, but the markup is fairly easy to read once you get your head around it, and it turns out that the rendering is very consistent across browsers, including the dreaded Safari. … [...]

  33. Amazon.com: Designing with Web Standards (2nd Edition): Jeffrey Zeldman: Books said on

    [...] that we’re staring on black text / orange background combo for … This is syndicated from Jeffrey Zeldman Presents The Daily Report. [...]

  34. Web Design References: Cascading Style Sheets said on

    [...] Floats, Clears, and Color Flashes – Jeffery Zeldman New! [...]

  35. Redesigned – Jeffrey Zeldman Presents The Daily Report said on

    [...] more about it here, here, there, and elsewhere. (Don’t freak out; these old posts are now in the new layout, adding a [...]

  36. Paul McClean said on

    Hi guys,
    Have been searching for a solution to this exact ‘flickering’ wrapper problem when the wrapper hasn’t got the height from it’s containing elements. The overflow: auto; solution worked perfectly. Thanks so much for posting. The design community thrives because of helpful stuff like this.

    All the best,
    Paul

Comments off.