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

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

    This should do it:

    img {    
    min-width: 100%;
    min-height: 100%;
    width: auto;
    height: auto;
    }
    
    0 讨论(0)
  • 2020-11-28 01:39

    The CSS object-fit: cover and object-position: left center property values now address this issue.

    0 讨论(0)
  • 2020-11-28 01:45

    Try this:

    img {
      position: relative;
      left: 50%;
      min-width: 100%;
      min-height: 100%;
      transform: translateX(-50%);
    }
    

    Hope this helps

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