This container lacks the fix.
See how this float no longer protrudes out of the containing box, with no extra clearing element used in the container!
This float container has a class attribute of "clearfix", which applies the :after fix, or the Holly hack, depending on the browser.
IE/Mac Strikes Back
All this is wonderful, but unfortunately IE for the Mac does not "auto-clear" floats, and also does not support
:after, and so is left out of the clearing party. What's to be done?
You might callously abandon IE/Mac, but consider that many people who use older Macs can't run Safari, or several other modern browsers. Thankfully this browser has been dropped by Microsoft, and at some future time the numbers of such IE/Mac users will become miniscule. Remember that even if a float appears to stick out of a container, no content will actually be obscured. It just won't look as pretty for those few viewers, that's all. Each author will have to decide on this issue according to their specific needs.
This page once described a Javascript method to force compliance in IE/Mac, but now thanks to
Mark Hadley and
Matt Keogh it's now possible to dispense with that ugly Javascript and go with a straight CSS fix. Woohoo!
Taming the IE/Mac Float Problem
Basically the fix is just a matter of applying a
display: inline-block; to the
.clearfix class, and hiding that property from all other browsers. That's it! We can easily do this with our existing code, slightly modified.
The
.clearfix {display: inline-block;} is seen by all browsers, and fixes IE/Mac. Then, inside the rule set that is hidden from IE/Mac, the display is reset to
block. That's all she wrote! Simply stick the above code into your CSS, and use
.clearfix on any box that has to contain a sizable float. Ain't that cool? Just watch out for previous external floats triggering the
IE Float Model, as mentioned earlier.
Kudos to
Alex Robinson for finding that inline-block is superior to the older inline-table fix for IE/Mac.
A Word Of Warning (this is important!)
The W3C float specification requires that a cleared element shall stay below
all previous floats. There are no exceptions to this requirement! "Previous" in this case means any float that comes earlier in the source document.
Up until November of 2004, Firefox was still incorrectly clearing only the floats that were vertically above the clearing element, rather than all previous floats. This meant that in those earlier Gecko browsers you could place a floated column down one side of the screen, and inside another column (possibly another floated column) you could clear a smaller interior float,
without that cleared element dropping below the previous floated column. Since only Gecko had this problem, it was obvious that something was wrong every time this happened to a page. Normally Gecko is the good browser, but in this one case it was the culprit. See, IE is not
always the bad guy!
However, this easy clearing method has muddled the issue quite a bit, since now Explorer is not actually being cleared at all, while Gecko browsers have finally been corrected so they do clear all previous floats.
...
Oh no! Do you see what will now happen in our hypothetical float page? IE, seeing no real clearing elements, will look great. Meanwhile, in newer Gecko browsers and Opera 7, the CSS generated clearing element in the first easycleared box will drag the height of that box waaaay down the page, until that invisible clearer is vertically below the bottom of the previous float column (assuming there
is a bottom!). This can "generate" a huge empty space inside that once-small easycleared box, depending on the actual height of the neighboring float column.
Of course Opera 7 has always correctly implimented the clearing specs just like IE does (aside from bugs), and the Mac browsers are not involved either. If you are wondering how this issue can be fixed, well, it can't. Gecko and Opera are now both following the float clearing specs correctly, and IE only fails because of the faked "clearing" we are forcing upon it.
Preventing External Clearing
If you have the above described problem, one way to prevent the clearer from clearing the adjacent float column is to make the container a float itself. Of course once you float the container you no longer need easyclearing, sigh...
Note that when all the main elements in a column setup are floats, the worst IE float bugs simply do not happen. Thus using an all-float approach to column design can actually be easier to accomplish, at least within a rigid-width layout.
Them That Done It
Thanks to
Tony Aslett for showing us the way. His site,
csscreator.com is a killer CSS forum where newbies and gurus alike hang out and exchange CSS know-how. Tony's original demo page for this method can be found
here, and the relevant forum thread is
here.
Kudos also to
Doug for pointing out the "period problem" in FireFox, and to
Mark Hadley for that elegant IE/Mac fix, and to
Matt Keogh for showing how "inline-table" also fixes IE/Mac while using an already-approved CSS property. Once more the CSS community comes thru for us all! :-)