Is there a way to get a background in CSS to stretch or scale to fill its container?
16 Answers
Use the CSS 3 property background-size
:
#my_container {
background-size: 100% auto; /* width and height, can be %, px or whatever. */
}
This is available for modern browsers, since 2012.

- 11,017
- 2
- 48
- 37
-
95To preserve the aspect ratio of the image you should use "background-size: cover;" or "background-size: contain;". I've built a polyfill that implements those values in IE8: https://github.com/louisremi/background-size-polyfill – Louis-Rémi Dec 05 '12 at 09:53
-
6I know this is a year old but I have to (laugh and) agree with the 'decent browsers' part. Check all of your browsers but I've had more issues with IE than any of them. – ErocM Jan 11 '13 at 20:36
-
46I had to set `background-size: 100% 100%;` to get the desired effect I was going for, if that helps anyone else. – FarFigNewton Jan 28 '13 at 22:18
-
@Louis-Rémi How to center in both directions, when using "cover"? – ceving May 27 '13 at 12:11
-
@ceving add `background-position:50% 50%` - it's then used as the origin. – Niels Keurentjes Jun 03 '13 at 12:03
-
1explanation of `cover` and `contain` [on MDN](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Scaling_background_images#Special_values.3A_.22contain.22_and_.22cover.22) – mohas Mar 14 '14 at 07:16
-
guanome thanks, that solved my problem. cover and contain did not work in my scenario. – Apr 08 '14 at 19:35
-
4this doesn't actually work. The question is to **fill** the container which means we want it to stretch so it doesn't repeat. This answer on the other hand will repeat in the height direction if the height doesn't cover the area. "Stretch to fill" means stretch however you have to so it doesn't repeat. The correct answer is "cover" from below. That actually handles the case that this doesn't. – gman Feb 23 '15 at 00:59
For modern browsers, you can accomplish this by using background-size
:
body {
background-image: url(bg.jpg);
background-size: cover;
}
cover
means stretching the image either vertically or horizontally so it never tiles/repeats.
That would work for Safari 3 (or later), Chrome, Opera 10+, Firefox 3.6+, and Internet Explorer 9 (or later).
For it to work with lower verions of Internet Explorer, try these CSS:
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.myBackground.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='myBackground.jpg', sizingMethod='scale')";
-
2To Clement: What you suggest for filters for previous IE:s work only partly. It scales the picture width ok but the height scaling goes wrong by not restricting it to anything. The higher the page so is the background picture.. =( I tried this on IE8. And also, could it be in anyway possible use these 'cover' commands and somehow make this compatible with mobiles like iphone? I know there is the viewport problem but could it be possibke to scale the viewport to whole screen in a way that background is scaled here?? Thanks for responses! – Feb 10 '11 at 18:55
-
3The IE-specific filters are not ideal solutions so feel free to condition for IE with CSS alternatives. I personally use the CSS3 "cover" on my own site and it works fine on iOS devices, just be sure to define the device-width. – Clement Apr 19 '11 at 22:38
-
14
-
This solutions worked best for me where I do not wanted to stretch the image either horizontally or vertically more than it's actual resolution. Thanks..!! – Mr. Noddy Dec 04 '17 at 12:01
-
2NB: As of Chromium 78.0.3904.97, it is not possible to scale svg images independently along each axis. You may have to scale it up and convert it to a raster image. – Mike Dec 19 '19 at 21:24
-
2@Mike It works if you add `preserveAspectRatio="none"` to the SVG image. – Michał Perłakowski Aug 16 '21 at 14:32
Scaling an image with CSS is not quite possible, but a similar effect can be achieved in the following manner, though.
Use this markup:
<div id="background">
<img src="img.jpg" class="stretch" alt="" />
</div>
with the following CSS:
#background {
width: 100%;
height: 100%;
position: absolute;
left: 0px;
top: 0px;
z-index: 0;
}
.stretch {
width:100%;
height:100%;
}
and you should be done!
In order to scale the image to be "full bleed" and maintain the aspect ratio, you can do this instead:
.stretch { min-width:100%; min-height:100%; width:auto; height:auto; }
It works out quite nicely! If one dimension is cropped, however, it will be cropped on only one side of the image, rather than being evenly cropped on both sides (and centered). I've tested it in Firefox, Webkit, and Internet Explorer 8.

- 24,034
- 16
- 68
- 69

