How do I fit an image (img) inside a div and keep the aspect ratio?

后端 未结 12 1987
广开言路
广开言路 2020-11-28 01:31

I have a 48x48 div and inside it there is an img element, I want to fit it into the div without losing any part, in the mean time the ratio is kept, is it achievable using h

相关标签:
12条回答
  • 2020-11-28 02:11

    Try CSS:

    img {
      object-fit: cover;
      height: 48px;
    }
    
    0 讨论(0)
  • 2020-11-28 02:11

    Here's an all JavaScript approach. It scales an image incrementally down until it fits correctly. You choose how much to shrink it each time it fails. This example shrinks it 10% each time it fails:

    let fit = function (el, w, h, percentage, step)
    {
        let newH = h;
        let newW = w;
    
        // fail safe
        if (percentage < 0 || step < 0) return { h: h, w: w };
        if (h > w)
        {
            newH = el.height() * percentage;
            newW = (w / h) * newH;
            if (newW > el.width())
            {
                return fit(el, w, h, percentage - step, step);
            }
        }
        else
        {
            newW = el.width() * percentage;
            newH = (h / w) * newW;
            if (newH > el.height())
            {
                return fit(el, w, h, percentage - step, step);
            }
        }
    
        return { h: newH, w: newW };
    };
    
    img.bind('load', function ()
    {
        let h = img.height();
        let w = img.width();
        let newFit = fit($('<img-wrapper-selector>'), w, h, 1, 0.1);
    
        img.width(newFit.w);
        img.height(newFit.h);
    });
    

    Feel free to copy and paste directly.

    0 讨论(0)
  • 2020-11-28 02:12

    What worked for me was:

    <div style='display: inline-flex; width: 80px; height: 80px;'>
    <img style='max-width: 100%; max-height: 100%' src='image file'>
    </div>
    

    inline-flex was required to keep the images from going outside of the div.

    0 讨论(0)
  • 2020-11-28 02:15

    Setting the photo as a background image will give us more control over size and placement, leaving the img tag to serve a different purpose...

    Below, if we want the div to be the same aspect ratio as the photo, then placeholder.png is a small transparent image with the same aspect ratio as photo.jpg. If the photo is a square, it's a 1px x 1px placeholder, if the photo is a video thumbnail, it's a 16x9 placeholder, etc.

    Specific to the question, use a 1x1 placeholder to maintain the div's square ratio, with a background image using background-size to maintain the photo's aspect ratio.

    I used background-position: center center; so the photo will be centered in the div. (Aligning the photo in the vertical center or bottom would get ugly with the photo in the img tag.)

    div {
        background: url(photo.jpg) center center no-repeat;
        background-size: contain;
        width: 48px; // or a % in responsive layout
    }
    img {
        width: 100%;
    }
    
    <div><img src="placeholder.png"/></div>
    

    To avoid an extra http request, convert the placeholder image to a data: URL.

    <img src="data:image/png;base64,..."/>
    
    0 讨论(0)
  • 2020-11-28 02:19

    HTML

    <div>
        <img src="something.jpg" alt="" />
    </div>
    

    CSS

    div {
       width: 48px;
       height: 48px;
    }
    
    div img {
       display: block;
       width: 100%;
    }
    

    This will make the image expand to fill its parent, of which its size is set in the div CSS.

    0 讨论(0)
  • 2020-11-28 02:20

    Unfortunately max-width + max-height do not fully cover my task... So I have found another solution:

    To save the Image ratio while scaling you also can use object-fit CSS3 propperty.

    Useful article: Control image aspect ratios with CSS3

    img {
        width: 100%; /* or any custom size */
        height: 100%; 
        object-fit: contain;
    }
    

    Bad news: IE not supported (Can I Use)

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