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

前端 未结 8 995
孤城傲影
孤城傲影 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:42

    You don't necessarily want to stretch in a certain direction based on which is bigger. For example, I have a widescreen monitor, so even if it's a wider image than it is tall, stretching it left-to-right may still clip the top and bottom edges off.

    You need to calculate the ratio between the window width and height and the image width and height. The smaller one is your controlling axis - the other is dependent. This is true even if both axes are larger than the respective window length.

    
    

提交回复
热议问题