Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 430
Ivan Avatar asked Sep 14 '19 10:09

Ivan


People also ask

Does position sticky work on Safari?

CSS position:sticky is Fully Supported on Safari 13, which means that any user who'd be accessing your page through Safari 13 can see it perfectly.

Why is position sticky not working?

Troubleshooting position sticky Position sticky will most probably not work if overflow is set to hidden, scroll, or auto on any of the parents of the element. Position sticky may not work correctly if any parent element has a set height. Many browsers still do not support sticky positioning.

What does Webkit sticky mean?

position: sticky is a new way to position elements and is conceptually similar to position: fixed . The difference is that an element with position: sticky behaves like position: relative within its parent, until a given offset threshold is met in the viewport.

How do you make position sticky work with the overflow property?

How to Make position: sticky Work With the overflow Property? By specifying a height on the overflowing container, you should be able to make position: sticky work whilst having the container element have the overflow property set.


1 Answers

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.

like image 188
Ivan Avatar answered Sep 30 '22 19:09

Ivan