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
  • 27,845
  • 29
  • 97
  • 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,342
  • 3
  • 35
  • 49
6

With the background-size property (which has limited support).

Rory O'Kane
  • 27,337
  • 11
  • 91
  • 127
Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
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