<div class="parent">
<img src="" class="midImg"/>
</div>.parent{
width: 500px;
height: 500px;
position: relative;
}
.parent img{
width: 200px;
height:200px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%); // margin-left:-100px;margin-top:-100px;
}
居中图片样式
本文介绍了一种使用CSS实现图片在父容器中水平垂直居中的方法。通过设置父元素的宽度、高度及相对定位,结合子元素(图片)的绝对定位与transform属性,使图片能够精确地居中显示。
6万+

被折叠的 条评论
为什么被折叠?



