宽高未知
在不知道div或者其他标签宽度的时候,在完成在父级元素中居中.
.father {
position: relative;
}
.child {
position:absolute;
left:50%;
width:fit-content;
height:100%;
transform:translateX(-50%);
-webkit-transform:translateX(-50%);
-moz-transform:translateX(-50%);//这里的50%是指盒子本身的50%;
}
常用方法一
.father {
position: relative;
}
.child {
width:100px;
height:100px;
margin:auto;
position:absolute;
top:0px;
right:0px;
bottom:0px;
left:0px;
}
常用方法二
.father {
position: relative;
}
.child {
width:100px;
height:100px;
margin:auto;
position:absolute;
top:50%;
margin-top:50%;
}