How does ones scale a background image with CSS? The image is attached with CSS, and I would like to scale it to fit a certain width
Asked
Active
Viewed 8,532 times
3
-
http://stackoverflow.com/questions/376253/stretch-and-scale-css-background – Jawad Jul 27 '11 at 14:39
-
http://stackoverflow.com/questions/1150163/stretch-and-scale-a-css-image-background-with-css-only – Jawad Jul 27 '11 at 14:39
-
1http://css-tricks.com/766-how-to-resizeable-background-image/ – Jawad Jul 27 '11 at 14:40
-
point taken. I did try to search, obviously not well enough ;) – Mild Fuzz Jul 27 '11 at 14:47
3 Answers
7
You may try the following code:
.aboutpic {
width: 150px; /* replace 150px with something you need */
float: left;
background-image: url(../images/aboutpic.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: 100%;
height: 139px; /* replace 139 px with something you need */
}
Then in the html just put:
<div class="aboutpic"></div>
It works. I tested it.

Sajib Mahmood
- 3,382
- 3
- 37
- 50
6
With the background-size
property (which has limited support).

Rory O'Kane
- 29,210
- 11
- 96
- 131

Quentin
- 914,110
- 126
- 1,211
- 1,335
0
Javascript is a good way to start, "jquery.backstretch.js" is a very good jquery lib to scaling the background img.
Or, if your target client is using modern browser which means it can support CSS3, use this:
body{
background:url(background.jpg);
background-repeat:no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

MrB
- 1
- 2