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

前端 未结 15 1398
佛祖请我去吃肉
佛祖请我去吃肉 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:25

    All answers below have fixed width and height, which makes solution "not responsive".

    To achieve the result but keep image responsive I used following:

    1. Inside container place a transparent gif image with desired proportion
    2. Give an image tag inline css background with image you want to resize and crop

    HTML:

    .container img{ width: 100%; height: auto; background-size: cover; background-repeat: no-repeat; background-position: center; }​

提交回复
热议问题