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

发布于 2024-08-12 06:29:11 字数 386 浏览 2 评论 0原文

我只是好奇为什么 HTML 中的

标签被弃用。

是一种通过将容器封装在
标记中来快速居中对齐文本和图像块的简单方法,我真的找不到现在有什么更简单的方法可以做到这一点。

任何人都知道如何将“东西”居中(不是 margin-left:auto; margin-right:auto; 和 width 的东西)的简单方法,可以替换

; ?而且,为什么它被弃用了?

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

離殇 2024-08-19 06:29:11

元素已被弃用,因为它定义了其内容的表示 - 它不描述其内容。

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

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

One method of centering is to set the margin-left and margin-right properties of the element to auto, and then set the parent element’s text-align property to center. This guarantees that the element will be centered in all modern browsers.

桜花祭 2024-08-19 06:29:11

最不受欢迎的答案

  1. 已弃用的标签不一定是标签;它是一个好的标签。
  2. 标签处理表示逻辑,center就是其中之一;
  3. center 标签与带有 text-align:center 的 div 不同;
  4. 事实上,您有另一种方式来做某事并不意味着它无效。

让我解释一下,因为这里有臭名昭著的反对者,他们会认为我在捍卫老式 HTML4 之类的东西。不,我不是。但围绕 center 的争论只是一场趋势之战,没有真正的理由放弃一个能很好地服务于有效目的的标签。

那么让我们看看反对它的主要论点。

  • “它描述的是表示,而不是语义!”
    不。它描述了一种逻辑排列 - 是的,它具有默认外观,就像

      等其他标签一样。但重点是封闭部分与其周围环境的关系。中心说“这是我们分开的东西
      视觉上不同的定位”。

  • “无效”
    是的。它只是已被弃用,例如稍后可以删除。至今已超过 15 年。显然,它不会去任何地方。有一些主要网站(包括 google.com)使用此标记,因为它非常可读且切中要害 - 这与我们喜欢 HTML5 标记的原因相同。

  • “HTML5 不支持”
    实际上,它是最受广泛支持的标签之一。 MDN 表示“不鼓励使用它,因为它可能随时被删除”——很好,但引用一句经典的话,那一天可能永远不会到来。 Center 已在 2004 年左右被弃用 - 它仍然存在并且仍然有用。

  • “这是老派”
    鞋带也很老派。新方法不会使旧方法失效。你想要感觉进步和现代:很好。但不要使其成为法律。

  • “这很愚蠢/尴尬/蹩脚/讲述了一个关于你的故事”
    这些都没有。它就像一把锤子:用于特定工作的工具。同一工作有其他工具,同一工具有其他工作;但它是为了解决某个问题而创建的,而这个问题仍然存在,所以我们不妨使用专用的解决方案。

  • “你不应该这样做,因为 CSS”
    居中绝对可以通过 CSS 来实现,但这只是一种方法,而不是唯一的方法,更不用说唯一合适的方法。任何受支持、工作且可读的东西都应该可以使用。另外,同样的争论发生在 Flexbox 和 CSS 网格之前,这很有趣,因为当时没有 CSS 方法来实现 center 的功能。不,text-align:center 不一样。不,margin:auto 不一样。在 Flexbox 之前反对中心标签的人只是对 CSS 了解不够。

长话短说:

不使用

的唯一原因是人们会讨厌你。

The Least Popular Answer

  1. A deprecated tag is not necessarily a bad tag;
  2. There are tags that deal with presentation logic, center is one of them;
  3. A center tag is not the same as a div with text-align:center;
  4. The fact that you have another way to do something doesn't make it invalid.

Let me explain because there are notorious downvoters here who will think I'm defending oldschool HTML4 or something. No I'm not. But the debate around center is simply a trend war, there is no real reason to ditch a tag that serves a valid purpose well.

So let's see the major arguments against it.

  • "It describes presentation, not semantics!"
    No. It describes a logical arrangement - and yes, it has a default appearance, just as other tags like <p> or <ul> do. But the point is the enclosed part's relation to its surroundings. Center says "this is something we separate by
    visually different positioning".

  • "It's not valid"
    Yes it is. It's just deprecated, as in, could be removed later. For 15+ years now. And it's not going anywhere, apparently. There are major sites (including google.com) that use this tag because it's very readable and to the point - and those are the same reasons we like HTML5 tags for.

  • "It's not supported in HTML5"
    It's one of the most widely supported tags, actually. MDN says "its use is discouraged since it could be removed at any time" - good point, but that day may never come, to quote a classic. Center was already deprecated in like 2004 or so - it's still here and still useful.

  • "It's oldschool"
    Shoelaces are oldschool too. New methods don't invalidate the old. You want to feel progressive and modern: fine. But don't make it the law.

  • "It's stupid / awkward / lame / tells a story about you"
    None of these. It's like a hammer: one tool for a specific job. There are other tools for the same job and other jobs for the same tool; but it was created to solve a certain problem and that problem is still around so we might as well just use a dedicated solution.

  • "You shouldn't do this, because, CSS"
    Centering can absolutely be achieved by CSS but that's just one way, not the only one, let alone the only appropriate one. Anything that's supported, working and readable should be ok to use. Also, the same argument happened before flexboxes and CSS grids, which is funny because back then there was no CSS way to achieve what center did. No, text-align:center is not the same. No, margin:auto is not the same. Anyone who argued against center tags before flexbox simply didn't know enough about CSS.

