想设置一个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.7k 阅读
2 回答3.2k 阅读✓ 已解决
2 回答4.3k 阅读✓ 已解决
5 回答2.2k 阅读
3 回答1.9k 阅读✓ 已解决
2 回答1.7k 阅读✓ 已解决
4 回答2.5k 阅读✓ 已解决
具体原因不知道怎么说明,不过在w3.org上有一个说明:http://www.w3.org/TR/CSS21/visudet.html#blockwidth
然后在http://www.w3help.org/有一篇类似相关的文章,不知道是否能帮你 http://www.w3help.org/zh-cn/causes/RD8028