IE6 resize bug (position: relative becomes fixed)
One bug kept popping up on the sites I built, and I was almost going insane. The bug I’m talking about is specific to Internet Explorer 6 (IE6) and has to do with what happens when you resize the page. Here’s an example page showing the bug. Open it in IE6 and try resizing the window. Let me explain:
[Update: Paula comments that IE7 beta 2 has the same problem. Thanks Paula.]
Setting up the IE6 resize bug
To see the bug in action you need two things: You need to center your page using the body element and you need to be using a fixed width (not percent) on the element. If you use this you will quickly notice that all elements you set position: relative on, will stay fixed on the page when you resize the window. They behave as if you just set position: fixed on them, but only until you reload. It’s really a fun effect (if you know the fix).
Example code:
body {
margin: 0 auto;
width: 760px; /* Any fixed or fluid width */
}
/* Affects all elements with position: relative; */
#example p {
position: relative;
}Here’s a live example of the IE6 resize bug with borders added for clarity.
The bug is kinda rare you could say. But using body for centering is a great way of getting rid of one extra container div, so I have been using it more and more lately (IE5 can’t handle that, but you don’t support it do you?). Anyway, not being able to use position: relative is not ok.
Making things behave as normal again
Fortunately the fix is very easy. You simply add position: relative to the body element. *Blam* All previous broken elements start behaving like expected again. Here’s the same example with the bug fixed.
That’s a couple of hours less IE6 adoption time on my next project. I hope it helps someone out there too.
matthijs (3 comments) ( 5 Sep 2006 )
Cool. Thanks for the write-up. Imagine can be frustrating to deal with this one if you don’t know a solution yet.
So, besides this one, have you encountered other problems in container-divless layouts? I always used a container div because of the IE5 problems, but since it’s time to leave that one behind I might go divless as well.
Emil Stenström (605 comments) ( 5 Sep 2006 )
@matthijs: Nope, no other problems. I sometimes use both html and body and add background images and widths to both, and even that can work well. I recently wrote a writup on the KTH website. That one uses styling on both html and body. Thanks for your comment!
Matthijs (3 comments) ( 6 Sep 2006 )
Thanks for your answer Emil. I’l be using the wrap-divless technique in a redisign in which I cannot change the html. Using the body will save me from having to place the rules on lots of other elements.
Robert Nyman (33 comments) ( 6 Sep 2006 )
THANK YOU! Currently I’m doing work for a client where I’ve encountered this exact problem! Tomorrow, first thing I’ll do is test your solution!
Robert Nyman (33 comments) ( 6 Sep 2006 )
Hmm, come to think of, most likely it won’t solve my specific problem…
I’m using it to create a CSS Scrollable Table with Fixed Header and there I have it to emulate a fixed positioning.
When I add
position: relative;to any of the parent elements to the one that acts fixed, the bug goes away (i.e. it doesn’t have to be thebodyelement) but then I also lose the the effect of a fixed header…Emil Stenström (605 comments) ( 7 Sep 2006 )
@Robert Nyman: Setting it to a parent element made that element become “fixed until reload” in my examples. I’m afraid I don’t have any experience from the fixed thead example you link to, all I know is that IE is very bad at redesigning table elements.
stefano (2 comments) ( 7 Sep 2006 )
I have to thank you because I also got crazy with this but was not able to find out the bug. Great!
Ste
Paula (2 comments) ( 11 Sep 2006 )
This bug is not only IE6.
I use IE7 BETA 2, and I had this problem.
Emil Stenström (605 comments) ( 11 Sep 2006 )
@Paula: very interesting, seems like this fix was needed after all.
Paula (2 comments) ( 12 Sep 2006 )
it lacked to say: this same error in the IE7 BETA 2, is corrected with position: relative in body.
=D
Roger Johansson (5 comments) ( 26 Sep 2006 )
The bug is still there in IE 7 RC1, so don’t expect it to go away any time soon. Grr.
Kane (1 comments) ( 19 Oct 2006 )
Thanks! It was very useful. Just discovered this bug and spent 30 mins trying to solve it, but accidentally found your site!
Emil Stenström (605 comments) ( 19 Oct 2006 )
@Roger: Damn (but good for this article :) I guess it will be valid a while longer.
@Kane: Good to know you found it useful :)
Brian (7 comments) ( 1 Dec 2006 )
I ran into this problem as well, but putting position:relative on the body tag didn’t fix it for me.
I think it is because I had a table and table cell with align=center on the page. After I put position:relative on the element inside the centered table cell, it worked.
Thanks for pointing me in the right direction.
Emil Stenström (605 comments) ( 4 Dec 2006 )
@Brian: Yeah, I’ve noticed that too, this does not work for all cases. You shouldn’t use the align attribute though, it’s design and should be in the CSS (text-align: center).
Ido Tzang (1 comments) ( 28 Mar 2007 )
I was so happy to find this post – i have that same exact problem… only thing is – your patch didn’t solve the problem for me.
Do happen to have any other helpful ideas / directions ?
10x,
Ido
SAM0077 (1 comments) ( 4 Apr 2007 )
Thank you Emil for the fix. I tried it and I am having trouble with it. Is there anything else than positon: relative; to add to the CSS body?
Stuart Radley (1 comments) ( 5 Apr 2007 )
Wow, I actually developed a technique for NOT using position relative because of this problem – although it caused great frustration. I was amazed that this works perfectly in IE7 (don’t have 6 to hand). I can’t wait to start using this again! Thanks!
Phil (2 comments) ( 20 Apr 2007 )
Thanks! Really helpful.
Terry McGuire (1 comments) ( 25 May 2007 )
You rock!
This saved me such a headache:
ie6-resize-bug
Thank you.
James (3 comments) ( 2 Jun 2007 )
OMG! Thank you so much, this was driving me insane!!!!!! easy fix indeed!
thomas (2 comments) ( 12 Jul 2007 )
Thank you for this blogentry, it was extremely helpful for me!
cheers
Vibhore (1 comments) ( 17 Sep 2007 )
Thanks a lot for the tip, i just wasted 5 hrs fixing it and i am sure i would have never tried this fix on my own!!!
Peter (3 comments) ( 16 Oct 2007 )
Appreciate your sharing the fix. I was pretty freaked out when I just discovered this bug, but thankfully Google led me to your page. Thanks again!
Peter Morris (1 comments) ( 29 Oct 2007 )
Thanks very much! Such an easy fix! Wish I’d read your blog before tearing the remainder of my hair out!
tim (4 comments) ( 22 Nov 2007 )
you are a savior, this has been bugging me for months!
sciontphono (1 comments) ( 17 Jan 2008 )
Really good…I was getting crazy trying to solve this bug…THANKS!
Rodrigo (1 comments) ( 26 Feb 2008 )
Thanks.. your post and examples helped me a lot!!
anonymous (6 comments) ( 25 Mar 2008 )
Awesome! Thanks for the post. But looks like you need it for your website itself. I resized the window on this article and it was not snapping into place… tsk tsk.
Nilesh Ashra (1 comments) ( 1 Apr 2008 )
Hi dude,
This page was first hit on google when searching for “ie6 position resize” and fixed my problem instantly – thanks a lot :)
Cas (1 comments) ( 8 Apr 2008 )
This bug was driving me nuts until I found your fix – and as an aside – I found this bug was in effect in IE 7 as well (but my page rendered fine in Firefox and Safari, which was what had me stumped). THANK YOU.
Jay (2 comments) ( 23 Apr 2008 )
You’re my hero of the hour! I was going nuts wondering why it was doing that in IE for a site I’m developing. I found this article after Googling the problem – tried the solution, and it works. I hate IE, but without it I’d bill a lot less hours when developing a site.
Emil Stenström (605 comments) ( 23 Apr 2008 )
Hi all, cool to see that you like my little fix :)
kev (1 comments) ( 7 May 2008 )
This is just what I have been looking for as part of a multiple solution to an IE and Firefox bug. I am toggling between two divs that both contain different text. The first fix was to make sure that position:relative was put into the text layers so that they would inherit their proper z-index from their position in the code. The second fix is this one described on this page, so that those relatively positioned divs will move correctly on page resize for both IE and Firefox. Thanks all.
gaurav (2 comments) ( 22 May 2008 )
Thanks for the article. I was facing the same concern on one of the web pages I am working on right now. Since I am from Java/J2EE background, I don’t work on CSS so much.
Thanks again.
Michael (6 comments) ( 31 May 2008 )
Thank you so much, friend! This has been driving me nuts for two days now. Your solution fixed EVERYTHING. You so rock!
lyndonaus (2 comments) ( 8 Jul 2008 )
You have restored a 68yr old man’s faith in human nature with your generosity in sharing this fix!!!
Nathaniel B (1 comments) ( 21 Jul 2008 )
Many thanks for this fix! I was wondering what was going on!
James (3 comments) ( 24 Jul 2008 )
Thanks so much!!! This was so frustrating.
Lucy (1 comments) ( 25 Jul 2008 )
Thank you! I was puzzled by that …
Jon (6 comments) ( 6 Aug 2008 )
Thanks Emil! Interestingly, this bug was more a problem for me in IE7 than IE6 (I have all kinds of position values going on). You helped me end my day on a high note!! Thanks again.
Jon
Emanuele Ciriachi (1 comments) ( 24 Sep 2008 )
You sir, just spared me a lot of trouble. Our website will hopefully be updated tomorrow without this problem, if you hurry you can still see it today (shows up in the “Featured Products” section).
senen (1 comments) ( 4 Oct 2008 )
Was going crazy trying to fix the problem, im using so much time just for trying to keep the same look on ie as on all the other browsers. hate ie. thx alot for the help
volomike (1 comments) ( 12 Oct 2008 )
Emil. You the man — even 2 years after posting this article. Had a client project and this came up. Was baffling and only occurred with IE-type browsers. Opera, Safari, FF — they all worked fine. It’s sad that even IE7 final version has this glaring bug. And I’m sure that bug report will fall on deaf ears at Microsoft. Your fix set me straight.
Emil Stenström (605 comments) ( 12 Oct 2008 )
Thanks everyone! Nice to see that my silly articles help people :)
Aaron (4 comments) ( 22 Oct 2008 )
I just spent the last day and half trying to squash this bug. Thanks for posting about it. My frustration level just dropped a bunch.
Smitty (1 comments) ( 11 Dec 2008 )
You are the freaking man. I’ve been having issues with a very important client and this helped out so much. I knew it was the relative positioning but I didn’t know the fix. Thanks for NOT keeping that to yourself.
Emil Stenström (605 comments) ( 11 Dec 2008 )
@Aaron, Smitty: Thanks for taking the time to thank me! :) I was in your exact position and was force to reverse-engineer a fix. I thought I might as well post it here.
Devin (1 comments) ( 23 Jan 2009 )
Thanks for the post. I ended up having to use absolute positioning to get my div in the right place- but I appreciate the info.
Roman (3 comments) ( 28 Jan 2009 )
Thank you very much for your research and post! It seems to work.
Leonardo A. Souza (1 comments) ( 30 Jan 2009 )
Thank you! Thank you! Thank you! Thank you a lot! You save the time (and the live) for a desperate brazilian developer!
jason (9 comments) ( 3 Feb 2009 )
DUDE. i have been freaking out with this pathetic ie6 bug for so long now…bashing my head against a mined spiked poisoned wall for ages trying to understand why setting position to relative or absolute was behaving as fixed in my scrollable div.
the solution was simply to set the containing element to position:relative. so simple yet impossible to guess.
thanks man. you’ve really saved my life as i was about to go jump in front of a semi-truck just to finally achieve peace of mind.
Farzan (1 comments) ( 7 Mar 2009 )
Thank you, thank you, thank you.
This hack saved my neck… my customer was killing me and I didn’t know how I could fix it.
wingMan (1 comments) ( 5 Apr 2009 )
Thanks for posting Bro!
This has been driving me nuts for the last month.
PavelGee (1 comments) ( 30 Apr 2009 )
Thanks so much! Being a non-professional, and facing so many bugs in IE at the onset of the project left me desperate at times.
Lucky I came across your page!
Juan Araya (1 comments) ( 18 Jun 2009 )
Emil !!!
THANK YOU! THANK YOU!! THANK YOU!!!!
Had noticed this issue in some websites but was not aware I was a victim of it. A client just sent me an email complaining about this.
Thanks again for your excellent documentation.
Clinton Montague (1 comments) ( 6 Jul 2009 )
Thanks for posting the answer to this problem, it would have caused a lot of brain-ache if I didn’t stumble across this post. I’ll be writing a post about this problem in the near future linking back here :)
Thanks again
Sam (2 comments) ( 23 Sep 2009 )
Thanks very much for posting this — so simple to fix, but so very frustrating if you don’t know how! I can’t understand how an operation the size of Microsoft can still put out a product that fails to meet basic standards. mindblowing. Cheers!
anonymous (6 comments) ( 3 Oct 2009 )
All these bugs are a pain in the bum!
it’s good to be informed about them.
Wolf Oliver Abbas (1 comments) ( 9 Oct 2009 )
I can not believe it. Your post is three years old and the bug is still not fixed. I got the very same problem. Thank you for the solution!
Michael (6 comments) ( 11 Jan 2010 )
BEWARE: this fix may have side affects to existing sites! My site wasn’t able to show thickbox popups any more in IE6, after I had added position:relative to the body element. So it is’t the solution for me… :(
Jasper (2 comments) ( 5 Feb 2010 )
@Michael, just use a container div instead of body, then it’ll work.