Html 为什么 <nobr> 已被弃用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28532076/
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
Why is the <nobr> deprecated?
提问by vbezhenar
I understand that this tag could easily be replaced with <span class="nowrap">
and a little bit of CSS, but in real life markup I often find that <nobr>
tag is more appropriate. It's not about style, it's about content. <nobr>V. V. Putin</nobr>
for example (in russian typography last name and first name shouldn't be line breaked, I think similar rules apply to other languages).
我知道这个标签可以很容易地用<span class="nowrap">
一点 CSS替换,但在现实生活中的标记中,我经常发现这个<nobr>
标签更合适。这不是关于风格,而是关于内容。<nobr>V. V. Putin</nobr>
例如(在俄语版式中,姓氏和名字不应换行,我认为类似的规则适用于其他语言)。
I guess that deprecation means there's something better but I don't see how styling is better than separate tag. Of course there are places where CSS should be used. Do I miss something?
我想弃用意味着有更好的东西,但我看不出样式比单独的标签更好。当然有些地方应该使用CSS。我错过了什么吗?
回答by Quentin
It isn't deprecated because it was never standard in the first place.
它没有被弃用,因为它从一开始就不是标准的。
HTML is (in theory) a semantic markup language. It describes the structure and semantics of a document along with relationships to other resources.
HTML(理论上)是一种语义标记语言。它描述了文档的结构和语义以及与其他资源的关系。
HTML is not supposed to describe presentation. A bunch of presentational features were added during the browser wars. Some of these became standardised. Most of them were subsequently deprecated when CSS came along.
HTML 不应该描述演示文稿。在浏览器大战期间添加了一系列展示功能。其中一些变得标准化。当 CSS 出现时,它们中的大多数随后被弃用。
CSS is a language for describing presentation. When you have a chunk of text that shouldn't have a line break in it, that is usually a matter of presentation so CSS is the right place to do it.
CSS 是一种描述表示的语言。当你有一大块文本不应该有换行符时,这通常是一个演示问题,所以 CSS 是正确的地方。
The exceptions are usually handled by non-breaking spaces (
).
异常通常由不间断空格 (
) 处理。
回答by Adam Katz
Feel free to use the obsolete/non-standard <nobr>
HTML tag — so long as you define it in CSS.
随意使用过时的/non-standard <nobr>
HTML 标签——只要你在 CSS 中定义它。
It's virtually guaranteed that this element name will never be repurposed for any task other than its original behavior, and this name is descriptive of the action you'll define in CSS, making it intuitive for people reviewing your HTML.
几乎可以保证该元素名称永远不会被重新用于其原始行为以外的任何任务,并且该名称描述了您将在 CSS 中定义的操作,从而使人们可以直观地查看您的 HTML。
Custom elements(including obsolete elements) will make your code fail to validateas HTML.
Your CSS will need to look like this:
您的 CSS 需要如下所示:
nobr { white-space: nowrap; hyphens: none; }
You may or may not want (or need) the hyphens: none;
part, but I have added it for completeness since it appears some implementations of <nobr>
(used to?) also suppress hyphenation while white-space: nowrap;
does not do that.
您可能需要也可能不需要(或不需要)该hyphens: none;
部分,但我为了完整性已经添加了它,因为它的一些实现<nobr>
(用于?)也抑制了连字符,white-space: nowrap;
而不会这样做。
This defines the white-space
as nowrap
, which suppresses line breaks within white space and the hyphens
as none
, which prevents breaking within words (includingignoring characters within words that "suggest line break points").
这定义了white-space
as nowrap
,它抑制了空格内的换行符和hyphens
as none
,它防止了单词中的换行(包括忽略单词中“建议换行点”的字符)。
回答by Dave Burton
Foolishness. That's the only reason.
愚蠢。这是唯一的原因。
<nobr>
is universally supported, because it is needed, to convey the fact that the enclosed content is semanticallya single unit, so it should not be split across multiple lines.
<nobr>
被普遍支持,因为它需要传达这样一个事实,即封闭的内容在语义上是一个单一的单元,所以它不应该分成多行。
回答by Patrick Hofman
I found this interesting and very explaining comment on a w3c mailing list:
我在w3c 邮件列表上发现了这个有趣且非常解释性的评论:
<NOBR> cannot be deprecated as it has never been part of even transitional W3C HTML versions; it is purely proprietory.
<NOBR> 不能被弃用,因为它从未成为 W3C HTML 过渡版本的一部分;它完全是专有的。
Yep. So it never was and never will be part of the spec. Just something 'missing' added using CSS.
是的。所以它从来没有也永远不会成为规范的一部分。只是使用 CSS 添加了一些“缺失”的东西。
回答by vortex
The proper way to achieve what you want is to declare that "V. V. Putin" is a proper noun, and define in CSS that such a proper noun should not be line breaked.
实现您想要的正确方法是声明“VV Putin”是专有名词,并在CSS中定义这样的专有名词不应换行。
<span class="propernoun">V. V. Putin</span>
and in CSS you would define
在 CSS 中你会定义
.propernoun {
white-space: nowrap;
}
Since... HTML is not about style, it is about content. Basically, this is the same as what the OP suggested, but the nowrap in class="nowrap" describes a presentational property in html which should be in the stylesheet.
因为... HTML 与样式无关,它与内容有关。基本上,这与 OP 建议的内容相同,但是 class="nowrap" 中的 nowrap 描述了 html 中应该在样式表中的表示属性。
回答by gvlasov
That's because a proper tag should be semanticallyuseful, when <nobr>
doesn't have any semantics apart from its style. I guess that's the same reason why <center>
and similar styling tags were deprecated.
那是因为一个合适的标签在语义上应该是有用的,当<nobr>
除了它的样式之外没有任何语义时。我想这<center>
与类似的样式标签被弃用的原因相同。