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

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

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


当前回答

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

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

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

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

其他回答

最不受欢迎的答案

已弃用的标记不一定是坏标记; 有处理表示逻辑的标签,center是其中之一; 中心标签与text-align:center的div不同; 事实上,你有另一种方法来做某事并不意味着它无效。

让我解释一下,因为这里有臭名昭著的反对者,他们会认为我在捍卫老式的HTML4或其他东西。不,我不是。但围绕中心的争论只是一场趋势之战,没有真正的理由抛弃一个有效的标签。

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

"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; diana:

不使用<center>的唯一原因是人们会讨厌你。

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

让标签控制外观的一个问题是,您的页面不能很好地使用针对残疾人的设备,例如屏幕阅读器。这也会导致在你的文本中有很多很多的标签,这些标签不能帮助阐明意思,而是用不同层次的信息使它变得混乱。

因此,CSS被认为是将格式/显示转移到不同的语言,这与文本是分开的,可以很容易地保持这种方式。其中,这允许切换样式表来更改Web页面的外观,而不涉及其他标记。而且能够做到在一个膨胀的foop很多页。

CSS提供的工具并不总是很优雅,我站在你这边。例如,没有办法进行有效的垂直定心。水平居中,如果它不仅仅是文本符合文本对齐,也不会好到哪里去。

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

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

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

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

发人深思:文本-语音合成器使用<center>会做什么?