Overview
To apply a transparency gradient to the edges of a container (e.g., to indicate more content is available to scroll or to fade out text), use CSS Masking with a linear gradient. This approach is superior to using a semi-transparent overlay because it actually fades the content itself, allowing the background to show through naturally without interfering with text selection or pointer events.
Implementation
To implement a soft edge fade:
Fading the bottom edge of a container
This is useful for indicating that there is more content below in a scrollable area.
.container {
/* Enable scrolling */
overflow-y: auto;
/* MANDATORY: Use vendor prefix for wider support in older browsers */
-webkit-mask-image: linear-gradient(to bottom, black 80%, transparent 100%);
/* Standard property for modern browsers */
mask-image: linear-gradient(to bottom, black 80%, transparent 100%);
}
Fading both top and bottom edges
You can use a single gradient with multiple color stops to fade both edges.
.dual-fade-container {
/* Content is visible between 10% and 90% of the height */
-webkit-mask-image: linear-gradient(to bottom, transparent 0%, black 10%, black 90%, transparent 100%);
mask-image: linear-gradient(to bottom, transparent 0%, black 10%, black 90%, transparent 100%);
}
Fallback strategies
If a browser does not support mask-image or the prefixed version:
- The content will not fade and will display with sharp edges.
- Ensure the interface is still functional and content is readable without the fade (progressive enhancement).
- You can use a semi-transparent overlay as a fallback, but be aware it requires knowing the background color and may interfere with text selection unless
pointer-events: noneis used.
/* Fallback using an overlay for browsers that do not support masking */
@supports (not (mask-image: linear-gradient(to bottom, black, transparent))) and (not (-webkit-mask-image: linear-gradient(to bottom, black, transparent))) {
.container {
position: relative;
}
.container::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 20%;
/* Fallback assumes a solid background color (e.g., white) */
background: linear-gradient(to bottom, rgba(255,255,255,0), rgba(255,255,255,1));
pointer-events: none; /* Allow interaction with text underneath */
}
}