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.

EDIT: all browsers except IE now support all mask- properties mentioned here.

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>

Answer from vals on Stack Overflow
🌐
W3Schools
w3schools.com › css › css3_gradients.asp
CSS Gradients
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).
🌐
CSS-Tricks
css-tricks.com › thing-know-gradients-transparent-black
A Thing To Know about Gradients and "Transparent Black" | CSS-Tricks
January 10, 2017 - TL;DR transparent == #0000, not the previous with 0% alpha. ... “CSS Color Level 4” is in progress by WebKit (Safari) https://webkit.org/status/#specification-css-color-level-4 · till then WebKit might fix “Color interpolation for colors with alpha incorrect (gradients)” https://bugs.webkit.org/show_bug.cgi?id=150940, so the color(#eb8fa9 alpha(0%)) approaches might be irrelevant.
Discussions

CSS opacity gradient? - Stack Overflow
@MikhailKhazov The easiest solution ... to the gradient. - that is keeping it opaque till a non 0 percentage. A more difficult approach, but with more different posibilities, is setting more than 1 mask, I have added a snippet showing this posibility 2017-10-25T17:37:09.743Z+00:00 ... Except using css mask answered by @vals, you can also use transparency gradient ... More on stackoverflow.com
🌐 stackoverflow.com
How can I combine a horizontal color gradient with a vertical opacity gradient in CSS3?
You put the opaque gradient, and you fake the transparency on top by making a gradient from transparent to background-color. background-image: linear-gradient(0deg, transparent 0%, white 100%), linear-gradient(90deg, #e89005 0%, #ec7505 33%, #d84a05 66%, #f42b03 100%); Replace white by the background color you need. More on reddit.com
🌐 r/webdev
3
1
June 1, 2022
How to make a CSS border gradient with transparent background?
Hi there, is it possible to create a border gradient with a border-radius and a transparent background? This is a good tutorial to create a border gradient including border-radius. However it is not possible to set a transparent background. Is there any workaround to use a border gradient with ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
November 17, 2022
css - CSS3 Transparency + Gradient - Stack Overflow
RGBA is extremely fun, and so is -webkit-gradient, -moz-gradient, and uh... progid:DXImageTransform.Microsoft.gradient... yeah. :) Is there a way to combine the two, RGBA and gradients, so that th... More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Guides › Images › Using_gradients
Using CSS gradients - MDN Web Docs
This example overlaps three ... either that the colors of the gradients on the top of the stack are partially transparent or use the background-blend-mode CSS property....
Top answer
1 of 5
158

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.

EDIT: all browsers except IE now support all mask- properties mentioned here.

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>

2 of 5
9

Except using css mask answered by @vals, you can also use transparency gradient background and set background-clip to text.

Create proper gradient:

background: linear-gradient(to bottom, rgba(0, 0, 0, 1) 0%, rgba(0, 0, 0, 0) 100%);

Then clip the backgroud with text:

background-clip: text;
color: transparent;

Demo

https://jsfiddle.net/simonmysun/2h61Ljbn/4/

Tested under Chrome 75 under Windows 10.

Supported platforms:

  • https://caniuse.com/mdn-css_properties_background-clip_text
  • https://caniuse.com/css-gradients
🌐
W3Schools
w3schools.com › css › tryit.asp
Linear Gradient - with transparency
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
Polypane
polypane.app › blog › my-take-on-fading-content-using-transparent-gradients-in-css
My take on fading content using transparent gradients in CSS | Polypane
January 19, 2024 - span { display: block; background: linear-gradient(to bottom, white, transparent); color: transparent; -webkit-background-clip: text; background-clip: text; } Make sure to put the prefixed version before the standard declaration. In CSS the last declaration wins, so the standard will overwrite the prefixed version and be used instead
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › css › how-to-set-transparency-with-linear-gradient-in-css
How to Set Transparency with Linear Gradient in CSS ? - GeeksforGeeks
July 23, 2025 - In this approach, by adjusting gradient colors using the background-image property, we can determine how clear or opaque the image appears. This method adds attractiveness to our web designs.
🌐
Reddit
reddit.com › r/webdev › how can i combine a horizontal color gradient with a vertical opacity gradient in css3?
r/webdev on Reddit: How can I combine a horizontal color gradient with a vertical opacity gradient in CSS3?
June 1, 2022 -

