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

后端 未结 4 1054
清酒与你
清酒与你 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 13:35

    If you know the aspect ratio of the image ahead of time, you can use media queries with aspect-ratio.

    In this case, I know my image is has a 4:3 aspect ratio.

    With the media queries, it lets me toggle between filling the height when the height is the limiting dimension (aspect ratio wider than 4/3) and filling the width when the width is the limiting dimension.

    img {
      height: 100%;
      width: auto;
      margin: auto;
      position: absolute;
      top: 0; left: 0; bottom: 0; right: 0;
    }
    
    @media screen and (max-aspect-ratio: 4/3) {
      img {
        height: auto;
        width: 100%;
      }
    }
    

    https://jsfiddle.net/0cjc23oz/

    0 讨论(0)
  • 2021-01-07 13:54

    Add width:100%; height:auto; to your .resize_fit_center so it becomes this:

    .resize_fit_center {
        width:100%;
        height:auto;
        max-width:100%;
        max-height:100%;
        vertical-align: middle;
    }
    
    0 讨论(0)
  • 2021-01-07 13:59

    Well, you are using fixed width for elements. try to use relative units (in %) and make max-width to 100%. I think this will work.

    0 讨论(0)
  • 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;    
    }
    <div class='container'>
    </div>
    
    <div class='container' style='width:50px;height:100px;line-height:100px'>
    </div>
    
    <div class='container' style='width:140px;height:70px;line-height:70px'>
    </div>
    
    This one should scale up
    <div class='container' style='width:350px;height:350px;line-height:350px'>
    </div>

    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%;
    }
    <div class='container'>
        <img class='resize_fit_center fit_width'
          src='http://i.imgur.com/H9lpVkZ.jpg' />
    </div>
    
    <div class='container' style='width:50px;height:100px;line-height:100px'>
        <img class='resize_fit_center fit_width'
          src='http://i.imgur.com/H9lpVkZ.jpg' />
    </div>
    
    <div class='container' style='width:140px;height:70px;line-height:70px'>
        <img class='resize_fit_center fit_height'
          src='http://i.imgur.com/H9lpVkZ.jpg' />
    </div>
    
    This one should scale up
    <div class='container' style='width:350px;height:350px;line-height:350px'>
        <img class='resize_fit_center fit_width'
          src='http://i.imgur.com/H9lpVkZ.jpg' />
    </div>

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