The background-image CSS property sets one or more background images on an element.
Try it
The background images are drawn on stacking context layers on top of each other. The first layer specified is drawn as if it is closest to the user.
The borders of the element are then drawn on top of them, and the background-color is drawn beneath them. How the images are drawn relative to the box and its borders is defined by the background-clip and background-origin CSS properties.
If a specified image cannot be drawn (for example, when the file denoted by the specified URI cannot be loaded), browsers handle it as they would a none value.
Note: Even if the images are opaque and the color won’t be displayed in normal circumstances, web developers should always specify a background-color . If the images cannot be loaded—for instance, when the network is down—the background color will be used as a fallback.
Syntax
background-image:linear-gradient( to bottom,rgba(255, 255, 0, 0.5),rgba(0, 0, 255, 0.5)),url("catfront.png");/* Global values */background-image: inherit;background-image: initial;background-image: revert;background-image: revert-layer;background-image: unset;
Each background image is specified either as the keyword none or as an value.
To specify multiple background images, supply multiple values, separated by a comma.
Values
Is a keyword denoting the absence of images.
Accessibility concerns
Browsers do not provide any special information on background images to assistive technology. This is important primarily for screen readers, as a screen reader will not announce its presence and therefore convey nothing to its users. If the image contains information critical to understanding the page’s overall purpose, it is better to describe it semantically in the document.
Formal definition
Initial value
none
Applies to
all elements. It also applies to ::first-letter and ::first-line .
Inherited
no
Computed value
as specified, but with url() values made absolute
Animation type
discrete
Formal syntax
Examples
Layering background images
Note that the star image is partially transparent and is layered over the cat image.
HTML
div>pclass="catsandstars">This paragraph is full of catsbr/>and stars.p>p>This paragraph is not.p>pclass="catsandstars">Here are more cats for you.br/>Look at them!p>p>And no more.p>div>
CSS
p font-size: 1.5em;color: #fe7f88;background-image: none;background-color: transparent;>divbackground-image:url("mdn_logo_only_color.png");>.catsandstarsbackground-image:url("startransparent.gif"),url("catfront.png");background-color: transparent;>
Result
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Implementing image sprites in CSS
Image-related data types: ,
Image-related functions:
cross-fade()
element()
image()
image-set()
linear-gradient()
radial-gradient()
conic-gradient()
repeating-linear-gradient()
repeating-radial-gradient()
repeating-conic-gradient()
paint()
url()
Found a content problem with this page?
This page was last modified on Jul 20, 2023 by MDN contributors.
Often, we have background images that we put text on top of. An example could be a hero section, or the above-the-fold content on basically any marketing site these days. Some times, we need to improve the contrast between the text and the background image. Sure, we could just change the image itself — but some times that’s not an option.
The old and clunky way 👴
There are several ways to solve this, but this is how I learned to do it back in the days. I typically create the following HTML structure:
I would then make the image-box relatively positioned, all children absolutely positioned inside, and stack them in the order I would like.
What is this —syntax?
Note that we’re passing in the image url via something called CSS Custom properties. You might also know them as CSS variables. It’s a way to pass values between our HTML and CSS. You can read more about CSS Custom properties on MDN.
/* The container box is relative so we can position stuff inside of it */.image-boxposition:relative;>/* The background and overlay need to be absolutely positioned */.image-box__background,.image-box__overlayposition:absolute;left:0;top:0;right:0;bottom:0;>/* The background image div sizes and positions the background itself. It's also at the bottom-most position in our "div stack" (z-index 1) We set the image url via a CSS custom property, that's set via the style attribute in our HTML */.image-box__backgroundbackground:var(--image-url)centercenterno-repeat;background-size:cover;z-index:1>/* The overlay div is just a colored element with some opacity. It's above the background image in our stack, so it appears to darken the image */.image-box__overlaybackground:rgba(0,0,0,0.5);z-index:2;>/* The content div is at the top of our stack. We'd probably add some padding or flexbox properties here as well, to place the content appropriately */.image-box__contentposition:relative;z-index:3;/* Finally, style and place the content */color:white;min-height:100vh;display:flex;align-items:center;justify-content:center;>
The new cool way! 😎
That was a lot code. Turns out, it doesn’t have to be that way. Let’s change our HTML to look like this:
.image-box/* Here's the trick */background:linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)),var(--image-url)centercenter;background-size:cover;/* Here's the same styles we applied to our content-div earlier */color:white;min-height:50vh;display:flex;align-items:center;justify-content:center;>
The first background image is a linear gradient that goes from and to the same color. That color is a semi-transparent black, which works as an overlay for your second background. And that’s it really. If you’re feeling clever, you could also pass in the amount of darkening you’d want as a second css variable, for further customization. Or use an actual gradient to make your images pop a bit more.
Using box shadow to achieve the same
Turns out, CSS has several ways of layering «meta-content» on top of a background image. Another way to achieve the same is by using the box-shadow property with a huge spread value and the inset setting.
.image-box/* Here's the trick */box-shadow:inset000100vwrgba(0,0,0,0.5);/* Basic background styles */background:var(--image-url)centercenterno-repeat;background-size:cover;/* Here's the same styles we applied to our content-div earlier */color:white;min-height:50vh;display:flex;align-items:center;justify-content:center;>
Here’s a CodePen with this implementation as well: This gives you something we can animate as well (notice what happens when you hover the image), which can be a nice UX delight. You don’t have the same control over the gradient, however, so which technique you should choose is depending on the context of your design. It’s also been noted that this technique might not be as good for performance, especially on lower end devices. Remember to consider this as well when deciding on your technique. Thanks for coming to my DEV talk.
By using CSS, We can easily overlay an image over another image. Adding an image to another bigger image, Just like you have seen on YouTube video thumbs — A play button is displayed on the top of the video thumbnail.
In this tutorial, I will guide you through two different methods to place an image over another image. Placing one photo with another is very easy with the help of the position CSS element.
Both of these methods use almost the same technique but have different markups.
You can easily position the top image by changing the position element values. Also, You can add a play button or any other image to over another.
If you want to add a play button over the image, It’s better to use a semi-transparent PNG graphic with size (e.g. 64 x 64).
Method 1: Overlay Image Over Image using Background
The first method of overlay an image over another is by defining it as a background in CSS. Let’s first take a look the HTML code.
We have a wrapper div which have an image and empty span tag. This image is our first image and we added a span tag to add a second image through CSS. This will a video play button.
Now the CSS is also simple like HTML. We have positioned the wrapper to display correctly.
Then place the background image first as relative so that the div knows how big it should be.
For image, We set width 100% and height auto so it is responsive and should resize properly.
Here is a game of the second image. First, We place the overlay image as absolutes to the upper left of the first image. We set all it’s element value to zero to align it properly.
Again, We added width and height but this time, We set both element values to 100% to make work on mobile.
We also need to add z-index to this second image show above the first one.
We did add a background image with two rules. The first, It should not repeat and the second, It should position center.
Method 1: Place an image Over an image
In the second method, We will not define a second image as a background in the CSS. But we will place both images side by side on the HTML page.
Just like this, We place both images inside a container. Both have unique classes but remember, The parent-img-responsive class only use to make sure, Image work on mobile.