Make image fill div completely without stretching

后端 未结 13 1652
情话喂你
情话喂你 2020-11-30 04:22

I have large images of varying dimensions that need to completely fill 240px by 300px containers in both dimensions. Here is what I got right now, which only works for one d

相关标签:
13条回答
  • 2020-11-30 04:49

    This could do the job:

    .container {
        float: left;
        height: 300px;
        width: 240px;
        background-color: red;
        margin: 20px;
    }
    
    img {
        width:240px;
        height:300px;
    }
    
    0 讨论(0)
  • 2020-11-30 04:52

    Taking out the line: max-width:100% in your CSS file seems to do the trick.

    .container {
    height: 300px;
    width: 240px;
    background-color: red;
    float: left;
    overflow: hidden;
    margin: 20px;
    }
    
    img {
    height: auto;
    }
    

    Also you can add > to your closing div in your HTML file could make the code neater.

    <div class="container">
        <img src="http://placehold.it/300x1500">
    </div>
    <div class="container">
        <img src="http://placehold.it/1500x300">
    </div>
    

    Here is a working JSFiddle link: http://jsfiddle.net/HsE6H/19/

    0 讨论(0)
  • 2020-11-30 04:53

    Background can do this

    1. set image as background

    2.

    div {
       -webkit-background-size: auto 100%;
       -moz-background-size: auto 100%;
       -o-background-size: auto 100%;
       background-size: auto 100%;
    }
    

    or

    div {
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
    }
    
    0 讨论(0)
  • 2020-11-30 04:53

    We went down the path with an Angular app of using a variation on the jQuery approach above. Then one of our bright colleagues came up with a pure CSS approach. See this example here: https://jsfiddle.net/jeffturner/yrrncees/1/.

    Basically using line-height solved the problem for us. For those not wanting to hit the fiddle, the code fragments are:

    .container {
        margin: 10px;
        width: 125px;
        height: 125px;
        line-height: 115px;
        text-align: center;
        border: 1px solid red;
    }
    .resize_fit_center {
        max-width:100%;
        max-height:100%;
        vertical-align: middle;
    }
    

    The key is in using line-height and setting the container to do the same.

    0 讨论(0)
  • 2020-11-30 04:55

    I came across this topic because I was trying to solve a similar problem. Then a lightbulb went off in my head and I couldn't believe it worked because it was so simple and so obvious.

    CSS

    .container {
    height: 300px;
    width: 240px;
    background-color: red;
    float: left;
    overflow: hidden;
    margin: 20px;
    }
    
    img {
    min-width:100%;
    min-height:100%;
    }
    

    Just set the min-width and min-height to 100% and it will always automatically resize to fit the div, cutting off the excess image. No muss no fuss.

    0 讨论(0)
  • 2020-11-30 04:55

    Using an image as Div background has many disadvantages (like missing ALT for SEO). Instead of it, use object-fit: cover; in the image tag style!

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