Multiple backgrounds!

body {
  background: #eb01a5;
  background-image: url("IMAGE_URL"); /* fallback */
  background-image: url("IMAGE_URL"), linear-gradient(#eb01a5, #d13531); /* W3C */
}

These 2 lines are the fallback for any browser that doesn't do gradients. See notes for stacking images only IE < 9 below.

  • Line 1 sets a flat background color.
  • Line 2 sets the background image fallback.

The final line sets a background image and gradient for browsers that can handle them.

  • Line 3 is for all relatively modern browsers.

Nearly all current browsers have support for multiple background images and css backgrounds. See http://caniuse.com/#feat=css-gradients for browser support. For a good post on why you don't need multiple browser prefixes, see http://codepen.io/thebabydino/full/pjxVWp/

Layer Stack

It should be noted that the first defined image will be topmost in the stack. In this case, the image is on TOP of the gradient.

For more information about background layering see http://www.w3.org/TR/css3-background/#layering.

Stacking images ONLY (no gradients in the declaration) For IE < 9

IE9 and up can stack images this same way. You could use this to create a gradient image for ie9, though personally, I wouldn't. However to be noted when using only images, ie < 9 will ignore the fallback statement and not show any image. This does not happen when a gradient is included. To use a single fallback image in this case I suggest using Paul Irish's wonderful Conditional HTML element along with your fallback code:

.lte9 #target{ background-image: url("IMAGE_URL"); }

Background position, sizing etc.

Other properties that would apply to a single image may also be comma separated. If only 1 value is supplied, that will be applied to all stacked images including the gradient. background-size: 40px; will constrain both the image and the gradient to 40px height and width. However using background-size: 40px, cover; will make the image 40px and the gradient will cover the element. To only apply a setting to one image, set the default for the other: background-position: 50%, 0 0; or for browsers that support it use initial: background-position: 50%, initial;

You may also use the background shorthand, however this removes the fallback color and image.

body{
    background: url("IMAGE_URL") no-repeat left top, linear-gradient(#eb01a5, #d13531);
}

The same applies to background-position, background-repeat, etc.

Answer from Gidgidonihah on Stack Overflow
๐ŸŒ
CSS Gradient
cssgradient.io
CSS Gradient โ€“ Generator, Maker, and Background
Because they are of the image data type, gradients can be used anywhere an image might be. The most popular use for gradients would be in a background element. To put it more relevantly, gradients are part of an extremely popular design trend that has been gaining popularity over the last several years.
Gradients
Looking for gradient inspiration? Youโ€™ve come to the right place. Browse our nicely curated collection of beautiful gradients in all colors: blue, red and more.
Color Shades
Color shades are lighter and darker variations of a particular color, created by adding white or black. Explore shades of red, blue, green, yellow, purple and pink.
Backgrounds
Discover the best gradient backgrounds from a curated collection of the ultimate list of gradient sites. With 1000+ gradients, it's easy to find the ๐Ÿ‘Œ color!
CSS Gradient Text
Add eye-catching gradient effects to your website text using pure CSS. Learn how to create vibrant, image-free text gradients with step-by-step syntax examples and tips.
๐ŸŒ
W3Schools
w3schools.com โ€บ css โ€บ css3_gradients.asp
CSS Gradients
A linear gradient requires at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect. background-image: linear-gradient(direction, color-stop1, color-stop2, ...);...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ CSS โ€บ Guides โ€บ Images โ€บ Using_gradients
Using CSS gradients - MDN Web Docs
In the first example above, the lime goes from the 0% mark, which is implied, to the 20% mark, transitions from lime to red over the next 10% of the width of the gradient, reach solid red at the 30% mark, and staying solid red up until 45% through the gradient, where it fades to cyan, being fully cyan for 15% of the gradient, and so on. In the second example, the second color stop for each color is at the same location as the first color stop for the adjacent color, creating a striped effect. In both examples, the gradient is written twice: the first is the CSS Images Level 3 method of repeating the color for each stop and the second example is the CSS Images Level 4 multiple color stop method of including two color-stop-lengths in a linear-color-stop declaration.
Top answer
1 of 16
1806

Multiple backgrounds!

body {
  background: #eb01a5;
  background-image: url("IMAGE_URL"); /* fallback */
  background-image: url("IMAGE_URL"), linear-gradient(#eb01a5, #d13531); /* W3C */
}

These 2 lines are the fallback for any browser that doesn't do gradients. See notes for stacking images only IE < 9 below.

  • Line 1 sets a flat background color.
  • Line 2 sets the background image fallback.

The final line sets a background image and gradient for browsers that can handle them.

  • Line 3 is for all relatively modern browsers.

Nearly all current browsers have support for multiple background images and css backgrounds. See http://caniuse.com/#feat=css-gradients for browser support. For a good post on why you don't need multiple browser prefixes, see http://codepen.io/thebabydino/full/pjxVWp/

Layer Stack

It should be noted that the first defined image will be topmost in the stack. In this case, the image is on TOP of the gradient.

For more information about background layering see http://www.w3.org/TR/css3-background/#layering.

Stacking images ONLY (no gradients in the declaration) For IE < 9

IE9 and up can stack images this same way. You could use this to create a gradient image for ie9, though personally, I wouldn't. However to be noted when using only images, ie < 9 will ignore the fallback statement and not show any image. This does not happen when a gradient is included. To use a single fallback image in this case I suggest using Paul Irish's wonderful Conditional HTML element along with your fallback code:

.lte9 #target{ background-image: url("IMAGE_URL"); }

Background position, sizing etc.

Other properties that would apply to a single image may also be comma separated. If only 1 value is supplied, that will be applied to all stacked images including the gradient. background-size: 40px; will constrain both the image and the gradient to 40px height and width. However using background-size: 40px, cover; will make the image 40px and the gradient will cover the element. To only apply a setting to one image, set the default for the other: background-position: 50%, 0 0; or for browsers that support it use initial: background-position: 50%, initial;

You may also use the background shorthand, however this removes the fallback color and image.

body{
    background: url("IMAGE_URL") no-repeat left top, linear-gradient(#eb01a5, #d13531);
}

The same applies to background-position, background-repeat, etc.

2 of 16
93

If you also want to set background position for your image, than you can use this:

background-color: #444; // fallback
background: url('PATH-TO-IMG') center center no-repeat; // fallback

background: url('PATH-TO-IMG') center center no-repeat, -moz-linear-gradient(top, @startColor, @endColor); // FF 3.6+
background: url('PATH-TO-IMG') center center no-repeat, -webkit-gradient(linear, 0 0, 0 100%, from(@startColor), to(@endColor)); // Safari 4+, Chrome 2+
background: url('PATH-TO-IMG') center center no-repeat, -webkit-linear-gradient(top, @startColor, @endColor); // Safari 5.1+, Chrome 10+
background: url('PATH-TO-IMG') center center no-repeat, -o-linear-gradient(top, @startColor, @endColor); // Opera 11.10
background: url('PATH-TO-IMG') center center no-repeat, linear-gradient(to bottom, @startColor, @endColor); // Standard, IE10

or you can also create a LESS mixin (bootstrap style):

#gradient {
    .vertical-with-image(@startColor: #555, @endColor: #333, @image) {
        background-color: mix(@startColor, @endColor, 60%); // fallback
        background-image: @image; // fallback

        background: @image, -moz-linear-gradient(top, @startColor, @endColor); // FF 3.6+
        background: @image, -webkit-gradient(linear, 0 0, 0 100%, from(@startColor), to(@endColor)); // Safari 4+, Chrome 2+
        background: @image, -webkit-linear-gradient(top, @startColor, @endColor); // Safari 5.1+, Chrome 10+
        background: @image, -o-linear-gradient(top, @startColor, @endColor); // Opera 11.10
        background: @image, linear-gradient(to bottom, @startColor, @endColor); // Standard, IE10
    }
}
๐ŸŒ
AngryTools
angrytools.com โ€บ gradient โ€บ image
Gradient to Image maker
Multilayer gradient generator provides gradient with layers support, It also create PNG transparent images, base64 image code and CSS with linear and radial orientation. Layer container: In this tool page the top left corner is a layer container. Layers are like sheets of stack where you can ...
๐ŸŒ
The Burning Monk
theburningmonk.com โ€บ home โ€บ how to add a subtle gradient on top of an image using css
How to add a subtle gradient on top of an image using CSS | theburningmonk.com
July 30, 2022 - .overlay::before { content: ''; z-index: 10; width: 100%; height: 100%; display: block; position: absolute; background: linear-gradient(0deg, rgba(0, 0, 0, 0.9) 0%, rgba(0, 0, 0, 0) 50%); } Notice that content is set to ''. Unfortunately, if content is not set, then the pseudo ::before element would not be created. The width, height and position settings ensure that the pseudo ::before element completely overlaps with the <img> tag underneath it (along the z-axis). You can achieve the same result using additional <div>โ€˜s but I found this to be a very nice technique as it relies entirely on CSS.
๐ŸŒ
Web Dev Etc
webdevetc.com โ€บ blog โ€บ how-to-add-a-gradient-overlay-to-a-background-image-using-just-css-and-html
How to add a gradient overlay to a background image using just CSS and HTML - Web dev etc - my software development blog
However, the easiest way to do it is to just add another parameter to the background-image css rule. Take a look! ... #show_bg_2 { background-image: linear-gradient(to bottom, rgba(245, 246, 252, 0.52), rgba(117, 19, 93, 0.73)), url('images/background.jpg'); width: 80%; height: 400px; ...
Find elsewhere
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ CSS โ€บ Reference โ€บ Values โ€บ gradient โ€บ linear-gradient
linear-gradient() - CSS - MDN Web Docs - Mozilla
The linear-gradient() CSS function creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the data type, which is a special kind of .
๐ŸŒ
Peter
peter.coffee โ€บ how-to-use-css-pseudo-elements-to-add-a-gradient-to-images
How to add gradients to images with CSS ::after โ€” Peter Ramsing
July 23, 2016 - ::after ยท I created a CodePen but thought I'd explain some things here. You need to surround the <img /> tag with a <div>. This is because presently, the CSS spec doesn't specify how exactly the ::after interacts with replaced elements and the img is a replaced element.
Top answer
1 of 4
89

With z-index :

You may use a container and put the gradient on that container. Then use a negative z-index to position image behind the gradient.

.pickgradient {
  display:inline-block;
  background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.65) 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* IE10+ */
  background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* W3C */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00000000',GradientType=0 ); /* IE6-9 */
}

img{
  position:relative;
  z-index:-1;
  display:block;
  height:200px; width:auto;
}
<div class="pickgradient">
  <img src="http://i.imgur.com/HDssntn.jpg" />
</div>


With a pseudo element :

As commented, you can also use a pseudo element with the gradient and absolute positioning to put the gradient over the image :

.pickgradient{
  position:relative;
  display:inline-block;
}
.pickgradient:after {
  content:'';
  position:absolute;
  left:0; top:0;
  width:100%; height:100%;
  display:inline-block;
  background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.65) 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* IE10+ */
  background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* W3C */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00000000',GradientType=0 ); /* IE6-9 */
}

img{
  display:block;
  height:200px;width:auto;
}
<div class="pickgradient">
  <img src="http://i.imgur.com/HDssntn.jpg" />
</div>

2 of 4
37

For 2020, mask-image can work well. It works in modern browsers (not IE, -webkit- prefix in many browsers currently). https://caniuse.com/#feat=css-masks

img {
   height: 200px;
   width: auto;
   mask-image: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%);
   -webkit-mask-image: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%);
}
   <img src="http://i.imgur.com/HDssntn.jpg" />

