想设置一个div居中,使用padding:0 auto;width:1000px;
为什么不能是元素居中呢?
因为使用margin:0 auto;的话,居中的两边背景色会用空白·
想设置一个div居中,使用padding:0 auto;width:1000px;
为什么不能是元素居中呢?
因为使用margin:0 auto;的话,居中的两边背景色会用空白·
padding
是不可以让内容居中的;margin: 0 auto; width: 1000px;
可以让div居中,必要条件就是 必须要规定盒子的宽度;display: inline-block;
,然后在父div加上 text-align: center;
也会让div居中;示例:
html
<style> .wrapper { text-align: center; } .wrapper >.center-div { display: inline-block; } </style> <div class="wrapper"> <div class="center-div"> 我是居中的 </div> </div>
10 回答11.1k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
5 回答2k 阅读
具体原因不知道怎么说明,不过在w3.org上有一个说明:http://www.w3.org/TR/CSS21/visudet.html#blockwidth
然后在http://www.w3help.org/有一篇类似相关的文章,不知道是否能帮你 http://www.w3help.org/zh-cn/causes/RD8028