在不知道宽度的情况下对齐DIV标签

时间:2011-11-18 07:53:43

标签: html css center

  

可能重复:
  How can I center something if I don't know ahead of time what the width is?

我想知道如何在知道宽度的情况下对齐div标签。这与其他问题不同,因为我不介意使用<center>标记,但它似乎已经过了很长时间。在HTML组视图中。 <center>标记将来是否仍在使用?

由于

3 个答案:

答案 0 :(得分:6)

我通常使用width:autowidth:100%而不是margin:0 auto;

但是,您可以在下面的链接中看到与您的问题几乎相同的详细信息!

How can I center align a div without knowing the width?

答案 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;
}

检查

http://jsfiddle.net/efEgq/2/

相关问题