padding:0 auto;不能使内容居中呢?

发布于 2022-09-01 12:23:08 字数 106 浏览 12 评论 0

想设置一个div居中,使用padding:0 auto;width:1000px;
为什么不能是元素居中呢?
因为使用margin:0 auto;的话,居中的两边背景色会用空白·

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

缪败 2022-09-08 12:23:08

具体原因不知道怎么说明,不过在w3.org上有一个说明:http://www.w3.org/TR/CSS21/visudet.html#blockwidth

If both 'margin-left' and 'margin-right' are 'auto', their used values are equal. This horizontally centers the element with respect to the edges of the containing block.

然后在http://www.w3help.org/有一篇类似相关的文章,不知道是否能帮你 http://www.w3help.org/zh-cn/causes/RD8028

夏夜暖风 2022-09-08 12:23:08
  1. padding 是不可以让内容居中的;
  2. margin: 0 auto; width: 1000px; 可以让div居中,必要条件就是 必须要规定盒子的宽度;
  3. 你也可以把要居中的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>
蓝咒 2022-09-08 12:23:08

padding能设置为auto吗? 只有margin-left,margin-right,width能设置为auto

浅忆 2022-09-08 12:23:08

body 给个背景色

酸甜透明夹心 2022-09-08 12:23:08
<div style="padding:0 auto">
        <p>This is a div!</p>
</div>

元素可以居中

所以LZ到底想实现什么效果

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文