The background-image
property in CSS applies a graphic (e.g. PNG, SVG, JPG, GIF, WEBP) or gradient to the background of an element.
There are two different types of images you can include with CSS: regular images and gradients.
Images
Using an image on a background is pretty simple:
body {
background: url(sweettexture.jpg);
}
The url()
value allows you to provide a file path to any image, and it will show up as the background for that element.
You can also set a data URI for the url()
. That looks like this:
body {
/* Base64 encoded transparent gif */
background: url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7);
}
This technique removes one HTTP request, which is a good thing. But, there are a number of downsides, so before you start replacing all of your images make sure you consider all the pros and cons of Data URIs.
You can also use background-image
to sprite images, which is another useful method for reducing HTTP requests.
Gradients
Another option when using backgrounds is to tell the browser to create a gradient. Here’s a super-duper simple example of a linear gradient:
body {
background: linear-gradient(black, white);
}
You can also use radial gradients:
body {
background: radial-gradient(circle, black, white);
}
Technically speaking, gradients are just another form of background image. The difference is that the browser makes the image for you. Here’s an entire guide on how to make and use them.
The example above uses only one gradient, but you can also layer multiple gradients on top of each other. There are some pretty amazing patterns you can create using this technique.
Setting a fallback color
If a background image fails to load, or your gradient background is viewed on a browser that doesn’t support gradients, the browser will look for a background color as a fallback. You can specify your fallback color like this:
body {
background: url(sweettexture.jpg) blue;
}
Multiple background images
You can use multiple images, or a mixture of images and gradients, for your background. Multiple background images are well-supported now (all modern browsers and IE9+ for graphic images, IE10+ for gradients).
When you’re using multiple background images, be aware that there’s a somewhat counter-intuitive stacking order. List the image that should be at the front first, and the image that should be at the back last, like this:
body {
background: url(logo.png), url(background-pattern.png);
}
When you’re using multiple background images, you’ll often need to set more values for the background to get everything in the right place. If you want to use the background
shorthand, you can set the values for each image individually. Your shorthand will look something like this (notice the comma separating the first image and its values from the second image and its values):
body {
background:
url(logo.png) bottom center no-repeat,
url(background-pattern.png) repeat;
}
There’s no limit to how many background images you can set, and you can do cool things like animate your background images. There’s a good example of multiple background images with animation on David Walsh’s blog.
Demo
Browser support
IE | Edge | Chrome | Firefox | Safari | Opera |
---|---|---|---|---|---|
All | All | All | All | All | All |
iOS Safari | Chrome Android | Firefox Android | Android Browser | Opera Mobile |
---|---|---|---|---|
All | All | All | 90+ | 62+ |
Background image gradient support
IE | Edge | Chrome | Firefox | Safari | Opera |
---|---|---|---|---|---|
10+ | All | All | 3.6+ | 4+ | 11.5+ |
iOS Safari | Chrome Android | Firefox Android | Android Browser | Opera Mobile |
---|---|---|---|---|
All | All | All | 90+ | 62+ |
Background image SVG support
IE | Edge | Chrome | Firefox | Safari | Opera |
---|---|---|---|---|---|
9+ | All | 8+ | 4+ | 5+ | All |
iOS Safari | Chrome Android | Firefox Android | Android Browser | Opera Mobile |
---|---|---|---|---|
5+ | All | All | 90+ | 62+ |
More information
- CSS Backgrounds and Borders Module Level 3 (W3C)
- Understanding Success Criteria 1.1.1 (W3C Accessibility)
- How to Change a CSS Background Image’s Opacity (DigitalOcean)
- How to Apply Background Styles to HTML Elements With CSS (DigitalOcean)
Related tricks!
CSS Basics: Using Multiple Backgrounds
Stacking Order of Multiple Backgrounds
Positioning Offset Background Images
Background Image Shapes
How To: Resizeable Background Image
Apply a Filter to a Background Image
Zooming Background Images
More Control Over CSS Borders With background-image
Improving Perceived Performance with Multiple Background Images
Focusing a `background-image` on a Precise Location with Percentages
How to Repeat Text as a Background Image in CSS Using element()
06: Using SVG – SVG as background-image
Reveal a Background Image upon Text Selection
The Greatest CSS Tricks Vol. I
Related properties
background
.element { background: url(texture.svg) top center / 200px 200px no-repeat fixed #f8a100; }
background-attachment
.hero { background-attachment: fixed; }
background-blend-mode
.element { background-blend-mode: screen; }
background-clip
.element { background-clip: padding-box; }
background-color
element{ background-color: #ff7a18; }
background-origin
.element { background-origin: border-box; }
background-position
.element { background-position: 100px 5px; }
background-repeat
.element { background-repeat: repeat-x; }
background-size
.element { background-size: 300px 100px; }
Using this method – I want to hide the top layer on mobile width. I repeat background stack – but with that top layer deleted -and it still appears? When I tried opacity to hide it applied to all layers.
function example() {
element.innerHTML = “#F3-Col1-Row1 {
padding:77px 0px;
background: url(https://webassets.ssisurveys.com/savewithsurveys/panel/banners/Dash_mob_resp_med.png) no-repeat scroll right bottom, url(https://webassets.ssisurveys.com/savewithsurveys/panel/spot_art/green_top_bar.png) 2000px bottom #a4cb5a!important;
background-size: contain;
@media only screen (max-width: 959px) {
padding:33px 0px!important;
background: url(https://webassets.ssisurveys.com/savewithsurveys/panel/spot_art/green_top_bar.png) 2000px bottom #a4cb5a!important;
background-size: contain;
}
@media only screen (max-width: 580px) {
padding:33px 0px!important;
}
#F6-Col1-Row1 {
padding-top: 50px;
}
}”;
}
Excellent.
I am beginning with JEE and reading this article I improved much my knowledge.
Now I can better understand the mechanism how to program in this environment
can you tell me how to get to sesame street?
I love the background of this page. I’ll love to know what css code to use to create a similar effect…
Right click the background and select “inspect” / “inspect element” / etc. to see the code used to create something. In this case it looks like he used a base64 encoded svg:
background: linear-gradient(to bottom, #eee, rgba(222,112,6,0.2), #de7006),url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHZlcnNpb249JzEuMScgd2lkdGg9JzQwMCcgaGVpZ2h0PSc0MDAnPgoJPGRlZnMgaWQ9J2RlZnM0Jz4KCQk8ZmlsdGVyIGNvbG9yLWludGVycG9sYXRpb24tZmlsdGVycz0nc1JHQicgaWQ9J2ZpbHRlcjMxMTUnPgoJCQk8ZmVUdXJidWxlbmNlIHR5cGU9J2ZyYWN0YWxOb2lzZScgbnVtT2N0YXZlcz0nMScgYmFzZUZyZXF1ZW5jeT0nMC45JyBpZD0nZmVUdXJidWxlbmNlMzExNycgLz4KCQkJPGZlQ29sb3JNYXRyaXggcmVzdWx0PSdyZXN1bHQ1JyB2YWx1ZXM9JzEgMCAwIDAgMCAwIDEgMCAwIDAgMCAwIDEgMCAwIDAgMCAwIDYgLTMuNzUgJyBpZD0nZmVDb2xvck1hdHJpeDMxMTknIC8+CgkJCTxmZUNvbXBvc2l0ZSBpbjI9J3Jlc3VsdDUnIG9wZXJhdG9yPSdpbicgaW49J1NvdXJjZUdyYXBoaWMnIHJlc3VsdD0ncmVzdWx0NicgaWQ9J2ZlQ29tcG9zaXRlMzEyMScgLz4KCQkJPGZlTW9ycGhvbG9neSBpbj0ncmVzdWx0Nicgb3BlcmF0b3I9J2RpbGF0ZScgcmFkaXVzPScxMCcgcmVzdWx0PSdyZXN1bHQzJyBpZD0nZmVNb3JwaG9sb2d5MzEyMycgLz4KCQk8L2ZpbHRlcj4KCTwvZGVmcz4KCTxyZWN0IHdpZHRoPScxMDAlJyBoZWlnaHQ9JzEwMCUnIHg9JzAnIHk9JzAnIGlkPSdyZWN0Mjk4NScgZmlsbD0nI2VlZWVlZScvPiAgICAgCgk8cmVjdCB3aWR0aD0nMTAwJScgaGVpZ2h0PScxMDAlJyB4PScwJyB5PScwJyBpZD0ncmVjdDI5ODUnIHN0eWxlPSdmaWxsOiNlMDg3Mjg7ZmlsdGVyOnVybCgjZmlsdGVyMzExNSknIC8+Cjwvc3ZnPg==);
how can i use an image saved on my pc as a background image.
Excellent.
I love the background of this page.
i using the right path to insert background image in local site, but the image is not displaying, i dont know why, i tried everything it just doesn’t work
solved (../image
the 2 points and the slash solves the problem!
hey i dont know how to give the path of the image in the url….can someone guide me?
How to set back-ground image via source(src) rather than url?
Unfortunately,
src()
is not a part of thebackground-image
syntax.Actually, it does work when you give the path of your picture inside url(). Make sure to give the correct path to the image. My image was stored in the same directory as my html, inside another folder called assets.
in a css file I found this:
background-image: url(https://c.pxhere.com/photos/4f/ee/candy_toppings_sweet_colorful_sugar-883692.jpg!d);
what means !d at the end ?
This is only an parameter, you can use any image type on background prop.
For example
background:url(https://cdn1.photostockeditor.com/c/1512/cup-green-teapot-pouring-red-liquid-to-white-teacup-closeup-photography-beverage-beverage-image.jpg)
“Setting a fallback color” doesn’t work for me. On refresh, it always flashes the color before displaying the image. Possibly a bug in Firefox and Chrome?