HTML - display an image as large as possible while preserving aspect ratio

前端 未结 8 1011
孤城傲影
孤城傲影 2020-12-28 16:44

I\'d like to have an HTML page which displays a single PNG or JPEG image. I want the image to take up the whole screen but when I do this:



        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 17:36

    The easiest way to do so (if you don't need to support IE) is setting the object-fit CSS property to contain:

    img { object-fit: contain; }
    

    See also:

    • https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
    • https://caniuse.com/#search=object-fit

提交回复
热议问题