Responsive Image full screen and centered - maintain aspect ratio, not exceed window

前端 未结 5 1224
慢半拍i
慢半拍i 2020-12-12 19:24

So I want an img to be displayed

  • as big as possible (filling the width when it is landscape / height when it is portrait)
  • no crop
  • <
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 19:53

    You could use a div with a background image instead and this CSS3 property:

    background-size: contain

    You can check out an example on:

    https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Scaling_background_images#contain

    To quote Mozilla:

    The contain value specifies that regardless of the size of the containing box, the background image should be scaled so that each side is as large as possible while not exceeding the length of the corresponding side of the container.

    However, keep in mind that your image will be upscaled if the div is larger than your original image.

提交回复
热议问题