Brian FitzGerald Brian FitzGerald - 3 years ago 58
Javascript Question

reserving space in browser layout for responsive images (preventing reflow)

I have been making changes to make my site more responsive and, in general, this has gone well. However, I have run into one problem:

Before, I always used height and width attributes on img elements in order to reserve space in the layout for the images while the browser loads them in. This prevents the layout from jerking around while the browser loads and calculates the needed space for the image.

After making my images more responsive, however, by using

max-width: 100%
and taking out the height and width attributes, the browser no longer reserves space for the image (because it no longer knows how tall or wide the image is going to be in advance since I couldn't explicitly tell it)

My goal is to have responsive images that also take up their appropriate space in the page layout upon its initial load. Does anyone know of a solution for this?

*EDIT (SOLUTION) - this is the best article I have found on the topic. Nice approach!

Answer Source

The correct answer here is to prevent the vertical reflow by using the "padding-bottom trick". To make this technique work, you must know the proportions of the image in advance.

Thanks to Anders M. Anderson for posting an excellent article on the topic: http://andmag.se/2012/10/responsive-images-how-to-prevent-reflow/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download