How can I fill a div with an image while keeping it proportional?

前端 未结 15 1443
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 00:56

I found this thread — How do you stretch an image to fill a

while keeping the image's aspect-ratio? — that is not entirely the thing that I want.

I h

15条回答
  •  半阙折子戏
    2020-11-28 01:37

    An old question but deserves an update as now there is a way.

    The correct CSS based answer is to use object-fit: cover, which works like background-size: cover. Positioning would be taken care of by object-position attribute, which defaults to centering.

    But there is no support for it in any IE / Edge browsers, or Android < 4.4.4. Also, object-position is not supported by Safari, iOS or OSX. Polyfills do exist, object-fit-images seems to give best support.

    For more details on how the property works, see CSS Tricks article on object-fit for explanation and demo.

提交回复
热议问题