CSS Display an Image Resized and Cropped

前端 未结 19 1574
时光取名叫无心
时光取名叫无心 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:46
    img {
        position: absolute;
        clip: rect(0px,60px,200px,0px);
    } 
    
    0 讨论(0)
  • 2020-11-22 08:46

    If you are using Bootstrap, try using { background-size: cover; } for the <div> maybe give the div a class say <div class="example" style=url('../your_image.jpeg');> so it becomes div.example{ background-size: cover}

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

    With CSS3 it's possible to change the size of a background-image with background-size, fulfilling both goals at once.

    There are a bunch of examples on css3.info.

    Implemented based on your example, using donald_duck_4.jpg. In this case, background-size: cover; is just what you want - it fits the background-image to cover the entire area of the containing <div> and clips the excess (depending on the ratio).

    .with-bg-size {
      background-image: url('https://i.stack.imgur.com/wPh0S.jpg');
      width: 200px;
      height: 100px;
      background-position: center;
      /* Make the background image cover the area of the <div>, and clip the excess */
      background-size: cover;
    }
    <div class="with-bg-size">Donald Duck!</div>

    css3 background-image background-size

    0 讨论(0)
  • 2020-11-22 08:47
    img {
      position: absolute;
      clip: rect(0px, 140px, 140px, 0px);
    }
    
    <img src="w3css.gif" width="100" height="140" />
    
    0 讨论(0)
  • 2020-11-22 08:49

    What I've done is to create a server side script that will resize and crop a picture on the server end so it'll send less data across the interweb.

    It's fairly trivial, but if anyone is interested, I can dig up and post the code (asp.net)

    0 讨论(0)
  • 2020-11-22 08:53
    <p class="crop"><a href="http://templatica.com" title="Css Templates">
        <img src="img.jpg" alt="css template" /></a></p> 
    

    .crop {
        float: left;
        margin: .5em 10px .5em 0;
        overflow: hidden; /* this is important */
        position: relative; /* this is important too */
        border: 1px solid #ccc;
        width: 150px;
        height: 90px;
    }
    .crop img {
        position: absolute;
        top: -20px;
        left: -55px;
    }
    
    0 讨论(0)
提交回复
热议问题