Sticky Labels in an Overflow-X div
I currently have a component like in the picture. When I scroll left/right only the overflow-x div scrolls.
The div contains a list of numbers at the top, that you can see in blue in the second picture
At the moments the numbers are in an absolute overlay like so:
I would like for the numbers to remain sticky to the left border of the div when scrolling to the right. In picture 3 if I scroll more to the right the 1 should stay fixed until the next number.
I assume that hiding the numbers when the next one is close is not something I can (should) do in CSS but I was wondering if there was a way for the elements to stay sticky using CSS.
Anyone has anything I can try?



2 Replies
sticky is exactly what you're looking for I suppose. position: sticky; and left or right: 0; to the number and position: relative; to the whole container that scrolls. These things are quite common for vertical scroll, but work horizontally all the same 🙂
Yeah, just slap a
position: sticky
on it and set the stuck margins with inset