CSS force image resize and keep aspect ratio

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

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

\"\"

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

    I've struggled with this problem quite hard, and eventually arrived at this simple solution:

    object-fit: cover;
    width: 100%;
    height: 250px;
    

    You can adjust the width and height to fit your needs, and the object-fit property will do the cropping for you.

    More information about the possible values for the object-fit property and a compatibility table are available here: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit

    Cheers.

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

    The background-size property is ie>=9 only, but if that is fine with you, you can use a div with background-image and set background-size: contain:

    div.image{
        background-image: url("your/url/here");
        background-size: contain;
        background-repeat: no-repeat;
        background-position: center;
    }
    

    Now you can just set your div size to whatever you want and not only will the image keep its aspect ratio it will also be centralized both vertically and horizontally within the div. Just don't forget to set the sizes on the css since divs don't have the width/height attribute on the tag itself.

    This approach is different than setecs answer, using this the image area will be constant and defined by you (leaving empty spaces either horizontally or vertically depending on the div size and image aspect ratio), while setecs answer will get you a box that exactly the size of the scaled image (without empty spaces).

    Edit: According to the MDN background-size documentation you can simulate the background-size property in IE8 using a proprietary filter declaration:

    Though Internet Explorer 8 doesn't support the background-size property, it is possible to emulate some of its functionality using the non-standard -ms-filter function:

    -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='path_relative_to_the_HTML_file', sizingMethod='scale')";
    
    0 讨论(0)
  • 2020-11-22 10:54

    To force image that fit in a exact size, you don't need to write too many codes. It's so simple

    img{
        width: 200px;
        height: auto;
        object-fit: contain; /* Fit logo in the image size */
            -o-object-fit: contain; /* Fit logo fro opera browser */
        object-position: top; /* Set logo position */
            -o-object-position: top; /* Logo position for opera browser */
        }
    <img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png" alt="Logo">

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

    The solutions below will allow scaling up and scaling down of the image, depending on the parent box width.

    All images have a parent container with a fixed width for demonstration purposes only. In production, this will be the width of the parent box.

    Best Practice (2018):

    This solution tells the browser to render the image with max available width and adjust the height as a percentage of that width.

    .parent {
      width: 100px;
    }
    
    img {
      display: block;
      width: 100%;
      height: auto;
    }
    <p>This image is originally 400x400 pixels, but should get resized by the CSS:</p>
    <div class="parent">
      <img width="400" height="400" src="https://placehold.it/400x400">
    </div>

    Fancier Solution:

    With the fancier solution, you'll be able to crop the image regardless of its size and add a background color to compensate for the cropping.

    .parent {
      width: 100px;
    }
    
    .container {
      display: block;
      width: 100%;
      height: auto;
      position: relative;
      overflow: hidden;
      padding: 34.37% 0 0 0; /* 34.37% = 100 / (w / h) = 100 / (640 / 220) */
    }
    
    .container img {
      display: block;
      max-width: 100%;
      max-height: 100%;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
    }
    <p>This image is originally 640x220, but should get resized by the CSS:</p>
    <div class="parent">
      <div class="container">
        <img width="640" height="220" src="https://placehold.it/640x220">
      </div>
    </div>

    For the line specifying padding, you need to calculate the aspect ratio of the image, for example:

    640px (w) = 100%
    220px (h) = ?
    
    640/220 = 2.909
    100/2.909 = 34.37%
    

    So, top padding = 34.37%.

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

    How about using a pseudo element for vertical alignment? This less code is for a carousel but i guess it works on every fixed size container. It will keep the aspect ratio and insert @gray-dark bars on top/bottom or left/write for the shortest dimension. In the meanwhile the image is centered horizontally by the text-align and vertically by the pseudo element.

        > li {
          float: left;
          overflow: hidden;
          background-color: @gray-dark;
          text-align: center;
    
          > a img,
          > img {
            display: inline-block;
            max-height: 100%;
            max-width: 100%;
            width: auto;
            height: auto;
            margin: auto;
            text-align: center;
          }
    
          // Add pseudo element for vertical alignment of inline (img)
          &:before {
            content: "";
            height: 100%;
            display: inline-block;
            vertical-align: middle;
          }
        }
    
    0 讨论(0)
  • 2020-11-22 10:58

    img {
      display: block;
      max-width:230px;
      max-height:95px;
      width: auto;
      height: auto;
    }
    <p>This image is originally 400x400 pixels, but should get resized by the CSS:</p>
    <img width="400" height="400" src="http://i.stack.imgur.com/aEEkn.png">

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

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