可能重复:
How can I center something if I don't know ahead of time what the width is?
我想知道如何在知道宽度的情况下对齐div标签。这与其他问题不同,因为我不介意使用<center>
标记,但它似乎已经过了很长时间。在HTML组视图中。 <center>
标记将来是否仍在使用?
由于
答案 0 :(得分:6)
我通常使用width:auto
或width:100%
而不是margin:0 auto
;
但是,您可以在下面的链接中看到与您的问题几乎相同的详细信息!
答案 1 :(得分:3)
通过将左右边距设置为自动来对块元素进行居中。
margin-left: auto;
margin-right: auto;
<center>
工作在可预见的将来继续工作,因此没有技术理由不使用它,但是在现代Web开发中,应该承认“separation of presentation and content”的原则并使用HTML布局的语义和CSS。
答案 2 :(得分:1)
这样写:
#parent {
text-align:center;
}
.child {
display:inline-block;
*display:inline /* IE */
*zoom:1;/* IE */
text-align:left;
}
检查