javascript - 为何margin:auto auto 不能上下左右对齐?
本文介绍了javascript - 为何margin:auto auto 不能上下左右对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
一个html中只有一个div。
<div class="test">
</div>
css1:
.test{
width:200px;
height:200px;
border:1px solid red;
margin:0 auto;
}
css1可以使div.test左右居中。
css2:
.test{
width:200px;
height:200px;
border:1px solid red;
margin:auto auto;
}
为何css2不可以使div.test上下左右居中??
请不要回答如何使他div.test上下左右居中,请回答为何margin:auto auto;不能上下左右居中??
解决方案
这是个好问题,那我问你,当一行存在两个元素的时候,你可以左右居中吗?同理当你的上下都可能有元素存在的情况下你要浏览器怎么去计算对应元素应该存在的位置?
这篇关于javascript - 为何margin:auto auto 不能上下左右对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文