CSS image scaling to fit within area not distort

后端 未结 6 683
栀梦
栀梦 2020-12-30 20:19

Is there a way with CSS or otherwise of making an image fit within an area. Lets say I have multiple images of different sizes and I want them all to fit within a div of 150

6条回答
  •  难免孤独
    2020-12-30 20:52

    This worked for me:

    img.perfect-fit {
        width: auto;
        height: auto;
        min-width: 100%;
        min-height: 100%;
    }
    

    It tries to do a "perfect fit" of the container, stretching itself to fit the bounds while maintaining image proportion. Haven't tested it with IE6.

    JSFiddle: http://jsfiddle.net/4zudggou/

提交回复
热议问题