Crop image in CSS

后端 未结 8 591
情书的邮戳
情书的邮戳 2021-01-12 07:23

I\'ve created a two-column grid of images, which works fine with all-landscape images: Link. However, I\'ve added a portrait image that throws off the layout, so I\'d like t

8条回答
  •  感情败类
    2021-01-12 08:03

    You can crop imgs like this:

    CSS:

    .crop-container {
        width: 200px;
        height: 300px;
        overflow: hidden;
    }
    .crop-container img {
        margin-top: -100px;
        margin-left: -200px;
    }
    

    Adjust the height and width of the container to adjust the dimensions of the cropped img and adjust the amount of negative margin-top and margin-left on the img element itself to choose which part of the image to crop to.

    HTML:

    Working Fiddle

    enter image description here

    EDIT: Alternative solution for a 2 column grid with fixed height rows:

    CSS:

    body {
        margin: 0;
    }
    div.img {
        float: left;
        width: 49%;
        margin: 0.5%;
        height: 100%;
        background-size: cover!important;
        background-repeat: no-repeat!important;
    }
    div.row {
        height: 300px;
    }
    

    HTML:

     
     
     
     

    Working Fiddle

提交回复
热议问题