Center image element in parent div

后端 未结 4 982
情深已故
情深已故 2020-11-28 14:58

How I can set center an image element inside a parent div? I would like to do this so that the middle of the image is always in the center of his parent. Also I want the ima

4条回答
  •  广开言路
    2020-11-28 15:23

    You'll need to move the text-align: center; from the CSS for the image to the CSS for its parent div, so your CSS looks like this:

    .box {
        height: 100%;
        width: 450px;
        border: 2px solid red;
        background: green;
        overflow: hidden; 
        text-align:center
    }
    
    .box img {
        height: 100%;
        width: auto;
    }
    

    And that's it!

提交回复
热议问题