CSS: center and scale up or down image while preserving aspect ratio

后端 未结 4 1084
清酒与你
清酒与你 2021-01-07 13:26

How to center and scale up or down image while preserving aspect ratio in html/css?

I have a fiddle showing my current solution, but it does not scale up the image w

4条回答
  •  粉色の甜心
    2021-01-07 14:01

    You can drop the inner img element and do like this.

    This will work no matter image proportions.

    .container {
        margin: 10px;
        width: 115px;
        height: 115px;
        border: 1px solid red;
        background: url(http://i.imgur.com/H9lpVkZ.jpg) no-repeat center center;
        background-size: contain;    
    }
    This one should scale up

    If you know each image prop. for each container you can simply keep your html and do this.

    .container {
        margin: 10px;
        width: 115px;
        height: 115px;
        line-height: 115px;
        text-align: center;
        border: 1px solid red;
    }
    .resize_fit_center {
        vertical-align: middle;
    }
    .fit_width {
        width:100%;
    }
    .fit_height {
        height:100%;
    }
    This one should scale up

提交回复
热议问题