顶宽居中:
只有块元素:才能实现这样效果
子元素在父容器中:水平方向居中;
1:给当前元素设置固定宽度
2:让当前元素左右外边距自动
html,body{
height: 100%;
}
.box{
width: 200px;
height: 200px;
border: 2px solid red;
/* 顶宽居中 */
/* 设置上下左右外边距 上下为0 左右自动 */
/* margin: 0px auto; */
/* 设置左外边距 */
margin-left: auto;
/* 设置右外边距 */
margin-right: auto;
}
.wrap{
width: 500px;
height: 500px;
background-color: red;
}
.inset{
width: 200px;
height: 200px;
background-color: black;
margin: 20px auto;
}
<body>
<div class="box"></div>
<div class="wrap">
<div class="inset">
</div>
</div>
</body>