Can I use for both height and width-constrained images?

后端 未结 1 1787
独厮守ぢ
独厮守ぢ 2021-01-18 14:28

I have a slideshow sort of thing where images will expand as large as they can, but not exceed the viewport\'s width or height. Basically object-fit: contain.

相关标签:
1条回答
  • 2021-01-18 14:47

    I think I got it (1/2) is equal to (width/height):

    <img 
        srcset="http://lorempixel.com/960/1920/sports/10/w1920/ 960w"
        sizes="(min-aspect-ratio: 1/2) calc(100vh * (1 / 2))"
        />
    

    Or with more code:

    <img
        srcset="http://lorempixel.com/960/1920/sports/10/w1920/ 960w"
        sizes="(min-aspect-ratio: 1/2) calc(100vh * (1 / 2)), 100vw"
        />
    

    As a side note: I also have written a script that automatically calculates the right sizes for parent height/width constrained sizes. See here the parent-fit script.

    0 讨论(0)
提交回复
热议问题