My position: sticky element isn't sticky when using flexbox
Since flex box elements default to stretch
, all the elements are the same height, which can't be scrolled against.
Adding align-self: flex-start
to the sticky element set the height to auto, which allowed scrolling, and fixed it.
Currently this is supported in all major browsers, but Safari is still behind a -webkit-
prefix, and other browsers except for Firefox have some issues with position: sticky
tables.
.flexbox-wrapper {
display: flex;
overflow: auto;
height: 200px; /* Not necessary -- for example only */
}
.regular {
background-color: blue; /* Not necessary -- for example only */
height: 600px; /* Not necessary -- for example only */
}
.sticky {
position: -webkit-sticky; /* for Safari */
position: sticky;
top: 0;
align-self: flex-start; /* <-- this is the fix */
background-color: red; /* Not necessary -- for example only */
}
<div class="flexbox-wrapper">
<div class="regular">
This is the regular box
</div>
<div class="sticky">
This is the sticky box
</div>
</div>
JSFiddle showing the solution
In my case, one of the parent containers had overflow-x: hidden;
applied to it, which will break position: sticky
functionality. You'll need to remove that rule.
No parent element should have the above CSS rule applied to it. This condition applies to all parents up to (but not including) the 'body' element.
You can also try adding a child div to the flex item with the contents inside and assign position: sticky; top: 0;
to that.
That worked for me for a two column layout where the contents of the first column needed to be sticky and the second column appear scrollable.