Using CSS Fixed Position Elements Across Browsers Without Javascript

Posted October 16, 2009 at 9:34 AM

Tags: HTML / CSS

Yesterday, I posted my first experimentation with CSS fixed-position elements and getting them to work across all browsers, including IE6. The solution I demonstrated yesterday involved using proprietary Javascript expressions in the CSS that would be used by IE6; this technique worked pretty well, but caused a jittery effect as the "fixed" position elements repositioned themselves during page scroll.

While I was doing research on using the "position: fixed" CSS property, I came across two solutions for getting fixed-position elements in IE6. The first was the one that I demonstrated yesterday; the second one was to fake fixed-position elements by making them absolutely-positioned elements. In order to get that to work, however, we need to move the primary content out of the BODY tag and into a sub-content tag. This way, the BODY tag can have a height / width of 100% which will work for absolutely-positioned elements, while the sub-content container can take care of the scrolling:

 
 
 
 
 
 
Creating Fixed-Position Elements Across All Browsers Using A Sub-Content Container And Absolutely Position Elements. 
 
 
 

With this page architecture, since the BODY tag does not scroll, elements within the BODY tag, but outside the sub-content container, can be positioned absolutely and will act just as if they were fixed-position elements. Here is the sample page that I created:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>Using CSS Fixed Position Across Browsers</title>
  • <style type="text/css">
  •  
  • html,
  • body {
  • margin: 0px 0px 0px 0px ;
  • padding: 0px 0px 0px 0px ;
  • }
  •  
  • #site-body-container {}
  •  
  • #site-body-content {
  • padding: 15px 15px 15px 15px ;
  • }
  •  
  • div.fixed-position {
  • background-color: #F0F0F0 ;
  • border: 1px solid #CCCCCC ;
  • height: 48px ;
  • line-height: 50px ;
  • position: fixed ;
  • text-align: center ;
  • width: 148px ;
  • z-index: 1000 ;
  • }
  •  
  • div.fixed-n {
  • left: 50% ;
  • margin-left: -75px ;
  • top: 0px ;
  • }
  •  
  • div.fixed-n-e {
  • right: 0px ;
  • top: 0px ;
  • }
  •  
  • div.fixed-e {
  • margin-top: -25px ;
  • right: 0px ;
  • top: 50% ;
  • }
  •  
  • div.fixed-s-e {
  • bottom: 0px ;
  • right: 0px ;
  • }
  •  
  • div.fixed-s {
  • bottom: 0px ;
  • left: 50% ;
  • margin-left: -75px ;
  • }
  •  
  • div.fixed-s-w {
  • bottom: 0px ;
  • left: 0px ;
  • }
  •  
  • div.fixed-w {
  • margin-top: -25px ;
  • left: 0px ;
  • top: 50% ;
  • }
  •  
  • div.fixed-n-w {
  • left: 0px ;
  • top: 0px ;
  • }
  •  
  •  
  • /* -------------------------------------------------- */
  • /* -- IE 6 FIXED POSITION HACK ---------------------- */
  • /* -------------------------------------------------- */
  •  
  • html,
  • body,
  • #site-body-container {
  • _height: 100% ;
  • _overflow: hidden ;
  • _width: 100% ;
  • }
  •  
  • #site-body-container {
  • _overflow-y: scroll ;
  • _overflow-x: hidden ;
  • _position: relative ;
  • }
  •  
  • div.fixed-position {
  • _position: absolute ;
  • }
  •  
  • /* For the scrollbar. */
  • div.fixed-n-e,
  • div.fixed-e,
  • div.fixed-s-e {
  • _margin-right: 16px ;
  • }
  •  
  • /* For the scrollbar. */
  • div.fixed-n,
  • div.fixed-s {
  • _margin-left: -83px ;
  • }
  •  
  • </style>
  • </head>
  • <body>
  •  
  • <div class="fixed-position fixed-n">
  • North
  • </div>
  •  
  • <div class="fixed-position fixed-n-e">
  • North East
  • </div>
  •  
  • <div class="fixed-position fixed-e">
  • East
  • </div>
  •  
  • <div class="fixed-position fixed-s-e">
  • South East
  • </div>
  •  
  • <div class="fixed-position fixed-s">
  • South
  • </div>
  •  
  • <div class="fixed-position fixed-s-w">
  • South West
  • </div>
  •  
  • <div class="fixed-position fixed-w">
  • West
  • </div>
  •  
  • <div class="fixed-position fixed-n-w">
  • North West
  • </div>
  •  
  • <!-- ------- -->
  • <!-- ------- -->
  •  
  • <div id="site-body-container">
  • <div id="site-body-content">
  •  
  • <div style="height: 3000px ;">
  •  
  • <p>
  • Lorem ipsum dolor sit amet, consectetur
  • adipiscing elit. Aliquam dictum enim in mauris
  • luctus convallis. Aliquam erat volutpat.
  • Suspendisse potenti. Duis blandit, urna vitae
  • feugiat porttitor, risus est ornare metus, at
  • dignissim urna velit id enim. Donec lectus nisi,
  • consectetur eget sollicitudin id, bibendum
  • laoreet velit.
  • <p>
  •  
  • </div>
  •  
  • </div>
  • </div>
  •  
  • </body>
  • </html>

