Set width/height of image to avoid reflow on image load

后端 未结 6 1546
甜味超标
甜味超标 2020-12-30 23:00

When I use image tags in html, I try to specify its width and height in the img tag, so that the browser will reserve the space for them even before the images

6条回答
  •  被撕碎了的回忆
    2020-12-30 23:41

    I'm also looking for the answer to this problem. With max-width, width= and height=, the browser has enough data that it should be able to leave the right amount of space for an image but it just doesn't seem to work that way.

    I worked around this with a jQuery solution for now. It requires you to provide the width= and height= for your tags.

    CSS:

    img { max-width: 100%; height: auto; }
    

    HTML:

    
    

    jQuery:

    $('img').each(function() { 
        var aspect_ratio = $(this).attr('height') / $(this).attr('width') * 100;
        $(this).wrap('
    '); });

    This automatically applies the technique seen on: http://andmag.se/2012/10/responsive-images-how-to-prevent-reflow/

提交回复
热议问题