Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep div height while the image is loading

I have a square image within .img-container. Sometimes it takes a few seconds for an image to load and the .img-container collapses and only takes the full height when the image loads. However, I would like it to keep the full height (as if the image is there) while the image is loading.

I would've easily done this by setting a min-height on img-container class, however it's a fluid grid and the image is responsive (notice bootstrap's img-responsive helper class) which makes it hard to set the min-height to an appropriate value for different screen sizes (although achievable with media queries as a last resort). Solving this by putting a placeholding image sounds like an overkill (especially performance wise on mobile). Also, not sure what would load first then, the placeholder image or the actual image.

<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
    <div class="card">
      <span class="img-container thumbnail clearfix">
        <img alt class="pull-left img-responsive" src="http://...">
      </span>
      <div class="caption">
        <a href="http://.." class="card-title lead">
          Some text
        </a>
      </div>
    </div>
  </div>
like image 998
Sbbs Avatar asked Sep 08 '25 15:09

Sbbs


1 Answers

EDIT DUE TO COMMENT

If you do not specify a source at all (not even a dummy, temporary one), the browser will not even try to "guess" the image's height, and so it collapses. If you know the ratio of the image (it's obviously 1:1 in case of a square picture), you can use the following trick to preoccupy the space, and scale the image along with the div.

Set the following CSS:

.test-div-inner {
    padding-bottom:100%;
    background:#EEE;
    height:0;
    position:relative;
}
.test-image {
    width:100%;
    height:100%;
    display:block;
    position:absolute;
}

Then you can use the following HTML:

<div class="test-div-inner">
    <img class="test-image" src="https://www.ilikewallpaper.net/ipad-air-wallpapers/download/4556/Andromeda-Galaxy-ipad-4-wallpaper-ilikewallpaper_com_1024.jpg">
</div>

Here is the working example: http://jsfiddle.net/pQ5zh/3/

Note that the fiddle contains another div element, this is only required if you would like to give it all a padding or border, since the padding-bottom calculates the padding in pixels based on the width of the div INCLUDING THOSE PARAMETERS, which is NOT the effect we want to achieve (the image would be a little taller than it should be).

For non-square images:

If you would like to change the ratio of the picture, just change the padding-bottom of the container div accordingly. For example, if you would like to place an image with a ratio of 2:1, change the padding to 50%. To keep it short: the ratio of the container div's width and padding should always be equal to the ratio of the image's width and height.


There is an easy way to do exactly this, but it only works for square images.
Specify the width of the image (using CSS) to be 100%. This way the browser will automatically assume that the image height is the same as it's width, and preoccupy the place.

http://jsfiddle.net/pQ5zh/2/

.test-image {
    width:100%;
}

Note: There is a way to achieve this for non-square images too, but that is a bit more complicated.

EDIT: See above.

like image 162
skreborn Avatar answered Sep 10 '25 05:09

skreborn