一个html中只有一个p。
<p class="test">
</p>
css1:
.test{
width:200px;
height:200px;
border:1px solid red;
margin:0 auto;
}
css1可以使p.test左右居中。
css2:
.test{
width:200px;
height:200px;
border:1px solid red;
margin:auto auto;
}
为何css2不可以使p.test上下左右居中??
请不要回答如何使他p.test上下左右居中,请回答为何margin:auto auto;不能上下左右居中??
大家讲道理2017-06-24 09:44:38
其他属性的使用值必须满足以下约束:
'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = 包含块的宽度
如果 'margin-left' 和 'margin-right' 都为 'auto',则它们的使用值相等。这使元素相对于包含块的边缘水平居中。
下面是关于高度的:
如果 'margin-top' 或 'margin-bottom' 为 'auto',则它们的使用值为 0。如果 'height' 为 'auto',则高度取决于该元素是否有任何块级子元素以及是否它有填充或边框:
因为规范就是这么规定的算法,浏览器就是这么实现的。
CSS规范