I continuously find myself having problems with elements floated right in IE7.
I have read many Stack Overflow questions which are similar to this one but there doesn’t seem to be any consistently clean CSS answers.
What I mean by this is is I want to see answers which DO NOT change the HTML. E.g:
- Put the floated element first
- Add a ‘clear: both’ div after the floated element.
I understand that sometimes the floated element doesn’t account for its parents height and therefore sometimes fails to contain it properly. Occasionally I find myself ‘adding layout’ to an element with zoom: 1 which sometimes fixes it. Other times I find myself messing about in a conditional IE7 style-sheet which isn’t the best fix in my opinion.
Note: What I mean by ‘having layout’ – http://www.satzansatz.de/cssd/onhavinglayout.html
I have also read other answers to do with using relative and absolute positioning (parent div and child div respectively). This pulls it up but often affects surrounding divs.
I would be happy to add a bounty to this question if someone can give an in depth explain as to the reasons this happens and a detailed discussion of the various fixes, IDEALLY CSS ONLY!
Many thanks!
EDIT
The most common problem I encounter is when I have something like this:
Left Sidebar – Main – Right Sidebar
Right will often drop when floated. Ideally this should be in the format Left – Right – Main, but I continuously find myself styling developers work (Drupal mainly) where this is the case and it is too much hassle to get them to change their work. Make sense? Because I’m styling developers work they never put the clear block in too (which personally I think is horrible and dirty anyways!)
Introduction
Your title indicates a desire to see a fix for the
float: rightdrop bug, but your text implies some broader scope desire to see solutions to “problems with elements floated right in IE7.” There are many general problems with floated elements (rightorleft) in that browser. Even though one may question whether support of the IE7 browser is worthy of much attention any more, it undoubtedly will remain so for some people for some time. Therefore, I’m going to take the opportunity here to address numerous issues at once regarding floats in that browser. Note that many experiments and references below come from an excellent resource: http://www.brunildo.org/test/index.html.CSS for the Containing Element
For a containing parent to the floated element the following css should be set:
Making sure it hasLayout is important to prevent types of margin and padding errors, a type of peek-a-boo bug, and allowing the
overflowto clear. For a sequence of floats, some html may need changing ifpaddingon the container is desired to work properly.With regards to one “drop” issue with a
float: right, you may want to avoid setting an explicitheightormax-heighton the container. Amin-heightis okay. Setting aheightand then having thefloatbe taller than the container makes IE7 not behave with following elements. There is no pure css solution that I have found noted.If the container is itself
position: absolutethere can be issues with positioning a float that may require that float itself to be set toposition: absoluteinstead of being floated.References:
overflowto clear — http://www.quirksmode.org/css/clearing.htmlheight— http://austinmatzko.com/2007/07/25/internet-explorer-7-float-bug/, http://www.brunildo.org/test/fenc7.html (and his similar problem link on that page).absolute— Floating Too Far Right!CSS for the Floated Child
For a the floated child element, the css (besides
float: right) to set depends on two things:Absolute Container
Again, as noted above, a containing parent that is
absolutemay require a change in how the child is handled.Float is Also a Clearing Element
If the
floatis also going to have aclearset on it, then there are numerous issues that can arise depending totally upon thehtmlandcssof the elements around it. There is no single canonical fix, so look at the references below.References:
absolute— Floating Too Far Right!clear— http://www.brunildo.org/test/IEWfc.html, http://www.brunildo.org/test/IEWfc2.html, http://www.brunildo.org/test/IEWfc3.htmlCSS for Child Elements of Container Before the Float
If the
float: rightfollows an element in thehtmlstructure that should be to its left (and not above it), then that preceding element(s) must befloat: left.CSS for Child Elements of Container After the Float
A Clear Element
For an element after the
floatthat hasclearset, then if it haspaddingandbackground-color, make sure it also hasLayout to avoid a doubling of thepadding; also this prevents extra space above thecleardue to containerpadding, and avoids othermarginissues.References:
padding— http://www.brunildo.org/test/IEClearPadding.html and http://www.brunildo.org/test/IEFloatClearPadding.htmlmargins— http://www.brunildo.org/test/Op7_margins_float.html (look down the page for IE7)A Paragraph Before a Clear Element
Having a paragraph with a
margin-bottomand shorter in height than thefloat, located between the floated element and a clearing element, can create an extra gap between theclearand thefloatequal to thatmargin. There is no known pure css fix other than giving the paragraph a zero bottom margin (which may not be acceptable if the paragraph may go taller than the float).Reference:
Conclusion
I cannot guarantee I have addressed every issue that may occur with a right floated object, but certainly many major ones are covered. As to “why” these things happen, it is all “bugginess` in IE7.