Center Oversized Image in Div

前端 未结 12 1637
予麋鹿
予麋鹿 2020-11-27 08:52

I have been trying to sort out how to center an oversized image within a div using css only.

We are using a fluid layout, so the width of the image containers varies

12条回答
  •  借酒劲吻你
    2020-11-27 09:22

    Late to the game, but I found this method is extremely intuitive. https://codepen.io/adamchenwei/pen/BRNxJr

    CSS

    .imageContainer {
      border: 1px black solid;
    
      width: 450px;
      height: 200px;
      overflow: hidden;
    }
    .imageHolder {
      border: 1px red dotted;
    
      height: 100%;
      display:flex;
      align-items: center;
    }
    .imageItself {
      height: auto;
      width: 100%;
      align-self: center;
    
    }
    

    HTML

提交回复
热议问题