CSS Display an Image Resized and Cropped

前端 未结 19 1576
时光取名叫无心
时光取名叫无心 2020-11-22 08:09

I want to show an image from an URL with a certain width and height even if it has a different size ratio. So I want to resize (maintaining the ratio) and then cut the imag

相关标签:
19条回答
  • 2020-11-22 08:55

    Did you try to use this?

    .centered-and-cropped { object-fit: cover }
    

    I needed to resize image, center (both vertically and horizontally) and than crop it.

    I was happy to find, that it could be done in a single css-line. Check the example here: http://codepen.io/chrisnager/pen/azWWgr/?editors=110


    Here is the CSS and HTMLcode from that example:

    .centered-and-cropped { object-fit: cover }
    <h1>original</h1>
    <img height="200" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3174/bear.jpg" alt="Bear">
        
    <h1>object-fit: cover</h1>
    <img class="centered-and-cropped" width="200" height="200" 
    style="border-radius:50%" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3174/bear.jpg" alt="Bear">

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

    I needed to do this recently. I wanted to make a thumbnail-link to a NOAA graph. Since their graph could change at any time, I wanted my thumbnail to change with it. But there's a problem with their graph: it has a huge white border at the top, so if you just scale it to make the thumbnail you end up with extraneous whitespace in the document.

    Here's how I solved it:

    http://sealevel.info/example_css_scale_and_crop.html

    First I needed to do a little bit of arithmetic. The original image from NOAA is 960 × 720 pixels, but the top seventy pixels are a superfluous white border area. I needed a 348 × 172 thumbnail, without the extra border area at the top. That means the desired part of the original image is 720 - 70 = 650 pixels high. I needed to scale that down to 172 pixels, i.e., 172 / 650 = 26.5%. That meant 26.5% of 70 = 19 rows of pixels needed to be deleted from the top of the scaled image.

    So…

    1. Set the height = 172 + 19 = 191 pixels:

      height=191

    2. Set the bottom margin to -19 pixels (shortening the image to 172 pixels high):

      margin-bottom:-19px

    3. Set the top position to -19 pixels (shifting the image up, so that the top 19 pixel rows overflow & are hidden instead of the bottom ones):

      top:-19px

    The resulting HTML looks like this:

    <a href="…" style="display:inline-block;overflow:hidden">
    <img width=348 height=191 alt=""
    style="border:0;position:relative;margin-bottom:-19px;top:-19px"
    src="…"></a>
    

    As you can see, I chose to style the containing <a> tag, but you could style a <div>, instead.

    One artifact of this approach is that if you show the borders, the top border will be missing. Since I use border=0 anyhow, that wasn't an issue for me.

    0 讨论(0)
  • 2020-11-22 08:56
    <div class="crop">
        <img src="image.jpg"/>
    </div>
    
    .crop {
      width: 200px;
      height: 150px;
      overflow: hidden;
    }
    .crop img {
      width: 100%;
      /*Here you can use margins for accurate positioning of cropped image*/
    }
    
    0 讨论(0)
  • 2020-11-22 08:57

    Live Example: https://jsfiddle.net/de4Lt57z/

    HTML:

    <div class="crop">
      <img src="example.jpg" alt="..." />
    </div>
    

    CSS:

        .crop img{
          width:400px;
          height:300px;
          position: absolute;
          clip: rect(0px,200px, 150px, 0px);
          }
    

    Explanation: Here image is resized by width and height value of the image. And crop is done by clip property.

    For details about clip property follow: http://tympanus.net/codrops/2013/01/16/understanding-the-css-clip-property/

    0 讨论(0)
  • 2020-11-22 09:00
    .imgContainer {
      overflow: hidden;
      width: 200px;
      height: 100px;
    }
    .imgContainer img {
      width: 200px;
      height: 120px;
    }
    
    <div class="imgContainer">
      <img src="imageSrc" />
    </div>
    

    The containing div with essentially crop the image by hiding the overflow.

    0 讨论(0)
  • 2020-11-22 09:01

    You can put the img tag in a div tag and do both, but I would recommend against scaling images in the browser. It does a lousy job most of the time because browsers have very simplistic scaling algorithms. Better to do your scaling in Photoshop or ImageMagick first, then serve it up to the client nice and pretty.

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