Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari position:sticky not working in an overflow:auto element

According to CanIUse, there is a known issue with Safari and position:sticky inside an overflow:auto element:

A parent with overflow set to auto will prevent position: sticky from working in Safari

However, this is the exact use case that I need. I have a scrollable div, which is subdivided into two columns. The right column should be sticky and never move, even when the entire div is scrolled. The reason I'm using position:sticky on the right column is that I want the user to be able to still scroll the left column with the cursor on the right column. And this was the only solution that I found to have worked.

A working example for Firefox / Chrome is here: http://cssdeck.com/labs/zfiuz4pc The orange area remains fixed while scrolling, but in Safari it doesn't.

The example above has some unnecessary wrappers to my issue, but I wanted to replicate as closely as possible the environment where I want to have this code working in. The basic gist of it is I have this:

<div class="modal-content">
  <div class="left-content">
  </div>
  <div class="sticky-element">
  </div>
</div>

And the CSS:

.modal-content {
  display: flex;
  overflow: auto;
  flex-flow: row nowrap;
}

.left-content {
  flex: 0 0 300px;
}

.sticky-element {
  position: sticky;
  top: 0;
  right: 0;
  width: 200px;
}

Again, this works in FF/Chrome but not in Safari. Is there a workaround to get it to work in all browsers? Or is there a different approach I can use to maintain scrollability even with the mouse cursor over the sticky element?

like image 792
accelerate Avatar asked Aug 10 '18 19:08

accelerate


People also ask

Why does sticky not work with overflow?

If you've ever tried sticky positioning, then you have probably wondered why CSS position: sticky is not working for your web design. It's because an ancestor element has one of the following values for the overflow property: hidden, scroll, or auto.

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.

Why is my position sticky not working?

That can happen for many reasons: 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.

Does position sticky work on Safari?

Here's how to make position sticky work on Safari and Safari mobile. Follow this simple checklist: Add position : sticky to the element that's going to be sticky. Add top: 100px to the sticky element.


6 Answers

simply add position: -webkit-sticky;

like image 184
eledgaar Avatar answered Sep 18 '22 17:09

eledgaar


Adding display: block to the .sticky-element worked for me without having to add position: -webkit-sticky. Found this solution at this Codepen.

like image 20
Binita Gupta Avatar answered Sep 18 '22 17:09

Binita Gupta


I got this solution from someone else:

http://cssdeck.com/labs/bu0nx69w

Basically, instead of position:sticky, use position:fixed for the right panel. The key is to also you will-change:transform in a parent div (in the above example, in .modal-content) so position:fixed becomes fixed relative to that parent, and not the viewport. It's a neat little trick

like image 44
accelerate Avatar answered Sep 21 '22 17:09

accelerate


I had a similar case:

<div scroll>
    <div sticky />
    <list />
</div>

Just wrap the scroll content with a div worked like a charm:

<div scroll>
    <div>
       <div sticky />
       <list />
    </div>
</div>
like image 27
nelson eldoro Avatar answered Sep 21 '22 17:09

nelson eldoro


I had to use below to make it work in both Chrome and Safari:

position: sticky;
position: -webkit-sticky;
display: block;
like image 45
Ankush Jain Avatar answered Sep 18 '22 17:09

Ankush Jain


position: sticky will not work, if your parent element has overflow: hidden. having this sticky setting, it will have automatically transform your element from relative to actual fixed to the top border of the document.

like image 35
Tomi Avatar answered Sep 20 '22 17:09

Tomi