I have a text box. Height is set to 4em and I needed it to fade out at the bottom where the text overflows. My solution (tested in Chrome 63 and Firefox 57):
.fade-text .fade-text-gradient::before {
mix-blend-mode: screen;
content: '';
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: linear-gradient(180deg,transparent 50%, #fff 100%);
pointer-events: none;
}
.fade-text .fade-text-gradient {
display: inline-block;
position: relative;
color: #000;
background: #fff;
mix-blend-mode: multiply;
}
Example of use:
<div class="fade-text">
<div class="fade-text-gradient" style="height:4em;overflow:hidden">
<span>Insert text here - enough to overflow the div</span>
</div>
</div>
[EDIT] Even better support in browsers:
The above does not work in Microsoft Edge, so I did some more digging. This is tested in Microsoft Edge, Firefox, Chrome, Safari on iOS and Samsung Internet on Android. It works and is even simpler:
.fade-text {
background-image: linear-gradient(180deg, #000 70%, transparent 100%);
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}
Example of use:
<div class="fade-text" style="width:150px;height:150px;overflow:hidden">
Put enough text here to overflow the div
</div>
The text fades to transparent and therefore you can put any background color behind it. But don't add background color directly, instead put another div around it with the background color set.