2.6 KiB
2.6 KiB
Overflow scroll gradient
Adds a fading gradient to an overflowing element to better indicate there is more content to be scrolled.
HTML
<div class="overflow-scroll-gradient">
<div class="overflow-scroll-gradient__scroller">
Content to be scrolled
</div>
</div>
CSS
.overflow-scroll-gradient {
position: relative;
}
.overflow-scroll-gradient::after {
content: '';
position: absolute;
bottom: 0;
width: 300px;
height: 25px;
background: linear-gradient(rgba(255, 255, 255, 0.001), white); /* transparent keyword is broken in Safari */
pointer-events: none;
}
.overflow-scroll-gradient__scroller {
overflow-y: scroll;
background: white;
width: 300px;
height: 250px;
padding: 15px 0;
line-height: 1.2;
text-align: center;
}
Demo
Content to be scrolled
Explanation
position: relativeon the parent establishes a Cartesian positioning context for psuedo-elements.::afterdefines a pseudo element.background-image: linear-gradient(...)adds a linear gradient that fades from transparent to white (top to bottom).position: absolutetakes the pseudo element out of the flow of the document and positions it in relation to the parent.width: 300pxmatches the size of the scrolling element (which is a child of the parent that has the pseudo element).height: 25pxis the height of the fading gradient psuedo-element, which should be kept relatively small.bottom: 0positions the pseudo-element at the bottom of the parent.pointer-events: nonespecifies that the psuedo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.
Browser support
✅ No caveats.