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.
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.
You should follow me on Twitter here I'll tweet about JavaScript, HTML 5 and other such gems (amongst usual tweet-splurges)


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?
> Don%u2019t use
position:fixedinside a scrolling elementI 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
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.
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 !
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.
Great article with some valid conclusions. Though the drifting you described is well documented behavior (from the Apple developer docs):
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?
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.
There’s also a bug with
-webkit-overflow-scrolling: touchwhere pressing the status bar won’t jump the user to the top of the document.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).
Thanks for the post; this helped me immensely.
In my case I was trying to get an
onscrollevent 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
setIntervalorsetTimeoutfrom 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
onscrollevent 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.
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
If you use
-webkit-overflow-scrolling: autoinstead of-webkit-overflow-scrolling: touch, iOS will continuously fire theonscrollevent.The downside is this also disables scroll momentum.
Thanks for the illustrative explanation.
I have a question concerning -webkit-overflow-scrolling Where can I find the documentation to this property? Thanks!
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
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.