css-使不同大小的图片在固定大小的容器中居中

样图
这里写图片描述
- html代码

<body>
    <div class="box">
        <img src="img/bar/banner1.png"/>
    </div>
    <div class="box">
        <img src="img/bar/index-img1.png"/>
    </div>
    <div class="box">
        <img src="img/img.png"/>
    </div>
</body>
  • css代码
.box{
    float: left;
    border: 1px solid red;
    margin: 10px;
    /*上面的样式是为了加一些区别的效果*/
    position: relative;
    width: 200px;
    height: 200px;
}
.box>img{
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    max-width: 100%;
    max-height: 100%;
}

猜你喜欢

转载自blog.csdn.net/li522021642/article/details/80058256