Make image fill div completely without stretching

后端 未结 13 1737
情话喂你
情话喂你 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: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.

提交回复
热议问题