Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to fade-out the end of a text line in CSS?

Tags:

css

I've seen various examples of how to fade out a line of text in CSS. However, these all involve a gradient overlay which matches the background colour. Usually this might be white for example, where the background is also white.

However, if you have a gradient based background with some colour for example, the fade doesn't work in this approach. I can not find any other methods to achieve this desired look.

Is there not a way to fade out text to become transparent itself in a gradient method applied to it directly?

like image 650
Jquestions Avatar asked Aug 23 '17 21:08

Jquestions


People also ask

How do you add an animation to fade in CSS?

You can use fade-out animation on images. In this case, you can make an image go from visible to transparent. To accomplish this, you need to use the CSS opacity property. Through CSS opacity, you can define how transparent or opaque an element is.


2 Answers

Is something like this what you're looking for?

div {
  background: linear-gradient(to bottom right, red, yellow);
}
h2 {
    background-image: linear-gradient(90deg,#000000 0%,rgba(0,0,0,0));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    position:relative;
    display:inline-block;
    font-size: 50px;
}
<div>
  <h2>
    test test test test test
  </h2>
</div>
like image 159
A. W. Avatar answered Oct 14 '22 12:10

A. W.


There is a sleek solution which uses mask-image. Yes, it uses a linear-gradient(), however, because it works with pure transparencies, no knowledge of the background color is necessary:

div {
  white-space: nowrap;
  -webkit-mask-image: linear-gradient(to right, rgba(0,0,0,1) 90%, rgba(0,0,0,0));
}

Works for me in Chrome and caniuse for mask-image looks not bad.

like image 5
flori Avatar answered Oct 14 '22 11:10

flori