You'll notice that the standards-compliant browsers still use "position: fixed". Only IE6 (with the help of the underscore ("_") hack since my IE doesn't allow conditional comments) falls back on this sub-content container work around. Because the content scrollbars are not part of the main window, our right-aligned and center-aligned "fixed" position elements need to take some additional margin into consideration to prevent overlapping with the content container's scrollbar.

Here is a screen shot of IE6 using the above code:

 
 
 
 
 
 
Creating Fixed-Position Elements In IE6 Without Any Jittery Effect Using Pure CSS. 
 
 
 

And, since this solution does not use any proprietary IE-based Javascript expressions, scrolling around the window does not lead to any jittering.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page





Reader Comments

Oct 17, 2009 at 8:30 AM // reply »
2 Comments

Nice article.
I want to add important note about this usage.
If !Doctype is not used, position:fixed doesn't work on Internet Explorer.


Oct 17, 2009 at 2:41 PM // reply »
1 Comments

Nice solution.
It may interest you to watch this screencast (in Russian only, sorry) - http://www.artlebedev.ru/tools/technogrette/html/emulate-fixed/
Examples can be downloaded by clicking ??????? at the bottom of the page.


Oct 31, 2009 at 5:00 PM // reply »
7,572 Comments

@Memiso,

I hope people are *always* using doctypes.

@Oleg,

Thanks, I'll take a look.


Nov 3, 2009 at 2:55 AM // reply »
1 Comments

Doctype really cause lot's of issue with IE6


Nov 11, 2009 at 6:16 AM // reply »
2 Comments

Is there any ways to disable scroll bar, because it is unnecessary.


Nov 15, 2009 at 8:06 PM // reply »
7,572 Comments

@Anil,

I am not sure you can remove them when not necessary. I know you can add them when not necessary.


Post Comment  |  Ask Ben

Recent Blog Comments
Lee
Mar 22, 2010 at 10:08 AM
Javascript's Implicit Boolean Conversions
I would certainly still use if(strValue.length > 0) over if(strValue) simply because I believe it makes the code more self-documenting. Not everyone knows that an empty string evaluates to false. ... read »
Mar 22, 2010 at 7:43 AM
Terms Of Service / Privacy Policy Document Generator
Thankyou for this very helpful form. You've made my life much easier today. I'll have a look around your site... I'm sure there's some more good stuff here..Thanks Dave ... read »
Mar 22, 2010 at 7:21 AM
Encountered "(. Incorrect Select Statement, Expecting a 'FROM', But Encountered '(' Instead, A Select Statement Should Have a 'FROM' Construct.
I got this exception now. In case you're using var-es local struct, CF gives you couple of "new" exceptions: Encountered "local. and Encountered "id. Incorrect Select List, Incorrect select colum ... read »
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »