center image in div with overflow hidden

后端 未结 12 1676
攒了一身酷
攒了一身酷 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:38

    None of the above solutions were working out well for me. I needed a dynamic image size to fit in a circular parent container with overflow:hidden

    .circle-container {
        width:100px;
        height:100px;
        text-align:center;
        border-radius:50%;
        overflow:hidden;
    }
    
    .circle-img img {
      min-width:100px;
      max-width:none;
      height:100px;
      margin:0 -100%;
    }
    

    Working example here: http://codepen.io/simgooder/pen/yNmXer

提交回复
热议问题