Books

Thursday, March 20, 2014

When CSS position: sticky doesn’t work.

I spent a huge chunk of time at work yesterday trying to figure out why the hell webkits implementation of position: sticky, position: -webkit-sticky wasn’t working.

Applying it on the tablet site I was working on had no effect but when I viewed a demo site: http://html5-demos.appspot.com/static/css/sticky.html it works perfectly; so it was not a case of my browser not supporting the feature, It was definitely something in the markup or css that was screwing things up.

After some couple of hours hunting about, I found the culprit:

#bodyconstraint {
overflow: hidden !important;
}

A parent element has got overflow:hidden, and this, apparently throws position: -webkit-sticky off.

So in case you run into a situation where position: -webkit-sticky isn’t working, an heads-up: check to see if the element you want to position as sticky is not a child of a parent element that has had its overflow set to hidden.

Also, you need to set the top property of the element you want to position sticky. if not, it doesn't work too.

28 comments:

  1. Also should not use `body { height: 100% }`.

    ReplyDelete
  2. Thank you! I was in a similar situation yesterday, this made my morning!

    ReplyDelete
  3. Thanks for this. Saved me some serious time ☺

    ReplyDelete
  4. Specifically, it seems to be overflow-x that can't be set to hidden. Two other catches that I've found: 1) you must set top to some value and 2) once a sticky element's parent goes out of view, the element will no longer stick.

    ReplyDelete
  5. Thanks a lot man! This helped me to solve the same problem!

    ReplyDelete
  6. Thanks a lot man! It helped me to solve the same issue!

    ReplyDelete
  7. Huge thanks! It solved same issue on my side!

    ReplyDelete
  8. Anonymous10:57 PM

    Thank you! Huge time saver!

    ReplyDelete
  9. I helped. Thanks!

    ReplyDelete
  10. Anonymous11:18 AM

    Many thanks! Saved a lot of head scratching! :)

    ReplyDelete
  11. Anonymous8:45 AM

    This is still not working in IE and Firefox. :(

    ReplyDelete
  12. Anonymous1:33 AM

    Thanks for this, overflow:hidden on a parent was totally my issue.

    ReplyDelete
  13. Also... if the div you set to sticky is wrapped in an anchor tag, that will break the stickiness on Safari too.

    ReplyDelete
  14. Anonymous6:18 PM

    Saved me! Thanks!

    ReplyDelete
  15. I'm so furious about the sticky position.

    It's not just that its parent element can't be set to overflow:hidden, but NEITHER of its parents can have the overflow:hidden.

    What's even worse is that you can't just overwrite it from hidden to visible, but the whole overflow tag MUST be completely absent, otherwise it also won't work.

    ReplyDelete
  16. Saved me! Thanks!

    Seems like we need to make sure all the parent elements are not having the overflow: hidden set.
    Correct me if i am wrong.

    ReplyDelete
  17. Is there a reasonable explanation for this :D ??? Anyway. Thank you VERY much

    ReplyDelete
  18. Whoa.. 5 years later. This still helped someone.. me (a)

    ReplyDelete
  19. ashish jat1:04 PM

    thanx bro, you saved me from wasting so many hours in finding the right solution.

    ReplyDelete
  20. Cara, muito obrigado, esta merda fudeu a minha mente, você me ajudou muito.

    ReplyDelete
  21. Can't believe this post saved me today!

    ReplyDelete
  22. Anonymous10:29 PM

    saved a lot of work!..thanks :)

    ReplyDelete
  23. Anonymous8:09 PM

    with overflow-x: hidden also not working

    but thanks a lot that

    ReplyDelete
  24. Thank you for the solution, In my case, it's body overflow property not mentioned.

    ReplyDelete
  25. Anonymous6:19 PM

    Legend

    ReplyDelete
  26. Anonymous3:11 AM

    You, sir, are a hero! A huuuuuuuge thanks for sharing the solution.

    ReplyDelete
  27. Anonymous12:00 PM

    What is the workaround of you do need for example
    overflow-x: hidden; /* Disable horizontal scroll */
    on your parent element?

    ReplyDelete
  28. Anonymous3:54 PM

    In my case... also when parents have overflow:auto. Very odd!

    ReplyDelete