Responsive vertical center with overflow hidden

后端 未结 5 1100
北荒
北荒 2021-02-01 07:27

After searching both Stack Overflow and Google I still wonder how to vertical center a image that is bigger than it\'s parent element. I use no height, just max-height, because

5条回答
  •  北海茫月
    2021-02-01 08:05

    I found a way to make it work with only a max-height (as opposed to a fixed height) set on the container.

    The bad news is that it requires an additional wrapper element.

    HTML:

    CSS:

    .img-wrapper {
        overflow: hidden;
        max-height: 425px;
    }
    
    .img-container {
        max-height: inherit;
        transform: translateY(50%);
    }
    
    .img-wrapper img {
        display: block;
        transform: translateY(-50%);
    }
    

提交回复
热议问题