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

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

    Here you have my working example. I have used a trick that is setting the image as background of the div container with background-size:cover and background-position:center center

    I have placed the image with width:100% and opacity:0 making it invisible. Note that I am showing my image only because I have an special interest on calling the child click event.

    Please note that altought I am ussing angular it is completely irrelevant.

提交回复
热议问题