TL;DR:

The only reason not to use <center> is because people will hate you.

橪书 2024-08-19 06:29:11

我仍然使用

有时会使用标签,因为 CSS 中没有任何东西可以正常工作。尝试使用

的示例尽管 W3C 声称

CENTER 元素完全等同于指定 DIV 元素与 align 属性设置为“center”CENTER 元素已已弃用

<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><div style="text-align: center;"> 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><div style="text-align: center;margin-left:auto;margin-right:auto"> still didn't center either</td></tr></table></div>
<br />
<center><table border="1"><tr><td>Actually Centered with <center> tag</td></tr></table></center>
<br />
<div align="center;"><table border="1"><tr><td><div align="center;">, which according to w3c is equivalent, didn't center correctly.</td></tr></table></div>

<中心>得到结果。要使用 CSS,有时您必须将 CSS 放在多个位置并对其进行修改才能使其居中靠右。回答你的问题,CSS 已经成为一种宗教,信徒和追随者都回避

<我> 亵渎、邪恶,而且为了他们自己的工作安全而太过简单。 如果他们试图拿走你的,远离您,询问他们 colspan 或 rowspan 属性的 CSS 等效项是什么。

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

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

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

<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><div style="text-align: center;"> 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><div style="text-align: center;margin-left:auto;margin-right:auto"> still didn't center either</td></tr></table></div>
<br />
<center><table border="1"><tr><td>Actually Centered with <center> tag</td></tr></table></center>
<br />
<div align="center;"><table border="1"><tr><td><div align="center;">, which according to w3c is equivalent, didn't center correctly.</td></tr></table></div>

<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

时间你老了 2024-08-19 06:29:11

根据W3Schools.com

center 元素在 HTML 4.01 中已被弃用,并且在 XHTML 1.0 严格 DTD 中不受支持。

HTML 4.01 规范 给出了弃用该标记的原因:

CENTER 元素与指定 DIV 元素的对齐属性设置为“center”完全相同。

According to W3Schools.com,

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

The HTML 4.01 spec gives this reason for deprecating the tag:

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

近箐 2024-08-19 06:29:11

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

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

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

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

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.

.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.

<p class="text_center">Some Text</p>
心房敞 2024-08-19 06:29:11

您仍然可以将其与 XHTML 1.0 TransitionalHTML 4.01 Transitional 如果您愿意的话。唯一的其他方法(我认为最好的方法)是使用边距:

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

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

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

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

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

我很坚强 2024-08-19 06:29:11

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

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.

指尖微凉心微凉 2024-08-19 06:29:11

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

让标签控制外观的一个问题是,您的页面无法在适合残障人士的设备(例如屏幕阅读器)上正常运行。它还会导致文本中出现大量标签,这些标签无助于澄清含义,反而会用不同级别的信息使文本变得混乱。

因此,CSS 被认为是将格式/显示移动到另一种语言,该语言与文本分开,并且可以轻松地保持这种方式。除此之外,这允许切换样式表来更改网页的外观,而无需触及其他标记。并且能够一次性完成很多页面的操作。

CSS 为您提供的执行此操作的工具并不总是很优雅,但我站在您这边。例如,没有办法进行有效的垂直居中。水平居中,如果不仅仅是文本适合文本对齐,也好不了多少。

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

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.

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.

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.

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.

极度宠爱 2024-08-19 06:29:11

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

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

For text and images you can use text-align:

<div style="text-align: center;">
    I'm centered.
</div>
银河中√捞星星 2024-08-19 06:29:11

您可以将其添加到您的 CSS 中并使用

.center{
  text-align: center;
  margin: auto;
  justify-content: center;
  display: flex;
}

或者如果您想保留

< /code> 并做好万一被删除的准备,将其添加到您的 css 中

center{
  text-align: center;
  margin: auto;
  justify-content: center;
  display: flex;
}

You can add this to your css and use <div class="center"></div>

.center{
  text-align: center;
  margin: auto;
  justify-content: center;
  display: flex;
}

or if you want to keep <center></center> and be prepared in case its ever removed, add this to your css

center{
  text-align: center;
  margin: auto;
  justify-content: center;
  display: flex;
}
荒路情人 2024-08-19 06:29:11

引人深思:文本到语音合成器会用

做什么?

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

念﹏祤嫣 2024-08-19 06:29:11

CSS 有一个 text-align: center 属性,由于这纯粹是一个视觉事物,而不是语义,所以它应该归入 CSS,而不是 HTML。

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

趁年轻赶紧闹 2024-08-19 06:29:11

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

换句话说,喜欢中心标签,因为当您需要它时它既可爱又高效!

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

Center is loved in HTML because it is a fast way to prototype your work without the hassle of setting up the CSS in the early stages of your work. Of course, having center used on your finished page becomes a chore in comparison to using CSS to create global centers for various elements. Thus, center will always have a place on the web, and there are still some old school forums around that format centered elements in their posts this way.

In other words, Love the center tag because it's cute and efficient when you need it!

PS: Marquee is also a classic HTML tag that is still around and that looks freaking awesome ^__^

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文