For one of my <div> elements, I have the following CSS for a horizonal color gradient:

background: linear-gradient(90deg, #e89005 0%, #ec7505 33%, #d84a05 66%, #f42b03 100%);

Now I want to add a linear vertical opacity gradient on top of this. How would I go about adding a second one?

(as per rule requirements, the context is that I am trying to build a website for my portfolio to get hired, and I tried googling how to add multiple gradients to one block level element in CSS. I also googled how to add opacity gradient with color, but only got results on things like rgba which does it in the same direction.)

🌐
Amit Merchant
amitmerchant.com › fading-content-using-transparent-gradients-in-css
Fading content using transparent gradient in CSS
January 17, 2024 - Now, the trick to make the content fade at the bottom is to use a transparent gradient that sits on top of the text. Here’s what the core CSS for the same looks like.
🌐
Ahmad Shadeed
ishadeed.com › snippet › fake-opacity-gradient
How to Fake Opacity With a CSS Background
To implement that, we can use multiple CSS gradients. Here is how I did it: :root { --oval-w: 50%; --oval-h: 70%; --base-color: rgba(194, 236, 231, 0.8); --pattern: url("hero-bg.svg"); } .hero { min-height: 400px; background: linear-gradient(var(--base-color), var(--base-color)), radial-gradient(#c2ece7 25%, transparent) center/50% 90% no-repeat, var(--pattern) center/cover no-repeat; }
🌐
GitHub
github.com › gilmoreorless › postcss-gradient-transparency-fix
GitHub - gilmoreorless/postcss-gradient-transparency-fix: PostCSS plugin to fix up CSS gradients with transparency for older browsers
A simple definition like linear-gradient(red, transparent) would not only fade the colour from fully opaque to fully transparent, but it would also fade from red to black at the same time.
Starred by 25 users
Forked by 2 users
Languages   JavaScript 94.6% | HTML 5.4% | JavaScript 94.6% | HTML 5.4%
🌐
GitHub
github.com › tailwindlabs › tailwindcss › discussions › 3433
Applying background opacity to a gradient background · tailwindlabs/tailwindcss · Discussion #3433
I often find myself in the same scenario so I found a workaround to get the effect you want. Basically, since the .bg-opacity has no effects on gradients (as well as CSS variables) we can use the regular .opacity combined with some .absolute to get the effect to work.
Author   tailwindlabs
🌐
TutorialsPoint
tutorialspoint.com › css3-transparency-and-gradients
CSS3 Transparency and Gradients
October 31, 2023 - It starts with a color and then moved to being fully transparent − · <!DOCTYPE html> <html> <head> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .linearGradient { height: 200px; background-image: linear-gradient( to right, rgba(255,0,0,0), rgba(0,0,255,1) ); } </style> </head> <body> <h1>Linear Gradient with transparency</h1> <div class="linearGradient"></div> <p>The above linear gradient goes from being complete transparent to blue.</p> </body> </html>
🌐
CodePen
codepen.io › yochans › pen › ZEJNvRd
CSS | Transparent gradient in the image
Minimize CSS Editor · Fold All · Unfold All · body { padding: 0; margin: 10px; } .container { position: relative; max-width: 100%; width: 300px; height: 200px; } .container::after { content: ''; position: absolute; top: 0; left: 0; width: 300px; height: 200px; background-image: linear-gradient(180deg, transparent 0 30%, #FFF 70% 100%); } .container img { width: 100%; } !
🌐
vincoding
vincoding.com › css-transparent-gradient-overlay-image
CSS Transparent Gradient Overlay on an Image | vincoding
August 25, 2017 - Here we just need to add the linear-gradient property within the background style followed by the url property we already had in place. For a solid transparent color overlay we would want to keep the linear-gradient rgba values the same for the start and end.