Issues with position fixed & scrolling on iOS

With the release of iOS 5, fixed positioned layout is said to be supported in MobileSafari.

The word supported needs to be taken with a pinch of salt, because there’s all kinds of issues which I intend to show you in the following post.

Note that I have filed bugs for a number of these during the beta of iOS 5 – but god knows how the Radar Apple thing works, so I don’t know the issue numbers.

Update: I’ve added “scrolling == unusable position:fixed element” based on Corey Duston pointing out more bugs with position fixed.

position:fixed, who cares?

I might have argued that fixed positioned doesn’t matter or isn’t really required in a good app. However, there’s an increasing number of iOS apps I’ve noticed that are actually just a collection of WebViews (mini-MobileSafaris) with fixed position toolbars as seen in Apple’s own AppStore app, the native Facebook app and Instagram below:

AppStore via @devongovett, Facebook via @9eggs

Issues

I’ve created a number of example pages that you can view for yourself, which are used in the following videos.

Juddering

If you add position:fixed in any normal way as you might on a “desktop” site, you’ll see some degree of juddering as the page scrolls.

Note that this is the simulator running, but I’ve also captured the real iPhone (using Reflection) showing the same behaviour.

The page used was: jsbin.com/3/ixewok/6/ (edit)

No updated values on scroll

The sharp eyed viewer might have spotted some values changing in the video. I’m monitoring the window.scrollTop and window.pageYOffset (and another value which we’ll look at later). You’ll notice that the values don’t change until the scroll has finished.

This is a problem if you want to monitor the page position to simulate effects like the bumping and shunting of category headings like you might see in the address book app.

Position drift

If the page is zoomed at all, which you can get in iOS when the user rotates from portrait to landscape, as the user scrolls in any scale beyond 1 (i.e. zoomed), the position fixed element drifts upwards (I’ve seen this drift entirely out of view before in other sites):

The page used was: jsbin.com/3/ixewok/6/ (edit)

Focus jumping

If there’s a focusable element inside the position fixed element, i.e. an input element, this can cause the entire fixed element to jump out of place. This will only happen if the user has scrolled any amount (but if you’re using position:fixed you’re expecting exactly that kind of usage).

The page used was: jsbin.com/3/ixewok/8/ (edit)

Scrolling == unusable position:fixed element

Corey Dutson pointed out there’s another issue with position fixed. Although his example show scrolling using JavaScript, the core problem is: if the page moves programatically (i.e. the user didn’t cause the scroll) the elements inside the fix element are unavailable.

From the screencast I’ve recorded you can see using iWebInspector that although MobileSafari has painted the fixed element in place, it’s actually not there – the actual element remains in place until you touch and move the page again.

The page used was: jsbin.com/3/ixewok/13/ (edit)

I don’t have a fix for this yet, and I suspect it’s a core painting issue inside of MobileSafari – but I will keep playing to see if there’s something that can be done.

Fixing juddering

With iOS 5, MobileSafari also came with -webkit-overflow-scrolling: touch. This is actually intended for inline blocks of content to the page (I mean inline with respect to the document).

If I change the CSS in my previous example, and set the height of my html, body and content block to 100%, then apply the scrolling touch property to the content, the juddering goes away. However, that alone does not fix the juddering.

The trick would seem to be: make sure your fixed position element is not on a “moving canvas”. This example has the fixed element over a scrolling element, but not inside of it.

So when I tried to apply this technique to the body element, the juddering was still visible, as the fixed element was inside the scrolling element.

I also captured this on the real device too.

The page used was: jsbin.com/3/ixewok/10/ (edit)

Getting scroll position to update

Again, those keen eyes might have spotted values are moving again. Note that as I’ve changed the CSS the body is no longer scrolling, so the 0 values on the left and right are window.scrollTop and window.pageYOffset respectively. Since the window isn’t scrolling, the content block is in an overflow, the values won’t change.

However, the content.scrollX value is changing – but it doesn’t by default.

