center image in div with overflow hidden

后端 未结 12 1698
攒了一身酷
攒了一身酷 2020-12-08 18:59

I have an image of 400px and a div that is smaller (the width is not always 300px as in my example). I want to center the image in the div, and if there is an overflow, hide

12条回答
  •  无人及你
    2020-12-08 19:58

    this seems to work on our site, using your ideas and a little math based upon the left edge of wrapper div. It seems redundant to go left 50% then take out 50% extra margin, but it seems to work.

    div.ImgWrapper {
        width: 160px;
        height: 160px
        overflow: hidden;
        text-align: center;
    }
    
    img.CropCenter {
        left: 50%;
        margin-left: -100%;
        position: relative;
        width: auto !important;
        height: 160px !important;
    }
    
    

提交回复
热议问题