Science, Tech, Math › Computer Science How to Stretch a Background Image to Fit a Web Page Give your website visual interest with background graphics Share Flipboard Email Print Science, Tech, Math PHP Programming Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Jennifer Kyrnin Jennifer Kyrnin Writer University of California University of Washington Jennifer Kyrnin is a professional web developer who assists others in learning web design, HTML, CSS, and XML. Learn about our Editorial Process Updated on February 22, 2021 Reviewed by Jessica Kormos Reviewed by Jessica Kormos Saint Mary-of-the-Woods College Jessica Kormos is a writer and editor with 15 years' experience writing articles, copy, and UX content for Tecca.com, Rosenfeld Media, and many others. Learn about our Editorial Process What to Know Preferred method: Use the CSS3 property for background-size and set it to cover.Alternate method: Use the CSS3 property for background-size set to 100% and background-position set to center. This article explains two ways to stretch a background image to fit a web page using CSS3. The Modern Way Images are an important part of attractive website designs. They add visual interest to a page and help you achieve the design you are looking for. When you work with background images, you may want an image to stretch to fit the page despite the wide range of devices and screen sizes. The best way to stretch an image to fit the background of an element is to use the CSS3 property, for background-size, and set it equal to cover. div { background-image: url('background.jpg'); background-size: cover; background-repeat: no-repeat;} Take a look at this example of it in action. Here's the HTML in the image below. Now, take a look at the CSS. It's not much different than the code above. There are a few additions to make it clearer. Now, this is the result in full screen. By setting background-size to cover, you guarantee that browsers will automatically scale the background image, however large, to cover the entire area of the HTML element that it's being applied to. Take a look at a narrower window. According to caniuse.com, this method is supported by over 90 percent of browsers, making it an obvious choice in most situations. It does create some problems with Microsoft browsers, so a fallback might be necessary. The Fallback Way Here is an example that uses a background image for the body of a page and which sets the size to 100% so that it will always stretch to fit the screen. This method isn't perfect, and it might cause some uncovered space, but by using the background-position property, you should be able to eliminate the problem and still accommodate older browsers. body { background: url('bgimage.jpg'); background-repeat: no-repeat; background-size: 100%; background-position: center;} Using the example from above with the background-size set to 100% instead, you can see that the CSS looks mostly the same. The result on a full-screen browser or one with similar dimensions to the image is nearly identical. However, with a narrower screen, the flaws start to show. Clearly, it isn't ideal, but it will work as a fallback. According to caniuse.com, this property works in IE 9+, Firefox 4+, Opera 10.5+, Safari 5+, Chrome 10.5+, and on all the major mobile browsers. This covers you for all the modern browsers available today, which means you should use this property without fear that it will not work on someone's screen. Between these two methods, you shouldn't have any difficulty supporting nearly all browsers. As background-size: cover gains even more acceptance among browsers, even this fallback will become unnecessary. Clearly, CSS3 and more responsive design practices have simplified and streamlined using images as adaptive backgrounds within HTML elements. Cite this Article Format mla apa chicago Your Citation Kyrnin, Jennifer. "How to Stretch a Background Image to Fit a Web Page." ThoughtCo, Apr. 5, 2023, thoughtco.com/stretch-background-image-3466979. Kyrnin, Jennifer. (2023, April 5). How to Stretch a Background Image to Fit a Web Page. Retrieved from https://www.thoughtco.com/stretch-background-image-3466979 Kyrnin, Jennifer. "How to Stretch a Background Image to Fit a Web Page." ThoughtCo. https://www.thoughtco.com/stretch-background-image-3466979 (accessed June 8, 2023). copy citation Featured Video