- 3,206
- 4
- 21
- 24
-
2This works well...but was looking for something a little more robust (probably with javascript) that also centered it and adjusted based on if the picture was landscape or portrait. If anyone has a solution in that vein would love a link...thanks! – Brian Armstrong Mar 02 '10 at 19:03
-
4In case anyone else is interested, this seemed to work well: http://www.buildinternet.com/project/supersized/ – Brian Armstrong Mar 02 '10 at 19:20
-
1Horizontal centering can be done with `margin: 0 auto` on `.stretch`. By only setting the `width` or `height`, the aspect ratio stays the same. Try using `max-width` and `max-height` to limit the zoom-factor... – Ronald Mar 21 '10 at 16:21
-
1This doesn't work for me in ie8/ie9, but works in ie6/ie7 (and of course all other browsers). In ie8/ie9, the image only shows up for about 3/4 of the div. Anybody have the same issue? – Sep 16 '11 at 02:14
-
For ie7, setting `.stretch { height: auto }` worked in my case whereas `height:100%` didn't. Otherwise great solution thanks! – Leimi Oct 10 '11 at 15:01
-
3A flaw with SO: there's no way to mark answers which are no longer correct, leading to people like @Ullas being led astray. It's easy to scale backgrounds in all modern browsers. https://developer.mozilla.org/en/CSS/background-size – Glenn Maynard Jul 26 '12 at 20:41
-
It's not really a flaw with SO if the original author of the answer chooses not to retract/improve their answer when it no longer applies. A moderator or suitably privileged (and trusted) individual should take such action instead. Feel free to take up the matter in meta.stackoverflow.com as I will not discuss the matter further here. – Metalshark Aug 01 '12 at 11:25
-
That's a pity, if say for example compiling chms for old and new machines. But to get this _and_ the accepted answer working for both using something like [@media](https://philipnewcomer.net/2014/04/target-internet-explorer-10-11-css/) and jqery won't look very pretty at all! – Laurie Stearn Jul 03 '17 at 14:56
Use the background-size attribute in CSS3:
.class {
background-image: url(bg.gif);
background-size: 100%;
}
EDIT: Modernizr supports detection of background-size support. You can use a JavaScript workaround written to work however you need it and load it dynamically when there is no support. This will keep the code maintainable without resorting to intrusive CSS hacks for certain browsers.
Personally I use a script to deal with it using jQuery, its an adaption of imgsizer. As most designs I do now use width %'s for fluid layouts across devices there is a slight adaptation to one of the loops (accounting for sizes that aren't always 100%):
for (var i = 0; i < images.length; i++) {
var image = images[i],
width = String(image.currentStyle.width);
if (width.indexOf('%') == -1) {
continue;
}
image.origWidth = image.offsetWidth;
image.origHeight = image.offsetHeight;
imgCache.push(image);
c.ieAlpha(image);
image.style.width = width;
}
EDIT: You may also be interested in jQuery CSS3 Finaliz[s]e.

- 8,194
- 7
- 34
- 49
-
3Just to emphasize, Modernizr does NOT enable support for background-size in browsers that don't natively support it. It just has a convenient cross-browser test for it. It's up to you to fake it when the test returns false. – Chris Moschini Jun 26 '12 at 19:18
Try the article background-size. If you use all of the following, it will work in most browsers except Internet Explorer.
.foo {
background-image: url(bg-image.png);
-moz-background-size: 100% 100%;
-o-background-size: 100% 100%;
-webkit-background-size: 100% 100%;
background-size: 100% 100%;
}

- 6,900
- 5
- 31
- 55

