How to set an image's width and height without stretching it?

前端 未结 12 1594
南旧
南旧 2020-11-28 05:22

If I have:

#logo {
    width: 400px;
    height: 200px;
}

then


12条回答
  •  情歌与酒
    2020-11-28 05:53

    CSS3 object-fit

    Am not sure how far its been implemented by webkit, IE and firefox. But Opera works like magic

    object-fit works with SVG content, but the same effect can also be achieved by setting the preserveAspectRatio="" attribute in the SVG itself.

    img {
      height: 100px;
      width: 100px;
      -o-object-fit: contain;
    }
    

    Chris Mills demo's it here http://dev.opera.com/articles/view/css3-object-fit-object-position/

提交回复
热议问题