How to set an image's width and height without stretching it?

前端 未结 12 1550
南旧
南旧 2020-11-28 05:22

If I have:

#logo {
    width: 400px;
    height: 200px;
}

then


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

    This is quite old question, but I have had the exact same annoying issue where everything worked fine for Chrome/Edge (with object-fit property) but same css property did not work in IE11 (since its unsupported in IE11), I ended up using HTML5 "figure" element which solved all my problems.

    I personally did not use the outer DIV tag since that did not help at all in my case, so I avoided the outer DIV and simply replaced with 'figure' element.

    The below code forces the image to reduce/scale down nicely (without changing the original aspect ratio).

    <figure class="figure-class">
      <img class="image-class" src="{{photoURL}}" />
    </figure>
    

    and css classes:

    .image-class {
        border: 6px solid #E8E8E8;
        max-width: 189px;
        max-height: 189px;
    }
    
    .figure-class {
        width: 189px;
        height: 189px;
    }
    
    0 讨论(0)
  • 2020-11-28 05:47

    Yes you need an encapsulating div:

    <div id="logo"><img src="logo.jpg"></div>
    

    with something like:

    #logo { height: 100px; width: 200px; overflow: hidden; }
    

    Other solutions (padding, margin) are more tedious (in that you need to calculate the right value based on the image's dimensions) but also don't effectively allow the container to be smaller than the image.

    Also, the above can be adapted much more easily for different layouts. For example, if you want the image at the bottom right:

    #logo { position: relative; height: 100px; width: 200px; }
    #logo img { position: absolute; right: 0; bottom: 0; }
    
    0 讨论(0)
  • 2020-11-28 05:47

    You can use as below :

    .width100 {
      max-width: 100px;
      height: 100px;
      width: auto;
      border: solid red;
    }
    <img src="https://www.gravatar.com/avatar/dc48e9b92e4210d7a3131b3ef46eb8b1?s=512&d=identicon&r=PG" class="width100" />

    0 讨论(0)
  • 2020-11-28 05:53

    CSS3 object-fit

    Am not sure how far its been implemented by webkit, IE and firefox. But Opera works like magic

    object-fit works with SVG content, but the same effect can also be achieved by setting the preserveAspectRatio="" attribute in the SVG itself.

    img {
      height: 100px;
      width: 100px;
      -o-object-fit: contain;
    }
    

    Chris Mills demo's it here http://dev.opera.com/articles/view/css3-object-fit-object-position/

    0 讨论(0)
  • 2020-11-28 05:53

    If using flexbox is a valid option for you (don't need to suport old browsers), check my other answer here (which is possibly a duplicate of this one):

    Basically you'd need to wrap your img tag in a div and your css would look like this:

    .img__container {
        display: flex;
        padding: 15px 12px;
        box-sizing: border-box;
        width: 400px; height: 200px;
    
        img {
            margin: auto;
            max-width: 100%;
            max-height: 100%;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:54

    Load the image as a background for a div.

    Instead of:

    <img id='logo' src='picture.jpg'>
    

    do

    <div id='logo' style='background:url(picture.jpg)'></div>
    

    All browsers will crop the part of the image that doesn't fit.
    This has several advantages over wrapping it an element whose overflow is hidden:

    1. No extra markup. The div simply replaces the img.
    2. Easily center or set the image to another offset. eg. url(pic) center top;
    3. Repeat the image when small enough. (OK, dunno why you would want that)
    4. Set a bg color in the same statement, easily apply the same image to multiple elements, and everything that applies to bg images.

    Update: This answer is from before object-fit; you should now probably use object-fit/object-position.

    It is still useful for older browsers, for extra properties (such as background-repeat), and for edge cases (For example, workaround Chrome bugs with flexbox and object-position and FF's (former?) issues with grid + autoheight + object-fit. Wrapper divs in grid / flexbox often give... unintuitive results.)

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