我只是好奇为什么HTML中的<center>标记被弃用了。

<center>是一种通过将容器封装在<center>标签中来快速居中对齐文本和图像块的简单方法,现在我真的找不到任何更简单的方法了。

有人知道任何简单的方法如何居中“东西”(不是左边距:auto;margin-right:汽车;width),替换<center> ?还有,为什么它被弃用了?


当前回答

CSS有一个text-align: center属性,因为这是一个纯粹的视觉东西,而不是语义,它应该归为CSS,而不是HTML。

其他回答

如果您愿意,您仍然可以将其用于XHTML 1.0 Transitional和HTML 4.01 Transitional。唯一的另一种方法(在我看来最好的方法)是使用边缘:

<div style="width:200px;margin:auto;">
  <p>Hello World</p>
</div>

HTML应该定义元素,而不是控制它的表示。

我有时仍然使用<center>标签,因为CSS中的任何东西都不能很好地工作。尝试使用<div>技巧而失败的例子:

<div style="text-align: center;">This div is centered, but it's a simple example.</div> <br /> <div style="text-align: center;"><table border="1"><tr><td>&lt;div style="text-align: center;"&gt; didn't center correctly.</td></tr></table></div> <br /> <div style="text-align: center;margin-left:auto;margin-right:auto"><table border="1"><tr><td>&lt;div style="text-align: center;margin-left:auto;margin-right:auto"&gt; still didn't center either</td></tr></table></div> <br /> <center><table border="1"><tr><td>Actually Centered with &lt;center&gt; tag</td></tr></table></center>

<center> gets results. To use CSS instead, you sometimes have to put CSS in several places and mess with it to get it to center right. To answer your question, CSS has become a religion with believers and followers who shunned <center> <b> <i> <u> as blasphemy, unholy, and much too simple for the sake of their own job security. And if they try to take your <table> away from you, ask them what the CSS equivalent of the colspan or rowspan attribute is. It is not the abstract or bookish truth, but the lived truth that counts. -- Zen

Center在HTML中很受欢迎,因为它是一种快速创建工作原型的方法,而无需在工作的早期阶段设置CSS。当然,与使用CSS为各种元素创建全局中心相比,在完成的页面上使用中心变得很麻烦。因此,center将永远在网络上有一席之地,仍然有一些老派论坛在他们的帖子中以这种格式为中心的元素。

换句话说,喜欢中心标签,因为当你需要它的时候,它很可爱,很高效!

PS: Marquee也是一个经典的HTML标签,它仍然存在,看起来非常棒^__^

对于文本和图像,您可以使用text-align:

<div style="text-align: center;">
    I'm centered.
</div>

我所做的是采取常见的任务,如居中或浮动,并从中创建CSS类。当我这样做时,我可以在任何页面中使用它们。我还可以在同一个元素上调用任意多个函数。

.text_center {text-align: center;}
.center {margin: auto 0px;}
.float_left {float: left;}

现在我可以在HTML代码中使用它们来执行简单的任务。

<p class="text_center">Some Text</p>