DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
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.
ImagesUsing 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.
GradientsAnother 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鈥檚 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 colorIf 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 imagesYou 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.
DemoBrowser supportIEEdgeChromeFirefoxSafariOperaAllAllAllAllAllAlliOS SafariChrome AndroidFirefox AndroidAndroid BrowserOpera MobileAllAllAll90+62+Source: caniuseBackground image gradient supportIEEdgeChromeFirefoxSafariOpera10+AllAll3.6+4+11.5+iOS SafariChrome AndroidFirefox AndroidAndroid BrowserOpera MobileAllAllAll90+62+Source: caniuseBackground image SVG supportIEEdgeChromeFirefoxSafariOpera9+All8+4+5+AlliOS SafariChrome AndroidFirefox AndroidAndroid BrowserOpera Mobile5+AllAll90+62+Source: caniuseMore informationCSS Backgrounds and Borders Module Level 3 (W3C)Understanding Success Criteria 1.1.1 (W3C Accessibility)How to Change a CSS Background Image鈥檚 Opacity (DigitalOcean)How to Apply Background Styles to HTML Elements With CSS (DigitalOcean)Related tricks!Article onOct 27, 2018 CSS Basics: Using Multiple Backgrounds Chris CoyierArticle onMay 30, 2019 Stacking Order of Multiple Backgrounds Chris CoyierArticle onMar 30, 2020 Positioning Offset Background Images Chris CoyierArticle onMar 7, 2019 Background Image Shapes Joseph MarkusArticle onNov 20, 2013 How To: Resizeable Background Image Chris CoyierArticle onOct 2, 2018 Apply a Filter to a Background Image Chris CoyierArticle onApr 10, 2017 Zooming Background Images Dylan Winn-BrownArticle onAug 7, 2020 More Control Over CSS Borders With background-image Chris CoyierArticle onNov 3, 2016 Improving Perceived Performance with Multiple Background Images Chris CoyierArticle onNov 30, 2018 Focusing a `background-image` on a Precise Location with Percentages Jay SitterArticle onMar 26, 2020 How to Repeat Text as a Background Image in CSS Using element() Ollie Williams onSep 16, 2021 06: Using SVG – SVG as background-image Chris CoyierArticle onMay 26, 2015 Reveal a Background Image upon Text Selection Chris CoyierArticle onFeb 12, 2022 The Greatest CSS Tricks Vol. I Chris CoyierRelated properties Almanac onMay 20, 2021 background .element { background: url(texture.svg) top center / 200px 200px no-repeat fixed #f8a100; }Sara Cope Almanac onJun 6, 2017 background-attachment .hero { background-attachment: fixed; }Chris Coyier Almanac onMay 20, 2021 background-blend-mode .element { background-blend-mode: screen; }Robin Rendle Almanac onMay 20, 2021 background-clip .element { background-clip: padding-box; }Chris Coyier Almanac onMay 20, 2021 background-color element{ background-color: #ff7a18; }Chris Coyier Almanac onMay 20, 2021 background-origin .element { background-origin: border-box; }Chris Coyier Almanac onJun 2, 2021 background-position .element { background-position: 100px 5px; }Chris Coyier Almanac onMay 21, 2024 background-repeat .element { background-repeat: repeat-x; }Geoff Graham Almanac onMay 20, 2021 background-size .element { background-size: 300px 100px; }Chris Coyier