Not able to find the solution for this. Basically, what I am trying to achieve is to have an element with the position: sticky
appear with a scrollbar if there is more content then the browser height.
I don't want to use position: fixed
as it will add additional pain in the neck.
Here is the example of how I would expect it to work:
.placeholder { height: 200vh; width: 50%; float: right; } .child { position: fixed; top: 0; bottom: 0; height: 100%; overflow-y: auto; }
<div class="parent"> <div class="placeholder"></div> <div class="child"> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> </div> </div>
Fiddle 1
Here is example with the position: sticky
where the overflow
doesn't do anything:
.placeholder { height: 200vh; width: 50%; float: right; } .child { position: sticky; top: 0; bottom: 0; height: 100%; overflow-y: auto; }
<div class="parent"> <div class="placeholder"></div> <div class="child"> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> some long text <br /> </div> </div>
Fiddle 2
Any explanation / solution appreciated.
Make sure to use Firefox when testing
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.
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.
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.
An element with position: sticky; is positioned based on the user's scroll position. A sticky element toggles between relative and fixed , depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).
It's important to understand that sticky
elements are first treated like a relative
element and then a fixed
element (see MDN). Therefore, you must first look at it like a relative element. If you give a height of 100% to a relative element, then nothing really happens unless its parent element has a defined height.
If you want your sticky element to have a scrollbar too, you must give it a meaningful height. I suggest using viewport height:
.child { position: sticky; top: 0; bottom: 0; height: 50vh; overflow-y: auto; }
For the record - the "stickiness" doesn't appear to be working as expected in either FF or Safari in terms of the element becoming fixed
during scrolling. I'm not concerning myself with that - just focusing on the overflow issue.
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