center image in div with overflow hidden

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

    Most recent solution:

    HTML

    CSS

    .parent {
        width: 200px;
        height: 200px;
        overflow: hidden;
        /* Magic */
        display: flex;
        align-items: center; /* vertical */
        justify-content: center; /* horizontal */
    }
    

提交回复
热议问题