为什么 HTML 中不推荐使用 <center> 标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1798817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:23:38  来源:igfitidea点击:

Why is the <center> tag deprecated in HTML?

htmltagscenterdeprecated

提问by Andreas Grech

I am just curious as to why the <center>tag in HTML was deprecated.

我只是好奇为什么<center>不推荐使用 HTML 中的标签。

The <center>was a simple way of quickly center-aligning blocks of text and images by encapsulating the container in a <center>tag, and I really cannot find any simpler way on how to do it now.

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

Anyone know of any simple way on how to center "stuff" (not the margin-left:auto; margin-right:auto;and width thing), something that replaces <center>? And also, why was it deprecated?

任何人都知道如何将“东西”居中(不是margin-left:auto; margin-right:auto;和宽度的东西)的任何简单方法,可以代替<center>? 而且,为什么它被弃用了?

回答by Jordan Ryan Moore

The <center>element was deprecated because it defines the presentationof its contents — it does not describe its contents.

<center>元素已被否决,因为它定义了演示文稿的内容-它并没有描述它的内容。

One method of centering is to set the margin-leftand margin-rightproperties of the element to auto, and then set the parent element's text-alignproperty to center. This guarantees that the element will be centered in all modern browsers.

居中的一种方法是将元素的margin-leftmargin-right属性设置为auto,然后将父元素的text-align属性设置为center。这保证了元素将在所有现代浏览器中居中。

回答by Conrad Meyer

According to W3Schools.com,

根据W3Schools.com 的说法,

The center element was deprecated in HTML 4.01, and is not supported in XHTML 1.0 Strict DTD.

在 HTML 4.01 中不推荐使用 center 元素,并且在 XHTML 1.0 Strict DTD 中不受支持。

The HTML 4.01 specgives this reason for deprecating the tag:

HTML 4.01规范给出了这样的理由弃用标签:

The CENTER element is exactly equivalent to specifying the DIV element with the align attribute set to "center".

CENTER 元素完全等同于指定将 align 属性设置为“center”的 DIV 元素。

回答by Scott Radcliff

What I do is take common tasks like centering or floating and make CSS classes out of them. When I do that I can use them throughout any of the pages. I can also call as many as I want on the same element.

我所做的是处理诸如居中或浮动之类的常见任务,并从中生成 CSS 类。当我这样做时,我可以在任何页面中使用它们。我也可以在同一个元素上调用任意多个。

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

Now I can use them in my HTML code to perform simple tasks.

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

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

回答by Russell Hankins

I still use the <center> tag sometimes because nothing in CSS works as well. Examples of trying to use a <div> trick and failing:

有时我仍然使用 <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> 得到结果。要改用 CSS,有时您必须将 CSS 放在多个位置并弄乱它以使其居中正确。为了回答你的问题,CSS 已经成为一种信仰,信徒和追随者都回避 <center> <b> <i> <u> 为亵渎、不圣洁和为了自己的工作安全而过于简单。如果他们试图把你的 <table> 拿走,问问他们 colspan 或 rowspan 属性的 CSS 等价物是什么。

重要的不是抽象的或书本式的真理,而是活生生的真理。
-- 禅

回答by Sampson

You can still use this with XHTML 1.0 Transitionaland HTML 4.01 Transitionalif you like. The only other way (best way, in my opinion) is with margins:

如果您愿意,您仍然可以在XHTML 1.0 TransitionalHTML 4.01 Transitional 中使用它。唯一的另一种方式(我认为最好的方式)是使用边距:

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

Your HTML should define the element, not govern its presentation.

你的 HTML 应该定义元素,而不是管理它的表现。

回答by Carl Smotricz

It's intended that markup, i.e. the HTML tags, represent meaning and structure, not appearance. It was badly mixed up in early versions of HTML but the standards people are trying to clean that up now.

标记(即 HTML 标签)旨在表示含义和结构,而不是外观。它在 HTML 的早期版本中被严重混淆,但人们现在正试图清理它。

One problem with letting tags control appearance is that your pages don't play well with devices for the handicapped, such as screen readers. It also leads to having lots and lots of tags in your text that don't help clarify the meaning, but rather clutter it with information of a different level.

让标签控制外观的一个问题是您的页面不能很好地与残疾人设备(例如屏幕阅读器)配合使用。它还导致您的文本中有很多标签,这些标签无助于阐明含义,而是用不同级别的信息将其弄乱。

So CSS was thought up to move formatting/display to a different language, which is separate from the text and can easily be kept that way. Among other things, this allows switching stylesheets to change the appearance of a Web page without touching the other markup. And to be able to do that for lots of pages in one swell foop.

所以 CSS 被认为是将格式/显示移动到不同的语言,它与文本分开并且可以很容易地保持这种方式。除其他外,这允许切换样式表来更改网页的外观,而无需触及其他标记。并且能够在一个膨胀的过程中为很多页面做到这一点。

The tools CSS gives you to do this are not always elegant, I'm on your side there. For instance, there is no way to do effective vertical centering. And horizontal centering, if it's not just text amenable to text-align, is not much better.

CSS 为您提供的工具并不总是优雅的,我支持您。例如,没有办法进行有效的垂直居中。和水平居中,如果它不仅仅是适合于 的文本text-align,也好不到哪里去。

You have the choice of doing easy, effective and muddled or clean, elegant and cumbersome. I don't understand why Web developers put up with this mess, but I guess they're happy to have at least a chance to get their stuff done.

你可以选择做简单、有效和混乱或干净、优雅和繁琐。我不明白为什么 Web 开发人员要忍受这种混乱,但我想他们很高兴至少有机会完成他们的工作。

回答by jkndrkn

HTML is intended for structuring data, not controlling layout. CSS is intended to control layout. You'll also find that many designers frown on using <table>for layouts for this very same reason.

HTML 用于结构化数据,而不是控制布局。CSS 旨在控制布局。您还会发现,<table>出于同样的原因,许多设计师不赞成使用for 布局。

回答by Tomas Markauskas

For text and images you can use text-align:

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

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

回答by keithjgrant

CSS has a text-align: centerproperty, and since this is purely a visual thing, not semantic, it should be relegated to CSS, not HTML.

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

回答by J?rg W Mittag

Food for thought: what would a text-to-speech synthesizer do with <center>?

深思熟虑:文本到语音合成器可以做<center>什么?