赞
踩
设置父元素的text-align:center
.box {
width: 300px;
height: 300px;
margin: 100px auto;
text-align: center;
background-color: pink;
}
当块级元素设置了明确的宽度数值时,可以使用margin: 0 auto
<div class="container">
<div class="box"></div>
</div>
.container { position: relative; width: 300px; height: 300px; background-color: orange; } .box { position: absolute; left: 0; right: 0; width: 100px; height: 100px; background-color: pink; }
<div class="container">
<div class="box"></div>
</div>
.container {
display: flex;
justify-content: center;
width: 300px;
height: 300px;
background-color: orange;
}
.box {
width: 100px;
height: 100px;
background-color: pink;
}
.container {
width: 300px;
height: 300px;
background-color: orange;
}
.box {
position: relative;
left: 50%;
width: 100px;
height: 100px;
background-color: pink;
transform: translateX(-50%);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。