Faking css position: fixed in Internet Explorer 6

This is a trick that I have been doing for some time, but now that Internet Explorer 7 is out and it supports position fixed in css; I think this trick will been even more useful now that it will allow web developers to take better advantage of css and at the same time not leave out the Internet Explorer 6 user base. I am posting this here because I sent this to a friend a couple days ago and thought others might be able to use it as well.

The trick is really quite basic. For simplicity I am just putting this example inline, but you can put it where you want.

First your declare your intended style for IE7 (and basically every other browser):

<style type="text/css">
  #fixed_div {
    position: fixed;
    top: 0px; /* tweak this according to placement */
    left: 0px; /* tweak this according to placement */
    /* add additional styling, etc. */
  }
</style>

Second add in your Javascript function which is going to be doing your movement for IE6:

<script type="text/javascript">
function move_box() {
    var offset = 0; // set offset (likely equal to your css top)
    var element = document.getElementById('fixed_div');

    element.style.top = (document.documentElement.scrollTop + offset) + 'px';
}
</script>

Then declare the element itself:

<div id="fixed_div">I am fixed, even in IE6</div>

Lastly, implement your fix for IE6 only:

<!--[if lt IE 7]>
<style type="text/css">
  #fixed_div {
    position: absolute;
    top: 0px; /* tweak this according to placement */
    left: 0px; /* tweak this according to placement */
  }
</style>
<script type="text/javascript">
window.setInterval(move_box, 100);
</script>
<![endif]-->

I usually put this last part right before the end of the body (I guess you could just put the whole script there). This is because you don’t want the window.setInterval to fire before your fixed div is declared. You could use the body onload but I try to stay away from that because I know it is popular for other uses on more advanced pages and I don’t want to run the risk of interfering.

Here is the whole script put together:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
 . . .
<style type="text/css">
  #fixed_div {
    position: fixed;
    top: 0px; /* tweak this according to placement */
    left: 0px; /* tweak this according to placement */
    /* add additional styling, etc. */
  }
</style>
<script type="text/javascript">
function move_box() {
    var offset = 0; // set offset (likely equal to your css top)
    var element = document.getElementById('fixed_div');

    element.style.top = (document.documentElement.scrollTop + offset) + 'px';
}
</script>
 . . .
</head>
<body>
 . . .
<div id="fixed_div">I am fixed, even in IE6</div>
 . . .
<!--[if lt IE 7]>
<style type="text/css">
  #fixed_div {
    position: absolute;
    top: 0px; /* tweak this according to placement */
    left: 0px; /* tweak this according to placement */
  }
</style>
<script type="text/javascript">
window.setInterval(move_box, 100);
</script>
<![endif]-->
</body>
</html>

NOTE: This will only work properly on pages that have a valid doctype definition. If you don’t use one [you should start or] you need to change document.documentElement.scrollTop to document.body.scrollTop in the move_box function.

You can also modify this script to apply to multiple elements by passing arguments to the function or something similar. If you are doing multiple elements on a single page, you may want to consider setting up some kind of Javascript array that tracks what elements need to be corrected, this could save you from having multiple window.setInterval instances running.

Way to go Microsoft Internet Explorer 7

This may be one of the very few times that I will ever praise Microsoft for their work in the web browser industry. And quite honestly I am only praising them out of the fact that they are finally fixing one of Internet Explorer’s most abused bugs (i.e. It would have been truly better if they didn’t have the bug in the first place).

I am talking about the Star-HTML hack (I apologize for not having a better reference article) in Internet Explorer. Which is where you can add a ‘*’ in front of your CSS selector inside of a style tag and it will only be applied in Internet Explorer.

<style type="text/css">
  /* Will only be rendered in IE */
  * html {
    border: none;
    height: 100%;
    margin: 0px;
    padding: 0px;
    width: 100%;
  }
</style>

Apparently Internet Explorer 7 has corrected this issue so that it ignores the syntactically bad code just like most other browsers. Which I would say is a good thing. However there is a bit of an uproar brewing about it being removed because so many developers have come to rely on it to get their webpages to look right in both IE and Non-IE browsers.

Personally I welcome the fix and think that people who have relied on a hack composed of syntaxtically incorrect code should not be surprised that it has finally caught up with them. Even Microsoft gets around to fixing its bugs from time to time. Personally, I don’t understand why anyone writes syntactically bad code to target IE when you can use conditional comments (which have been around since IE 5) to add specialized styles, extra Javascript, ActiveX components, etc.

However in all fairness Internet Explorer 6 was released almost two years ago (August 6, 2004), which is a tremendous amount of time when it comes to the web (which is part of the reason why IE is so insecure and has such standards compliance problems; it is very outdated in comparison to other browsers). So I can understand that many developers have come to rely on certain things to be a constant and may have many pages to fix.

Internet Explorer 7 also boasts what seems to be a decent jump up in standards compliance. It is hard for me to say specifically what has improved; I have just noticed that when I layout pages, IE 7 beta 2 renders them a lot closer to Firefox, Opera and Konqueror without custom styles.

Another thing that I am glad that Microsoft is finally fixing in their browser is the support for PNG (Portable Network Graphic) transparency. However I am by no means applauding them on this effort because many other browser have had full support for quite some time and the format has been standardized since October 1996. I am just glad that once the browser is released and in time I will finally be able to leave behind GIF’s (a graphic format from the late 1980’s) and stop doing DOM based workarounds.

This is a poll that is going on about the Star-HTML hack. If you understand the concept I encourage you to vote (if you have not already).
http://www.positioniseverything.net/articles/poll/star-html.php

{
}