tags:

views:

1893

answers:

5

How can I expand an image to have the same height as the div after resizing? Or how can I fill the white space left by the image with a color?

A: 

Set a background color for the container div

rahul
A: 

Why have a div at all?

altCognito
A: 

Not sure what you are trying to do, could you clarify?

mdja
I have a div with height of 1000 and the background-image height is 800. What should I do so that the image will have 1000px height?
Cornel
+2  A: 

I think this will get you your desired result:

<style>
    div {
 width:50px;
 height:100px;
    }

    div img {
 height:100%;
    }
</style>
<div>
    <img src='path/to/img.gif' />
</div>

The image will now fill to the container div's height and scale its width with its aspect ratio. This could cause the image to expand past the container div's intended width boundaries though so be careful.

Dave L
I use the background-image property
Cornel
As far as I know, this isn't possible with current browsers.
Dave L
to prevent the image from expanding past the container div, use max-width: 100%; max-height: 100%; along with width: 100%; and height: 100%;
letronje
A: 

There is a way to do this in CSS3:

div {background-image: url(path/to/image); background-size: 100%}

The background-size property is only supported in newer versions of Safari and Opera. For the other browsers, you can fill the whitespace left around the image with a color using the background-color property, like so:

div {
  background-image: url(path/to/image);
  background-size: 100%;
  background-color: #000000;
  }

Or, in shorthand:

div {background:#000000 url(path/to/image) no-repeat; background-size:100%}

Using this code will cause the image to stretch in CSS3 enabled browsers (newest Opera, Safari, and possibly Firefox). Older browsers and IE will have the whitespace around the image filled with the background color specified.

vamin