Accessibility

CSS Advisor

Escaping floats

by CSSAdvisorAdmin on November 3, 2006
Edited by: LoriHC (Dreamweaver Team) on November 28, 2009
Avg Rating 3.2 (6)   |   Log in to rate post.

Tagged with Explorer 6.0 , Explorer 5.0 , Explorer 7.0 , Explorer 5.5 , Float

Problem

If a series of floats are placed inside a container with no specified width or height- and the floats' width exceeds the natural width of the container, the container will not expand vertically to fit the floats— and the floats will extend outside the container.

Solution

Specify zoom: 1 for the container (inside a conditional comment).

Detailed description

If a series of floats followed by a clearing div is placed inside a container with no specified width or height, and the floats' width exceeds the natural width of the container, the container will not expand vertically to fit the floats—and the floats will extend outside the container in Windows/Internet Explorer 6 and earlier. Other potential pitfalls include horizontal scrollbars appearing in the page when the window is resized to alternate widths.

This bug was originally reported and documented by Big John Gallant and Holly Bergevin at positioniseverything.net. (Read original article)

The old (pre-IE7) solution for this bug, and several others, was the Holly Hack. However, the Holly Hack relies on buggy behavior that is no longer present in Internet Explorer 7 -- and using it can cause things to go a bit haywire. Instead, use an Internet Explorer Conditional Comment (IECC) to apply zoom: 1 to the container in all versions of Internet Explorer. This will give the container the "layout" it needs to keep the floats from escaping.

<!--[if IE]>
<style type="text/css">
#containerID { zoom: 1;}
</style>
<![endif]-->




Text ID: ESCAPING_FLOATS



Comments (4)

To add a comment, please Log in.




Post updated

Hi rich30

Thanks for the prodding -- I've been meaning to update the posts that mention the Holly Hack as the solution. I've just updated this one and will update others shortly.

Use conditional comments instead of hack

As this only affects IE (I've seen this in IE7 as well) try a conditional comment instead of a hack.
Put this in the head of your document e.g.
<!--[if IE]><style type="text/css">
#featuredProducts {height:1%;}lt;/style>
<![endif]-->

I have to agree

I have to agree with sbstash on this issue, the only way to resolve it is to add a width/height to your container.

Why hack?

What is the point of using this hack?  Honestly, hacks should really be avoided at all costs.  How often does a designer place a series of floats inside a container w/o specifying any widths or heights?  Why would you even consider doing this?  This seems really obscure, especially considering IE 5 is still listed on the original post.

Here's a solution: apply a width/height to your floats.





CSS Advisor home
Submit a post

rss

CSS ADVISOR RSS FEEDS
Recent: POSTS | COMMENTS | EDITS


Browsers
Internet Explorer (45)
Mozilla/Firefox (16)
Opera (7)
Safari (13)
CSS Properties
Background (6)
Border (3)
Color (1)
Float (15)
Font (1)
Margin (6)
Padding (4)
Position (15)
Text (13)
Width/Height (9)