4

I'm working on a photography site with a lot of images and they have no fixed height and width as I want this site to be 100% fluid: how do you work around the ugly Chrome repaint of the images? (i.e. Images are first displayed at zero height and then rescaled to their final size moving around the entire layout)

I've tried pretty much everything and my last option is to hide the image repaint with a black div and then set its opacity to 0 when images are finished loading (BTW, I've tried this with a (document).ready call but it seems too soon: how would you do it?)

apaderno
  • 28,547
  • 16
  • 75
  • 90
teolives
  • 83
  • 1
  • 5

4 Answers4

3

Specify your image's height and width attribute / its dimensions.

<img src="img.jpg" width="125" height="60" alt="My First Photograph ever">

This helps the browser avoid a second pass to layout your page and it optimizes page load as well! :)

Zhianc
  • 1,411
  • 3
  • 20
  • 37
1

I know I am more than two years late, but how about the practice suggested here?

<div class="embed-container ratio-16-9">
      <img src="imgage.jpg"/>
</div>


.embed-container {
    position: relative;   
    height: 0;
    overflow: hidden;
    background-color:black;  
}

.ratio-16-9{
    padding-bottom:56.25%; /* 9/16*100 */
}

.ratio-4-3{
    padding-bottom:75%; /* 3/4*100 */
}

.ratio-1-1{
padding-bottom:100%; /* ... */  

Also, an important remark from the comments section to pay attention to, and improve upon the original technique:

Nice trick. However, if I was you, I would replace the "img" tag with a background image on your div (and background-size: cover or contain). That would avoid you the position trick, the overflow trick, and a lot of work for the browser.

I hope someone will find this useful.

pilau
  • 6,635
  • 4
  • 56
  • 69
1

Chrome (or any browser really) cannot avoid this 'repainting', since they don't know on forehand what size your images will be.

Thus, you will need to explicitly specify the sizes of your images, either in the image width and height properties itself, or via CSS.

Willem Mulder
  • 12,974
  • 3
  • 37
  • 62
  • I disagree, only webkit browsers seem to start with 0 height during rendering: FF, IE, opera, all save room for the pictures without rescaling the entire layout. – teolives Nov 17 '11 at 15:09
0

It’s hard to test, but you could try setting width/height in CSS

img {display: block; width: 100%; height: auto;}

if you want the images to be full-width. This might prevent a full-page repaint, but of course there’ll be some repaint regardless as images load. You can also investigate what’s happening with Chrome’s --show-paint-rects

Hope that helps

Oli Studholme
  • 2,600
  • 22
  • 16