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?
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 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.
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.
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.
simply add position: -webkit-sticky;
Adding display: block
to the .sticky-element
worked for me without having to add position: -webkit-sticky
. Found this solution at this Codepen.
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
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>
I had to use below to make it work in both Chrome and Safari:
position: sticky;
position: -webkit-sticky;
display: block;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With