Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
非常简单的问题:居中对齐没有设置宽度的 div 的正确方法是什么?为 div 执行此操作:
.class div{ margin: 0 auto; width: 300px; }
将 div 水平居中对齐,但如果删除设置的宽度则不会。
但是,这确实有效,但它是正确的方法吗?
.class div{ margin: 0 auto; display: table; }
谢谢!
像这样写:
HTML
<div class="parent"> <div class="class"></div> </div>
CSS
.parent{ text-align:center; } .class{ display:inline-block; text-align:left; }