๐ŸŒ
Coding For Everybody
codingforeverybody.com โ€บ snippets โ€บ css-gradient-over-a-background-image
CSS Gradient Over a Background Image - Coding For Everybody
This is what it looks like before we apply a CSS gradient. ... .your-background { background: linear-gradient(180deg, rgba(0,0,0,0) 0%, rgba(0,0,0,0.90) 100%), url(/path/to/your/image.jpg); }
๐ŸŒ
ColorSpace
mycolor.space โ€บ gradient
ColorSpace - CSS Gradient Color Generator
CSS Code: background-image: linear-gradient(to right top, #051937, #004d7a, #008793, #00bf72, #a8eb12);
๐ŸŒ
WebGradients
webgradients.com
WebGradients - Free CSS Gradients for Backgrounds and UI by itmeo
WebGradients is a free collection of 180 linear gradients that you can use as content backdrops in any part of your website. Easy copy CSS3 crossbrowser code and use it in a moment! We've also prepared a .PNG version of each gradient.
๐ŸŒ
W3Docs
w3docs.com โ€บ css
How to Add Both a Background Image and CSS3 Gradient to the Same Element
You can combine a background-image and CSS3 gradient on the same element by using several backgrounds. In our example below, we set the height and width of our <div> and add a background. Then, we set the background image fallback using the โ€œurlโ€ value of the background-image property. Finally, we set a background image and gradient for browsers that can deal with them. Also, set the background-size to ...
๐ŸŒ
CSS-Tricks
css-tricks.com โ€บ css-gradients-from-image
CSS Gradients from Image | CSS-Tricks
April 26, 2011 - Nicole Sullivan thinks โ€œwouldnโ€™t it be a nice if there was a tool to convert an image of a gradient into CSS code?โ€ The community responds with some newly-created tools to do just that.
๐ŸŒ
Cloudinary
cloudinary.com โ€บ home โ€บ how to use css gradient over image to make an impact
How to Use CSS Gradient Over Image To Make an Impact | Cloudinary
January 14, 2026 - Drawing focus to call-to-action buttons: A glow-like radial background behind a button can make it stand out on the page. Emphasizing profile images: Circular or elliptical gradients behind avatars add visual depth and focus. Designing artistic or abstract backgrounds: Combining multiple radial gradients can produce vibrant, textured visual effects. While CSS offers impressive control, Cloudinary adds a layer of automation and transformation that makes gradient overlays more dynamic, scalable, and responsive.
๐ŸŒ
Tailwind CSS
tailwindcss.com โ€บ docs โ€บ background-image
background-image - Backgrounds - Tailwind CSS
Use the bg-none utility to remove an existing background image from an element: ... Use utilities like bg-linear-[<value>] and from-[<value>] to set the gradient based on a completely custom value: <div class="bg-linear-[25deg,red_5%,yellow_60%,lime_90%,teal] ..."> <!-- ... --></div> For CSS variables, you can also use the bg-linear-(<custom-property>) syntax: