3

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

Mild Fuzz
  • 29,463
  • 31
  • 100
  • 148

3 Answers3

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;
}

http://www.w3schools.com/cssref/css3_pr_background-size.asp

MrB
  • 1
  • 2