- 2,314
- 3
- 24
- 39
-
1
-
1this stretched for me on the ipad, but didn't scale. thx for posting. – Don Cote Nov 28 '11 at 23:44
-
1
-
6If the background image is an SVG, it is also necessary to specify `preserveAspectRatio="none"` within the SVG. More info about this [here](https://css-tricks.com/scale-svg/#article-header-id-4). Demo [here](https://codepen.io/blendamental/pen/MMWJKq). – Mentalist Jun 11 '19 at 05:18
-
you can use background-size: cover; background-attachment: fixed; you can scale it – Siraj Ahmed Jan 16 '20 at 13:53
.style1 {
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Works in:
- Safari 3+
- Chrome Whatever+
- IE 9+
- Opera 10+ (Opera 9.5 supported background-size but not the keywords)
- Firefox 3.6+ (Firefox 4 supports non-vendor prefixed version)
In addition you can try this for an ie solution
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.myBackground.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='myBackground.jpg', sizingMethod='scale')";
zoom:1;
Credit to this article by Chris Coyier http://css-tricks.com/perfect-full-page-background-image/

- 40,239
- 15
- 88
- 108
-
-
@PKHunter , im not sure exactly what you are reffering to. This is using `background-size: cover` except with browser prefixeds and a IE solution – Blowsie Jul 25 '17 at 13:31
Not currently. It will be available in CSS 3, but it will take some time until it's implemented in most browsers.

- 86,251
- 24
- 115
- 132
-
2Great article available at A List Apart: [Supersize that Background, Please!](http://www.alistapart.com/articles/supersize-that-background-please/) – jensgram Nov 02 '10 at 11:29
-
3-1 I don't think this answer is very relevant... and it doesn't offer a solution of *any* kind. – Potherca Jun 15 '11 at 14:32
-
1@Potherca this answer was correct at the time (almost 3 years ago). – Eran Galperin Jun 16 '11 at 09:51
-
4There is a reason the date is shown. It should be kept for historical references. Are you going over all the old answers on the site and downvoting them? the answer is still correct even if more information has been added since then. Also note that other answers here basically state the same (and some are indeed incorrect - they state it's impossible). I get the feeling you picked on this one since it has some votes. – Eran Galperin Jun 17 '11 at 14:42
-
3No, I picked on this one since all it does is say "Computer say no" even though there are several solutions to this problem even back in 2008 ;-) – Potherca Oct 13 '11 at 19:47
In one word: no. The only way to stretch an image is with the <img>
tag. You'll have to be creative.
This used to be true in 2008, when the answer was written. Today modern browsers support background-size
which solves this problem. Beware that IE8 doesn't support it.

- 104,512
- 87
- 279
- 422
-
6@Blowsie - Cute. Check the answer date. The browser landscape has changed a bit in the last 5 years. – Vilx- Aug 13 '13 at 13:00
Define "stretch and scale"...
If you've got a bitmap format, it's generally not great (graphically speaking) to stretch it and pull it about. You can use repeatable patterns to give the illusion of the same effect. For instance if you have a gradient that gets lighter towards the bottom of the page, then you would use a graphic that's a single pixel wide and the same height as your container (or preferably larger to account for scaling) and then tile it across the page. Likewise, if the gradient ran across the page, it would be one pixel high and wider than your container and repeated down the page.
Normally to give the illusion of it stretching to fill the container when the container grows or shrinks, you make the image larger than the container. Any overlap would not be displayed outside the bounds of the container.
If you want an effect that relies on something like a box with curved edges, then you would stick the left side of your box to the left side of your container with enough overlap that (within reason) no matter how large the container, it never runs out of background and then you layer an image of the right side of the box with curved edges and position it on the right of the container. Thus as the container shrinks or grows, the curved box effect appears to shrink or grow with it - it doesn't in fact, but it gives the illusion that is what's happening.
As for really making the image shrink and grow with the container, you would need to use some layering tricks to make the image appear to function as a background and some javascript to resize it with the container. There's no current way of doing this with CSS...
If you're using vector graphics, you're way outside my realm of expertise I'm afraid.

- 39,070
- 21
- 110
- 151
-
Stretch=Change x and y dimensions independently changing the aspect ration of the image, Scale=change x and y dimensions proportionately maintaining the aspect ratio of the image. – Lawrence Dol Dec 17 '08 at 23:12
-
@SoftwareMonkey: As a background then no, you can't change the stretch and scale in the true sense of the terms in straight CSS. You have to use various CSS tricks to give the illusion that this is what's happening as I've described. – BenAlabaster Dec 18 '08 at 00:45
This is what I've made of it. In the stretch class, I simply changed the height to auto
. This way your background picture has always got the same size as the width of the screen and the height will allways have the right size.
#background {
width: 100%;
height: 100%;
position: absolute;
margin-left: 0px;
margin-top: 0px;
z-index: 0;
}
.stretch {
width:100%;
height:auto;
}

- 24,034
- 16
- 68
- 69

- 41
- 1
Another great solution for this is Srobbin's Backstretch which can be applied to the body or any element on the page - http://srobbin.com/jquery-plugins/backstretch/

- 268
- 11
- 23
-
Nick, we generally ask that you provide the salient parts in your answer, and supply an external link only for further detail - this helps answers remain useful in the event of link-rot. – Lawrence Dol Nov 02 '12 at 08:23
-
1My Bad. Generally speaking, you add backstretch to the of your document then initialize like so: $(".your-element").backstretch("path/to/image.jpg"); – nickff Nov 02 '12 at 14:00
Try this
body
{
background: url(http://p1.pichost.me/i/40/1639647.jpg) no-repeat fixed;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}

- 45
- 3
Add a background-attachment
line:
#background {
background-attachment:fixed;
width: 100%;
height: 100%;
position: absolute;
margin-left: 0px;
margin-top: 0px;
z-index: 0;
}
.stretch {
width:100%;
height:auto;
}

- 30,738
- 21
- 105
- 131

- 21
- 1
I would like to point out that this is equivalent to doing:
html { width: 100%; height: 100%; }
body { width: 100%; height: 100%; /* Add background image or gradient to stretch here. */}

- 30,738
- 21
- 105
- 131

- 21
- 1
An additional tip for SolidSmile's cheat is to scale (the proportionate re-sizing) by setting a width and using auto for height.
Ex:
#background {
width: 500px;
height: auto;
position: absolute;
left: 0px;
top: 0px;
z-index: 0;
}
Use the border-image : yourimage
property to set your image and scale it upto the entire border of your screen or window .

- 12,274
- 10
- 84
- 125