488

I have a background image in the following div, but the image gets cut off:

 <div style='text-align:center;background-image: url(/media/img_1_bg.jpg);background-repeat:no-repeat;width:450px;height:900px;' id="mainpage" align="center">

Is there a way to show the background image without cutting it off?

Purag
  • 16,941
  • 4
  • 54
  • 75
Rajeev
  • 44,985
  • 76
  • 186
  • 285

8 Answers8

938

You can achieve this with the background-size property, which is now supported by most browsers.

To scale the background image to fit inside the div:

background-size: contain;

To scale the background image to cover the whole div:

background-size: cover;

JSFiddle example or runnable snippet:

#imagecontainer {
  background: url("http://3.bp.blogspot.com/_EqZzf-l7OCg/TNmdtcyGBZI/AAAAAAAAAD8/KD5Y23c24go/s1600/homer-simpson-1280x1024.jpg") no-repeat;
  width: 100px;
  height: 200px;
  border: 1px solid;
  background-size: contain;
}
<div id="imagecontainer"></div>

There also exists a filter for IE 5.5+ support, as well as vendor prefixes for some older browsers.

Basj
  • 41,386
  • 99
  • 383
  • 673
grc
  • 22,885
  • 5
  • 42
  • 63
196

If what you need is the image to have the same dimensions of the div, I think this is the most elegant solution:

background-size: 100% 100%;

If not, the answer by @grc is the most appropriated one.

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

Sertage
  • 3,123
  • 1
  • 19
  • 26
34

You can use this attributes:

background-size: contain;
background-repeat: no-repeat;

and you code is then like this:

 <div style="text-align:center;background-image: url(/media/img_1_bg.jpg); background-size: contain;
background-repeat: no-repeat;" id="mainpage">
hojjat.mi
  • 1,414
  • 16
  • 22
15
background-position-x: center;
background-position-y: center;
Abhijeet1520
  • 163
  • 4
  • 6
  • Welcome to stackoverflow! Please explain your answer to make it helpful. https://stackoverflow.com/help/how-to-answer https://stackoverflow.com/tour – ethry Oct 04 '22 at 02:00
10

you also use this:

background-size:contain;
height: 0;
width: 100%;
padding-top: 66,64%; 

I don't know your div-values, but let's assume you've got those.

height: auto;
max-width: 600px;

Again, those are just random numbers. It could quite hard to make the background-image (if you would want to) with a fixed width for the div, so better use max-width. And actually it isn't complicated to fill a div with an background-image, just make sure you style the parent element the right way, so the image has a place it can go into.

Chris

Chris
  • 101
  • 1
  • 2
  • 1
    Link to a highly voted answer that talks about this solution in more detail: https://stackoverflow.com/questions/600743/how-to-get-div-height-to-auto-adjust-to-background-size and shows you how to calculate the `padding-top`. – John Lee May 01 '19 at 16:57
8

try any of the following,

background-size: contain;
background-size: cover;
background-size: 100%;

.container{
    background-size: 100%;
}

The background-size property specifies the size of the background images.

There are different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height).

  • percentage - Sets the width and height of the background image in percent of the parent element.
  • cover - Resize the background image to cover the entire container, even if it has to stretch the image or cut a little bit off one of the edges
  • contain - Resize the background image to make sure the image is fully visible

For more: https://www.w3schools.com/cssref/css3_pr_background-size.asp

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
  • Welcome to stackoverflow! Please explain your answer to make it helpful. https://stackoverflow.com/help/how-to-answer https://stackoverflow.com/tour – ethry Oct 04 '22 at 02:01
2

Alternative:

background-size: auto 100%;
Marcel
  • 2,810
  • 2
  • 26
  • 46
  • Welcome to stackoverflow! Please explain your answer to make it helpful. https://stackoverflow.com/help/how-to-answer https://stackoverflow.com/tour – ethry Oct 04 '22 at 02:00
2

you can also try this, set background size as cover and to get it look nicer also set background position center like so :

background-size: cover;
background-position: center ;
Mostafa Ghorbani
  • 397
  • 1
  • 3
  • 15