O'Reilly

CSS Fixed Positioning

By on  

When you want to keep an element in the same spot in the viewport no matter where on the page the user is, CSS's fixed-positioning functionality is what you need.

The CSS

	.element	{ position:fixed; top:2%; right:2%; }

Above we set our element 2% from both the top and right hand side of the viewport. You can scroll on the page forever but our element will still stick to the specified 2% mark. You can see fixed positioning in effect on the Facebook website (chat toolbar).

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() – this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

  • LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • Telephone Link Protocol

    We've always been able to create links with protocols other than the usual HTTP, like mailto, skype, irc ,and more;  they're an excellent convenience to visitors.  With mobile phone browsers having become infinitely more usable, we can now extend that convenience to phone numbers: The tel...

  • Page Peel Effect Using MooTools

    Soh Tanaka release a great script back in May titled Simple Page Peel Effect with jQuery & CSS. The idea is that you place a "peel" image on the upper-right side of an element which, when hovered, "peels" open and peels close....

Discussion

  1. sam

    facebook works in ie6 tho, your example doesnt. would be awesome if you could provide the workaround as well.
    thanks!

  2. Webling

    maybe this will help you http://tagsoup.com/cookbook/css/fixed/

    nice short article.

  3. I think Gmail/Other Google Products do that too when they are loading something via ajax, they put one of those in the corner of the screen.

  4. To fix this in <= ie6 requires using position: absolute instead of position:fixed.

    html, body {
    height: 100%;
    overflow: auto;
    }
    
    body .element {
    position: absolute;
    bottom: 0;
    }
    

    Then you can use:

    body > .element    { position:fixed;} 
    

    so that browsers > IE6 will work properly.

  5. Jestep, that solution will only work if you’re not using any REAL absolutely positioned elements on your page – they’ll just end up as position:fixed;. IE6 really sucks!

  6. http://www.gunlaug.no/contents/wd_additions_15.html

    I usually use this technique to get things to be fixed in IE6~

  7. @Sam, you’d be surprised how easy the FB workaround is: 1) add a class to the HTML that removes overflow; 2) inject an extra wrapper that has width and height of 100%, overflow and relative position; 3) make the “presence bar” absolute positioned.

    Another option is to play with _property: expression(“…”), but you risk crashing the browser.

    @Jestep, why not use a shorter hack e.g. _position:absolute ;)

  8. Now when I think of it, this could be a very small plug-in — just a concept.

    1) On document.ready create a dummy element; 2) change position to fixed and bottom to 0;

    Now having in mind that the initial position is static and unrecognized property values result into default value, thus if fixed is unsupported the value will remain static

    3) check for bottom offset 4) if it is 0, the position:fixed is supported, if not use the above mentioned fix; 5) delete the dummy element.

  9. Not to throw a spanner in the works, but with progressive enhancement in mind, we shouldn’t worry about our websites looking the same in all the browsers…. On top of that, we really need to start moving away from IE6, otherwise it will never disappear.

  10. The problem with IE6 is that it will be supported by Microsoft until 2010 plus it is used (as far as I know) as a primary browser within most business organsiations, and usually (for some baffling reason) their IT departments are reluctant to upgrade to better browsers

  11. sam

    people love to hate ie6, but the thing is, because its such a problematic browser, there are stacks of talented people coming up with workarounds, fixes, etc for it.
    IE6 has 20% of browser users still. def worth paying attention to.

  12. Posiiton fixed is something I try to avoid as much as possible because of the IE6 issue. If you have a good workaround let me know!

  13. I searched for “Unsupported value: “fixed'” and reached here.
    Thanks… this makes an idea of my problem.

    -Mahmud

    Mohammad Mahmud Kabir

    http://www.MahmudKabir.com

  14. can fixed-position after scroll and find div ?

  15. Jake Wood

    Just wondering how to make a page default to the left until the window is a certain size, and then become centered, i.e. facebook. The facebook logo is set left justified, until the window is big enough for all the menu items to be visible, then the page becomes centered.

  16. Angelica

    The codes worked but there’s one problem, the background of the timer we did is not moving together with the timer’s text. It is left at the top so what happen is that the only fixed element is the timer’s text not its background. How can I solve it? here’s the code.
    .#apDiv1{ position:fixed;}>

  17. Juan Paolo Escopete

    I got a problem how can I put the fixed positioned div always aligned to the body… coz I got the body centered and when I zoom out the fixed positioned div stays on the upper left corner of the screen.

  18. helpful post, thanks for solved my problem.

  19. Unfortunately this doesn’t work on some mobile browsers. However it does on the Kindle of all things.

  20. This was just what I needed, thank you very much for your post.

  21. Tom

    Position: fixed will result in content overlaps when zooming. Is there a way to fix that?

  22. Perfect! Thanks!

  23. balu

    Hi ,

    Nice one

    for example

    .right{position:fixed; top:5px; right:5px; } it will display in right side of webpage..

    .left{position:fixed; top:5px; left:5px; } it will display in left side of webpage..

  24. anon

    As an advertiser I love this feature much more than pop-up adverts, but I have learned from pop-up ads: you have to keep the levels of annoyance below what most people will accept else you will have to stop doing what you want.

    position: fixed is great because you can use it to float commercial messages in people’s faces but they can’t do much about it: browsers have pop-up blockers, but not position:fixed blockers. Even totally amateur sites use this for commercial purposes, floating twitter, FB icons etc. in their visitor’s faces. It seems few people have thought this through, the commercial messages and hype seems to have wonderfully side-tracked people.

    Best of all though is when people use p:f to recreate frames. But even better than frames the keyboard can’t be used properly to scroll the page, floating elements stand to obscure content. But that just makes the web harder to understand, which tends to be good for business. Bad for the people, but who cares about them? Rich and poor are relative after all, and I’m not going to work to get rich. But I’ll try and push others towards poverty.

  25. awesom, thanks a lot.

  26. really a nice tut to move the div…. i have done it by jquery earlier … lol…… now i will move it with the help of css only.

  27. This is super helpful, thank you! I am however having some issue with the:

    sb.css({position: 'fixed', bottom: '10px', right: '10px'});   
    

    It makes the sb (sidebar object from jquery) fatter than it was …

  28. Sharath

    This is helpful, but i have a question on problem caused by fixed position. the next div just overlapps with this one. anyway to fix it?

  29. Position: fixed will result in content overlaps when zooming. Is there a way to fix that?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • I&#8217;m an Imposter

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

  • Resize an Animated GIF

    Animated GIFs are images but you can't really handle them link other types of images, like PNGs or JPEGs or even WebPs.  GIFs are kind of a video file, because they have frames, but there's no real control over how they play or loop.  My favorite image...

  • Mobile Sites Vs. Responsive &#8211; The Day After Google&#8217;s Algorithm Update

    Google recently rolled out a much anticipated update to its Googlebot web crawler. The overall theme of the update centered on mobile web browsing. You've no doubt heard any number of statistics thrown around recently about the swelling user base of mobile browsing. Well, so has...

  • Tweet from Command Line with t

    I've been obsessed with command line magic lately, as you may have noticed.  I don't plan on using every utility I've written about but I'm having fun learning about what's out there and how it's done.  My latest quandary was how one could tweet and perform other Twitter-centric tasks...

  • Web Animation using JavaScript:  Animating Text