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.


	.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).

Track.js Error Reporting

Recent Features

Incredible Demos

  • MooTools History Plugin

    One of the reasons I love AJAX technology so much is because it allows us to avoid unnecessary page loads.  Why download the header, footer, and other static data multiple times if that specific data never changes?  It's a waste of time, processing, and bandwidth.  Unfortunately,...

  • MooTools dwCheckboxes Plugin

    Update / Fix: The checkboxes will no longer toggle when the "mouseup" event doesn't occur on a checkbox. Every morning I wake up to a bunch of emails in my Gmail inbox that I delete without reading. I end up clicking so many damn checkboxes...


  1. sam

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

  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.


    Mohammad Mahmud Kabir


  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

  • OSCON Portland:  Conference  Discount!

    O'Reilly puts on the best web industry conferences in the world.  These conferences include Fluent Conference, Velocity Conference, and the upcoming OSCON in Portland, Oregon from July 20-24.  Open Source Convention (OSCON) is a conference that focuses specifically on open source developers and the tools and possibilities...

  • Follow Redirects with cURL

    I love playing around with cURL. There's something about loading websites via command line that makes me feel like some type of smug hacker, just like tweeting from command line does. I recently cURL'd the Google homepage and saw the following: I found it weird that Google...

  • Developers Have WordPress, Amateurs Have Squarespace, Professional Designers Have the NEW Webydo!

    Web design platforms have traditionally come in one of two varieties. There are the solutions like WordPress and Drupal that are incredibly powerful, but an understanding of web development and coding is required to be able to use those platforms effectively. On the other side of the...

  • Chris Coyierâs Favorite CodePen Demos II

    Hey everyone! Before we get started, I just want to say it’s damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...

  • GSAP + SVG For Power Users: Motion Along A Path

    Now that the GreenSock API is picking up steam, there are many tutorials and Getting Started guides out there to provide good introductions to the library, not to mention GreenSock’s own Forum and Documentation. This article isn’t intended for beginners, but rather a...