CSS force image resize and keep aspect ratio

前端 未结 23 891
野趣味
野趣味 2020-11-22 10:04

I am working with images, and I ran across a problem with aspect ratios.

\"\"

相关标签:
23条回答
  • 2020-11-22 10:43

    Remove the "height" property.

    <img src="big_image.jpg" width="900" alt=""/>
    

    By specifying both you are changing the aspect ratio of the image. Just setting one will resize but preserve the aspect ratio.

    Optionally, to restrict oversizings:

    <img src="big_image.jpg" width="900" alt="" style="max-width:500px; height:auto; max-height:600px;"/>
    
    0 讨论(0)
  • 2020-11-22 10:45

    Just add this to your css, It will automaticly shrink and expand with keeping the original ratio.

    img {
        display: block;
        max-width: 100%;
        max-height: 100%;
        width: auto;
        height: auto;
    }
    
    0 讨论(0)
  • 2020-11-22 10:49

    To maintain a responsive image while still enforcing the image to have a certain aspect ratio you can do the following:

    HTML:

    <div class="ratio2-1">
       <img src="../image.png" alt="image">
    </div>
    

    And SCSS:

    .ratio2-1 {
      overflow: hidden;
      position: relative;
    
      &:before {
        content: '';
        display: block;
        padding-top: 50%; // ratio 2:1
      }
    
      img {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
      }
    }
    

    This can be used to enforce a certain aspect ratio, regardless of the size of the image that authors upload.

    Thanks to @Kseso at http://codepen.io/Kseso/pen/bfdhg. Check this URL for more ratios and a working example.

    0 讨论(0)
  • 2020-11-22 10:49

    You can use this:

    img { 
        width: 500px; 
        height: 600px; 
        object-fit: contain; 
        position: relative; 
        top: 50%; 
        transform: translateY(-50%); 
    }
    
    0 讨论(0)
  • 2020-11-22 10:50

    This will make image shrink if it's too big for specified area (as downside, it will not enlarge image).

    The solution by setec is fine for "Shrink to Fit" in auto mode. But, to optimally EXPAND to fit in 'auto' mode, you need to first put the received image into a temp id, Check if it can be expanded in height or in width (depending upon its aspect ration v/s the aspect ratio of your display block),

    $(".temp_image").attr("src","str.jpg" ).load(function() { 
        // callback to get actual size of received image 
    
        // define to expand image in Height 
        if(($(".temp_image").height() / $(".temp_image").width()) > display_aspect_ratio ) {
            $(".image").css('height', max_height_of_box);
            $(".image").css('width',' auto');
        } else { 
            // define to expand image in Width
            $(".image").css('width' ,max_width_of_box);
            $(".image").css('height','auto');
        }
        //Finally put the image to Completely Fill the display area while maintaining aspect ratio.
        $(".image").attr("src","str.jpg");
    });
    

    This approach is useful when received images are smaller than display box. You must save them on your server in Original Small size rather than their expanded version to fill your Bigger display Box to save on size and bandwidth.

    0 讨论(0)
  • 2020-11-22 10:50

    You can set the container to display: flex and align-items: center (other align-items values work too). Instead of align-items you can also set align-self on the image itself.

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