Workaround for a Safari position: sticky (-webkit-sticky) bug
Asked Answered
H

1

7

If you open this Fiddle https://jsfiddle.net/17uwnsq6/4/ in Safari (12.1.2 but should work for all recent versions) and start scrolling down the white scrollable area, at first the sticky "Header" element will remain sticky, but will later scroll off the screen. In Chrome and Firefox it always remains sticky as expected.

HTML and CSS for reference:

<div class="title">Title</div>
<div class="container">
  <div class="header">Header</div>
  <div class="content">Content</div>
</div>
html {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  height: 100%;
  margin: 0;
  padding: 0;
}

.container {
  flex: 1 1 0;
  overflow: auto;
}

.header {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
}

.content {
  height: 2500px;
}

.title {
  flex: 0 0 auto;
  background-color: lightblue;
}

It seems that this bug shows up when the container is sized using flex layout. Does anybody know of a workaround for this issue? The goal is to make the header always sticky, while at the same time to size the container so that it would occupy the part of the viewport left over by the "Title".

Hilarius answered 14/9, 2019 at 10:55 Comment(0)
H
24

I think I've figured it out. The trick is to put the entire children of the scrollable container (that is, the header and the content) into a wrapper div - then the bug isn't triggered.

Hilarius answered 14/9, 2019 at 18:38 Comment(5)
THANK YOU for documenting this. Was beating my head against the wall.Honeywell
Worked for me too! Thanks. What a weird issue.Mats
Thank you for the answer. What a weird bug. Make sure to add comments to your HTML for that so that future developers don't delete the seemingly unnecessary empty div ... :|Eamon
This bug ate my whole day... Good point dv02 -- I referenced this post in my comment :)Shoeshine
It helped! Entire day spent on my side as well. My case is a bit different - several sticky containers work all the time, but suddenly stop working when just anything is appended to the dom (tooltip, context menu, etc). The sticky-to-left container just stops being sticky until mouse move. Wrapping everything into one container with position absolute solved this issue.Thirlage

© 2022 - 2024 — McMap. All rights reserved.