Scaling Images Proportionally in CSS with Max-width

后端 未结 8 1425
余生分开走
余生分开走 2021-01-31 13:39

Right now, I\'m using max-width to scale images to fit. However, they don\'t scale proportionally. Is there a way to cause this to happen? I\'m open to Javascript/jQuery.

<
8条回答
  •  时光说笑
    2021-01-31 14:27

    You need to specify the original width and height:

    Whatever
    

    And then use something like this in the CSS:

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

    You could try this with jQuery if you don't have the width and height up front, but your mileage may vary:

    $(function(){
        $('#content img').load(function(){
           var $img = $(this);
           $img.attr('width', $img.width()).attr('height', $img.height());
        });
    });
    

    Obviously replace #content with whatever selector you want to scope the functionality to.

提交回复
热议问题