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

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

    The only way I achieved the "best case" scenario described, was putting the image as a background:

    <div class="container"></div>​
    
    .container {
        width: 150px;
        height: 100px;
        background-image: url("http://i.stack.imgur.com/2OrtT.jpg");
        background-size: cover;
        background-repeat: no-repeat;
        background-position: 50% 50%;
    }​
    
    0 讨论(0)
  • 2020-11-28 01:20

    It's a bit late but I just had the same problem and finally solved it with the help of another stackoverflow post (https://stackoverflow.com/a/29103071).

    .img {
       object-fit: cover;
       width: 50px;
       height: 100px;
    }
    

    Hope this still helps somebody.

    Ps: Also works together with max-height, max-width, min-width and min-height css properties. It's espacially handy with using lenght units like 100% or 100vh/100vw to fill the container or the whole browser window.

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

    Just fix the height of the image & provide width = auto

    img{
        height: 95vh;
        width: auto;
    }
    
    0 讨论(0)
  • 2020-11-28 01:24

    You can use div to achieve this. without img tag :) hope this helps.

    .img{
    	width:100px;
    	height:100px;
    	background-image:url('http://www.mandalas.com/mandala/htdocs/images/Lrg_image_Pages/Flowers/Large_Orange_Lotus_8.jpg');
    	background-repeat:no-repeat;
    	background-position:center center;
    	border:1px solid red;
    	background-size:cover;
    }
    .img1{
    	width:100px;
    	height:100px;
    	background-image:url('https://images.freeimages.com/images/large-previews/9a4/large-pumpkin-1387927.jpg');
    	background-repeat:no-repeat;
    	background-position:center center;
    	border:1px solid red;
    	background-size:cover;
    }
    <div class="img">	
    </div>
    <div class="img1">	
    </div>

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

    .image-wrapper{
        width: 100px;
        height: 100px;
        border: 1px solid #ffffd;
    }
    .image-wrapper img {
        object-fit: contain;
        min-width: 100%;
        min-height: 100%;
        width: auto;
        height: auto;
        max-width: 100%;
        max-height: 100%;
    }
    <div class="image-wrapper">
      <img src="">
    </div>

    0 讨论(0)
  • 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:

    <div class="container">
        <img style="background-image: url("https://i.imgur.com/XOmNCwY.jpg");" src="img/blank.gif">
    </div> 
    
    
    .container img{
       width: 100%;
       height: auto;
       background-size: cover;
       background-repeat: no-repeat;
       background-position: center;
    }​
    
    0 讨论(0)
提交回复
热议问题