Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 opacity gradient?

Tags:

css

I am looking to create an effect like this, but my website has a dynamic background-color. Note that this example uses a white overlay, which does not work with different backgrounds.

p {     width: 300px;     overflow: hidden;     height: 50px;     line-height: 50px;     position: relative; } p:after {     content: "";     width: 100px;     height: 50px;     position: absolute;     top: 0;     right: 0;     background: linear-gradient(90deg, rgba(255,255,255,0), rgba(255,255,255,1)); } 

What I was hoping to do was to set up a CSS opacity gradient. This sort of works, but the code is too messy. Looking at this second example, I could implement it in jQuery, but is there any way to do this entirely in CSS?

like image 812
pgee70 Avatar asked Mar 24 '13 10:03

pgee70


People also ask

How do I set the opacity of a gradient in CSS?

To add transparency, we use the rgba() function to define the color stops. The last parameter in the rgba() function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1 indicates full color (no transparency).

What is css3 gradient?

CSS gradients let you display smooth transitions between two or more specified colors. CSS defines three types of gradients: Linear Gradients (goes down/up/left/right/diagonally) Radial Gradients (defined by their center) Conic Gradients (rotated around a center point)

How do you add gradient to text in CSS?

To add a gradient overlay to a text element, we need to set three different CSS properties to the text we want to style: background-image: <gradient> background-clip: text. text-fill-color: transparent.

What is Webkit gradient?

First, -webkit-gradient uses a two-point syntax that lets you explicitly state where a linear gradient starts and ends. linear-gradient does away with this in favor of convenient box-filling behavior. If you really want the gradient to stop before the edges of the box, you can do so via color stop placement.


1 Answers

You can do it in CSS, but there isn't much support in browsers other than modern versions of Chrome, Safari and Opera at the moment. Firefox currently only supports SVG masks. See the Caniuse results for more information.

CSS:

p {     color: red;     -webkit-mask-image: -webkit-gradient(linear, left top, left bottom,      from(rgba(0,0,0,1)), to(rgba(0,0,0,0))); } 

The trick is to specify a mask that is itself a gradient that ends as invisible (thru alpha value)

See a demo with a solid background, but you can change this to whatever you want.

DEMO

Notice also that all the usual image properties are available for mask-image

p  {    color: red;    font-size: 30px;    -webkit-mask-image: linear-gradient(to left, rgba(0,0,0,1), rgba(0,0,0,0)), linear-gradient(to right, rgba(0,0,0,1), rgba(0,0,0,0));    -webkit-mask-size: 100% 50%;    -webkit-mask-repeat: no-repeat;    -webkit-mask-position: left top, left bottom;    }    div {      background-color: lightblue;  }
<div><p>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text </p></div>

Now, another approach is available, that is supported by Chrome, Firefox, Safari and Opera.

The idea is to use

mix-blend-mode: hard-light; 

that gives transparency if the color is gray. Then, a grey overlay on the element creates the transparency

div {    background-color: lightblue;  }    p {    color: red;    overflow: hidden;    position: relative;    width: 200px;    mix-blend-mode: hard-light;  }    p::after {    position: absolute;    content: "";    left: 0px;    top: 0px;    height: 100%;    width: 100%;    background: linear-gradient(transparent, gray);    pointer-events: none;  }
<div><p>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text </p></div>
like image 139
vals Avatar answered Sep 29 '22 11:09

vals