Firstly, you have to attach any touch event handler to get this value to update as the user is scrolling (or actually touching), so in JavaScript I can add:

content.ontouchstart = function () {};

The touch event will work with start, end and move, and just needs a value set (note that I didn’t test just setting it to true – that might work too!).

However, it’s still not perfect. You’ll see from the video above, that it only updated whilst I’m touching. As soon as I let go during a swipe to scroll, the momentum makes the page continue to scroll, but the value doesn’t update.

I’ve yet to work out if it’s even possible to capture this value. ::sigh::

To conclude / TL;DR

Don’t use position:fixed inside a scrolling element, it’s juddery and looks rubbish (I’ve seen much worse than the juddering shown in the videos). Do make use of -webkit-overflow-scrolling: touch and if you want the scroll values, make sure you attach a touch handler to that scrolling element.

At the same time: make this work in other mobile browsers too – don’t just cater to Apple. It’s a huge headache that Apple have half arsed-ed-ly fixed the position:fixed issue and done in a typically Microsoft proprietory way.

29 Responses to “Issues with position fixed & scrolling on iOS”

  1. Interesting findings. What’s your opinion of libraries such as iScroll http://cubiq.org/iscroll-4 that seek to mitigate some of the issues with scrolling on mobile?

  2. > Don%u2019t use position:fixed inside a scrolling element

    I think this does not work per spec (when using translate). I asked the question last month:
    https://twitter.com/thierrykoblentz/status/193074725491187712

    And the answer was: it cannot work

  3. Remy,

    Terrific post – well written, and incredibly well illustrated.

    I found another bug too, that’s at least as problematic…

    Let’s say that you have a tall form, and are using position:fixed to position a header or footer at the top or bottom of the form; the intent of the position:fixed div is to make sure that header/footer element remains visible within the viewport, regardless of where the user is in the form.

    When the user gives focus to an input (text field) in the form, iOS will display the keyboard.

    If you then click the ‘Next’ button several times, you’ll see that as you jump from field to field, iOS scrolls the form to keep your focused input in view.

    However, iOS doesn’t “update” the position of the position:fixed div. Instead of remaining in place (as it should), it moves (scrolls) along with the form, and renders fixed:position useless if it’s used in concert with a form that might scroll as the user skips through the form elements.

    Oddly, once you dismiss the keyboard, the fixed:position div regains it’s rightful place, but by then, the illusion of a “fixed position” is lost.

    Too bad.

  4. Hi !

    Excellent blog post, very interesting, and incredibly well “illustrated” with those shorts videos. Thanks for sharing your finds, it will propably help me get through this kind of trouble sooner or later. :)

    Cya !

  5. There’s also an annoying bug with the initial scroll position of a page whenever you refresh. Let’s say you have a position:fixed element that fully overlays the page, refreshing that page when you are scrolled down will cause the element to be at the top of the screen. Only a touch based scroll or a manual scrollTo with a lot of delay will cause the element to get its intended position.

    Until they fix the problem where position:fixed requires user interaction to kick in, I’ll stick with the good old javascript solutions, at least those are reliable.

  6. Torsten Walter May 25th, 2012 at 2:18 pm

    Great article with some valid conclusions. Though the drifting you described is well documented behavior (from the Apple developer docs):

    “In Safari on iPhone and iPad, the window size [...] cannot be changed by the user. To move around a webpage, the user changes the zoom level and position of the viewport as they double tap or pinch to zoom in or out, or by touching and dragging to pan the page. As a user changes the zoom level and position [...] webpage elements that have their position “fixed” to the viewport can end up outside the viewable content area, offscreen.”

  7. It looks like the heading is fixed but the main content is static (so the scrollbar of the main content disappears into the top of the heading).

    If the heading and content areas are both position fixed, will this jittering and the other problems still occur?

  8. Thx for the investigations!

    Just checked iOS6 beta and it seems like Apple changed the position fixed behaviour.
    I´m building a site with a fixed top navigation and on iOS5 it´s juddering as you described. On iOS6 beta the juddering is gone in this case.
    Funny thing is, the navigation collapses on scroll via CSS transition and this is not looking good on iOS6 beta, the animation is somehow broken. We´ll see how it turns out in the final release.

    Cheers, Jens.

  9. There’s also a bug with -webkit-overflow-scrolling: touch where pressing the status bar won’t jump the user to the top of the document.

  10. Thanks for the info Remy, I knew iOS struggled before with how we tend to expect position: fixed to behave and was surprised when a colleague showed me a working example on iOS 5. However I still had problems with what I was working on. Might be because I needed to alter the CSS dynamically as it only needed to be position: fixed when scrollTop was greater that a certain amount. In the end I couldn’t get it to work and had to fall back to position: absolute and calculate the “top”. Works well enough but also suffers from not updating until after the scrolling is complete (might try the ontouchstart trick).

  11. Thanks for the post; this helped me immensely.

    In my case I was trying to get an onscroll event to behave like desktop browsers on mobile ones (in that the event would be fired constantly as scrolling occurs rather than just when it stops).

    I was not successful but I did discover a few interesting things. For one iOS Safari, Android’s Gingerbread browser, and Opera Mobile (and quite likely some others) all pause any functions queued through setInterval or setTimeout from running until the scrolling action is complete. Firefox for Android, Ice Cream Sandwich, Jelly Bean, and Windows phones all allow them to run as normal.

    Also a lot of mobile browsers have trouble painting changes done in an onscroll event for whatever reason. Anyways I posted what I found on my blog if you’re interested – http://tjvantoll.com/2012/08/19/onscroll-event-issues-on-mobile-browsers/.

    Again thanks for taking the time to write these up.

  12. Here’s a simple fix for “unusable” fixed elements after programmatic scrolling on iOS5: http://14islands.tumblr.com/post/30313367126/solved-position-fixed-scrollto-bug-ios

  13. If you use -webkit-overflow-scrolling: auto instead of -webkit-overflow-scrolling: touch, iOS will continuously fire the onscroll event.

    The downside is this also disables scroll momentum.

  14. Thanks for the illustrative explanation.

    I have a question concerning -webkit-overflow-scrolling Where can I find the documentation to this property? Thanks!

  15. Another issue people will find with iOS fixed position elements is the “first broken scroll” issue, where the first time a user scrolls the page with fixed elements they wont “stick” to where they are meant to be. After that initial “first broken scroll” everything works fine, but that initial bug can make interfaces look choppy and unprofessional.

    I came up with a pretty simple fix on this thread: https://github.com/Simbul/baker/issues/504

  16. Sh*t. I’d just used the tutorial you’d put together here: http://jqueryfordesigners.com/fixed-floating-elements/ and saw it was working perfectly on my desktop, then when I went to use it on my iPhone (the intended audience, mobile users), it didn’t work. Searching for a solution I see this posting.

    Should I just give up trying to lock a div at the top of a mobile device’s screen once the user has scrolled beyond a certain point? I’m a content creator that codes by necessity, so I suck at it.

  17. Does anyone know if this is fixed (no pun intended :-) in IOS7. When I bring up the keyboard when an Input receives focus the position:fixed is useless.

  18. @Morten: No, elements with `position: fixed;` still seem to lose that attribute when an input receives focus in iOS 7. Very frustrating.

  19. I believe I have a good fix for the “long forms with position:fixed on keyboard showing causes fixed footer to stop staying fixed” problem. This is in dojo but it is so simple you can port it to anything. query is just dojo/query and ready is dojo/ready.

    This covers all cases – if I navigate form els via <> or click “done” on an element, or start scrolling.

    
    
    		var allInputs = query('input,textarea,select');
    		var d = document, navEl = "nav";
    		allInputs.on('focus', function(el){
    			 d.getElementById(navEl).style.position = "static";
    		});
    
    		var fixFooter = function(){
    		    if(d.activeElement.tagName == "BODY"){
    		    	d.getElementById(navEl).style.position = "fixed";
    		    }
    		};
    		allInputs.on('blur', fixFooter);
    		var b = d.body;
    		b.addEventListener("touchend", fixFooter );
    }
    
    
  20. I wrote an answer with some details about position:fixed problems I have on iOS5/6/7 – see http://stackoverflow.com/questions/10659891/fixed-position-div-freezes-on-page-ipad/10747648#10747648

    You can try it out at http://jsbin.com/icibaz/3 – just zoom and/or give the input focus.

  21. The juddering is also visible on Chrome Desktop for Windows 7, ever since they started using that so far nasty Blink engine. There is an almost constant gap/slit of 1-3 pixels between the fixed element (navbar), and the top of the viewport. Not only when scrolling. So you can see the content in that gap/slit.

    I wonder if this issue could be resolved by just giving the fixed element an extra 20px height, and a -20px margin-top. So the fixed element slips 20px out of the viewport (at the top). Gives you 20px leeway, sufficient for the most extreme joyride.

  22. @Robert I’d check your add ons. I don’t have that issue or that engine.

  23. Yet another great article, thank you.

    Two remarks:

    I was (and still am) confused by the following phrasing under “Fixing juddering”: “If I (…) the juddering goes away. However, that alone does not fix the juddering.” Ehrr, say what? It fixes it, right? Or doesn’t it…?

    In the penultimate paragraph, you mention: “However, it’s still not perfect. You’ll see from the video above, that it only updated whilst I’m touching. As soon as I let go during a swipe to scroll, the momentum makes the page continue to scroll, but the value doesn’t update.” The fact that “it’s still not perfect” isn’t really a property of the solution (that “it” refers to). It’s just regular behavior of hardware accelerated scrolling — your first demo video shows the exact same behavior.

    Keep up the nice work! Cheers.

  24. Hi.

    Hope you can help me? I’ve been building my responsive portfolio website and it’s almost finished. There’s only one more thing that I need to fix. For some reason I can’t seem to get the scroller to work on mobile devices. I’ve applied the ‘-webkit-overflow-scrolling: touch’ CSS to the elements which require a scroller but it still doesn’t work. I’ve even tried installing scroll related Jquery plugins but again it doesn’t make any difference. Please can you help me to get this to work? If you go to http://www.rbimdesign.co.uk/, the pages that I’m having the issue with is ‘About Me’, ‘My Work’ and the other pages within that section, ‘Contact’ and the ‘Site Map’.

    I don’t know if it’s the plugins I’m using on my website that’s preventing the scroll to work or what? Again if you can help me with this I’d really appreciate it. I’m not really a Front End Developer so you might have to explain in more detail how to get around the issue.

    Cheers.

  25. I have a workaround for the fixed position button issue. Basically if you program a scrollto or scrolltop/scrollleft in javascript, fixed position buttons become useless. This is because the button positions are stuck in ‘absolute’ while the graphics are rendered in ‘fixed’.

    There are many hacks posted that didn’t work for me. What DID work was placing the fixed elements in the body, and then instead of having the body scroll around, you disable scrolling on the body and instead make a div container do the scrolling. (Obviously only for mobile layouts — the desktop version may not render the scroll bars correctly depending on the width of your site.) This will keep the fixed elements from being messed up since the element they’re inside is no longer scrolling around.

    Hope this helps somebody! I couldn’t post to StackOverflow.com because I don’t have enough rep points.

  26. @Httpette – your suggested fix is totally broken:
    1. Go to http://jsbin.com/bepuwila/3
    2. Click the tryfix button to setup the focus/blur event.
    3. Click into the top input – header is not correctly located, footer still doesn’t show.
    4. Click next button on virtual keyboard – page scrolls up and header disappears, and footer is in completely wrong position.

  27. Sorry, link for comment just above should be http://jsbin.com/bepuwila/5

  28. Sorry, link for above should be http://jsbin.com/bepuwila/5

  29. [...] due to its sheer usefulness in modern day web applications is fixed positioning. Its issues are well documented on both iOS and Android. The browser detect required for determining fixed positioning [...]

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight