为什么<中心>是? HTML 中不推荐使用标签?中心>
我只是好奇为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
元素已被弃用,因为它定义了其内容的表示 - 它不描述其内容。
居中的一种方法是将元素的
margin-left
和margin-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
andmargin-right
properties of the element toauto
, and then set the parent element’stext-align
property tocenter
. This guarantees that the element will be centered in all modern browsers.最不受欢迎的答案
center
就是其中之一;center
标签与带有text-align:center
的 div 不同;让我解释一下,因为这里有臭名昭著的反对者,他们会认为我在捍卫老式 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
center
is one of them;center
tag is not the same as a div withtext-align:center
;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 byvisually 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:
我仍然使用
相当于
:
<中心>得到结果。要使用 CSS,有时您必须将 CSS 放在多个位置并对其进行修改才能使其居中靠右。回答你的问题,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>
:<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
根据W3Schools.com,
HTML 4.01 规范 给出了弃用该标记的原因:
According to W3Schools.com,
The HTML 4.01 spec gives this reason for deprecating the tag:
我所做的就是完成居中或浮动等常见任务,并从中创建 CSS 类。当我这样做时,我可以在任何页面中使用它们。我还可以在同一个元素上调用任意多个。
现在我可以在 HTML 代码中使用它们来执行简单的任务。
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.
Now I can use them in my HTML code to perform simple tasks.
您仍然可以将其与 XHTML 1.0 Transitional 和 HTML 4.01 Transitional 如果您愿意的话。唯一的其他方法(我认为最好的方法)是使用边距:
您的 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:
Your HTML should define the element, not govern its presentation.
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.标记(即 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.
对于文本和图像,您可以使用
text-align
:For text and images you can use
text-align
:您可以将其添加到您的 CSS 中并使用
或者如果您想保留
< /code> 并做好万一被删除的准备,将其添加到您的 css 中
You can add this to your css and use
<div class="center"></div>
or if you want to keep
<center></center>
and be prepared in case its ever removed, add this to your css引人深思:文本到语音合成器会用
做什么?
Food for thought: what would a text-to-speech synthesizer do with
<center>
?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.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 ^__^