HTML 5:是 <br>、<br/> 还是 <br />?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1946426/
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
HTML 5: Is it <br>, <br/>, or <br />?
提问by Eikern
I've tried checking other answers, but I'm still confused — especially after seeing W3schools HTML 5 reference.
我试过检查其他答案,但我仍然感到困惑——尤其是在看到W3schools HTML 5 参考之后。
I thought HTML 4.01 was supposed to "allow" single-tags to just be <img>
and <br>
. Then XHTML came along with <img />
and <br />
(where someone said that the space is there for older browsers).
我认为 HTML 4.01 应该“允许”单标签为<img>
和<br>
。然后 XHTML 出现了<img />
和<br />
(有人说旧浏览器有空间)。
Now I'm wondering how I'm supposed to format my code when practicing HTML 5.
现在我想知道在练习 HTML 5 时我应该如何格式化我的代码。
Is it <br>
, <br/>
or <br />
?
是<br>
,<br/>
还是<br />
?
采纳答案by Brian Campbell
Simply <br>
is sufficient.
简单<br>
就足够了。
The other forms are there for compatibility with XHTML; to make it possible to write the same code as XHTML, and have it also work as HTML. Some systems that generate HTML may be based on XML generators, and thus do not have the ability to output just a bare <br>
tag; if you're using such a system, it's fine to use <br/>
, it's just not necessary if you don't need to do it.
其他形式是为了与 XHTML 兼容;使编写与 XHTML 相同的代码成为可能,并使其也作为 HTML 工作。某些生成 HTML 的系统可能基于 XML 生成器,因此无法仅输出裸<br>
标签;如果你使用这样的系统,使用<br/>
它是可以的,如果你不需要它就没有必要。
Very few people actually use XHTML, however. You need to serve your content as application/xhtml+xml
for it to be interpreted as XHTML, and that will not work in old versions of IE - it will also mean that any small error you make will prevent your page from being displayed in browsers that do support XHTML. So, most of what looks like XHTML on the web is actually being served, and interpreted, as HTML. See Serving XHTML as text/html Considered Harmfulfor some more information.
然而,很少有人真正使用 XHTML。你需要提供你的内容,application/xhtml+xml
因为它被解释为 XHTML,这在旧版本的 IE 中不起作用 - 这也意味着你犯的任何小错误都会阻止你的页面在支持 XHTML 的浏览器中显示。因此,Web 上大部分看起来像 XHTML 的内容实际上是作为 HTML 提供和解释的。有关更多信息,请参阅将XHTML 视为有害的 text/html。
回答by Daan
I think this quote from the HTML 5 Reference Draftprovides the answer:
我认为HTML 5 参考草案中的这句话提供了答案:
3.2.2.2 Void Elements
The term void elementsis used to designate elements that must be empty. These requirements only apply to the HTML syntax. In XHTML, all such elements are treated as normal elements, but must be marked up as empty elements.
These elements are forbidden from containing any content at all. In HTML, these elements have a start tagonly. The self-closing tagsyntax may be used. The end tagmust be omitted because the element is automatically closed by the parser.
HTML Example:
A void element in the HTML syntax. This is not permitted in the XHTML syntax.<hr>
Example:
A void element using the HTML- and XHTML-compatible self-closing tag syntax.<hr/>
XHTML Example:
A void element using the XHTML-only syntax with an explicit end tag. This is not permitted for void elements in the HTML syntax.<hr></hr>
3.2.2.2 虚空元素
术语void 元素用于指定必须为空的元素。这些要求仅适用于 HTML 语法。在 XHTML 中,所有此类元素都被视为普通元素,但必须标记为空元素。
这些元素完全禁止包含任何内容。在 HTML 中,这些元素只有一个开始标签。在自闭的标签语法也可以使用。所述结束标记必须被省略,因为该元素被自动由解析器关闭。
HTML 示例:
HTML 语法中的空元素。这在 XHTML 语法中是不允许的。<hr>
示例:
使用 HTML 和 XHTML 兼容的自闭合标记语法的 void 元素。<hr/>
XHTML 示例:
使用 XHTML-only 语法和显式结束标记的 void 元素。这对于 HTML 语法中的 void 元素是不允许的。<hr></hr>
回答by Michael Krelin - hacker
XML doesn't allow leaving tags open, so it makes <br>
a bit worse than the other two. The other two are roughly equivalent with the second (<br/>
) preferred for compatibility with older browsers. Actually, space before /
is preferred for compatibility sake, but I think it only makes sense for tags that have attributes. So I'd say either <br/>
or <br />
, whichever pleases your aesthetics.
XML 不允许让标签保持打开状态,所以它<br>
比其他两个更糟糕。其他两个大致相当于第二个 ( <br/>
) 首选,以便与旧浏览器兼容。实际上,/
出于兼容性考虑,优先使用之前的空格,但我认为它只对具有属性的标签有意义。所以我说无论是<br/>
或<br />
,两者取悦你的审美观。
To sum it up: all three are valid with the first one (<br>
) being a bit less "portable".
总结一下:这三个都是有效的,第一个 ( <br>
) 不太“便携”。
Edit: Now that we're all crazy about specs, I think it worth pointing out that according to dev.w3.org:
编辑:既然我们都对规格很着迷,我认为值得指出的是,根据dev.w3.org:
Start tags consist of the following parts, in exactly the following order:
- A "<" character.
- The element's tag name.
- Optionally, one or more attributes, each of which must be preceded by one or more space characters.
- Optionally, one or more space characters.
- Optionally, a "/" character, which may be present only if the element is a void element.
- A ">" character.
开始标签由以下部分组成,完全按照以下顺序:
- 一个“<”字符。
- 元素的标签名称。
- 可选,一个或多个属性,每个属性前面必须有一个或多个空格字符。
- 可选,一个或多个空格字符。
- 可选地,一个“/”字符,只有当元素是空元素时才可能出现。
- 一个“>”字符。
回答by HymanDev
In HTML (up to HTML 4): use <br>
在HTML(最高 HTML 4)中:使用<br>
In HTML 5: <br>
is preferred, but <br/>
and <br />
is also acceptable
在HTML 5:<br>
是优选的,但<br/>
并<br />
也是可以接受
In XHTML: <br />
is preferred. Can also use <br/>
or <br></br>
在XHTML 中:<br />
是首选。也可以使用<br/>
或<br></br>
Notes:
笔记:
<br></br>
is not valid in HTML 5, it will be thought of as two line breaks.- XHTML is case sensitive, HTML is not case sensitive.
- For backward compatibility, some old browsers would parse XHTML as HTML and fail on
<br/>
but not<br />
<br></br>
在 HTML 5 中无效,它将被视为两个换行符。- XHTML 区分大小写,HTML 不区分大小写。
- 为了向后兼容,一些旧的浏览器会将 XHTML 解析为 HTML 并失败,
<br/>
但不会<br />
Reference:
参考:
回答by tvanfosson
回答by Kmeixner
I would recommend using <br />
for the following reasons:
我建议使用<br />
,原因如下:
1) Text and XML editors that highlight XML syntax in different colours will highlight properly with <br />
but this is not always the case if you use <br>
1) 以不同颜色突出显示 XML 语法的文本和 XML 编辑器将正确突出显示,<br />
但如果您使用,情况并非总是如此<br>
2) <br />
is backwards-compatible with XHTML and well-formed HTML (ie: XHTML) is often easier to validate for errors and debug
2)<br />
与 XHTML 向后兼容,并且格式良好的 HTML(即:XHTML)通常更容易验证错误和调试
3) Some old parsers and some coding specs require the space before the closing slash (ie: <br />
instead of <br/>
) such as the WordPress Plugin Coding spec: http://make.wordpress.org/core/handbook/coding-standards/html/
3) 一些旧的解析器和一些编码规范需要在结束斜杠前有空格(即:<br />
而不是<br/>
),例如 WordPress 插件编码规范:http: //make.wordpress.org/core/handbook/coding-standards/html/
I my experience, I have never come across a case where using <br />
is problematic, however, there are many cases where <br/>
or especially <br>
might be problematic in older browsers and tools.
根据我的经验,我从来没有遇到过使用<br />
有问题的情况,但是,在很多情况下,<br/>
或者特别<br>
是在旧浏览器和工具中可能会出现问题。
回答by sastanin
XML requires all tags to have a corresponding closing tag. So there is a special short-hand syntax for tags without inner contents.
XML 要求所有标签都有一个相应的结束标签。所以对于没有内部内容的标签有一种特殊的速记语法。
HTML5 is not XML, so it should not pose such a requirement. Neither is HTML 4.01.
HTML5 不是 XML,因此不应提出这样的要求。HTML 4.01 也不是。
For instance, in HTML5 specs, all examples with br
tag use <br>
syntax, not <br/>
.
例如,在HTML5 规范中,所有带有br
标记的示例都使用<br>
语法,而不是<br/>
.
UPDActually, <br/>
is permitted in HTML5. 9.1.2.1, 7.
回答by MindStalker
If you're interested in comparability (not compatibility, but comparability) then I'd stick with <br />
.
如果您对可比性(不是兼容性,而是可比性)感兴趣,那么我会坚持使用<br />
.
Otherwise, <br>
is fine.
否则,<br>
还好。
回答by Dan Bray
Both <br>
and <br/>
will do fine but I prefer <br/>
because it's slightly more logical. It is logical to expect a closing tag whenever there is an opening tag. Therefore your code is slightly easier to read if you don't use an opening tag when there isn't going to be a closing tag.
双方<br>
并<br/>
会做得很好,但我更喜欢<br/>
,因为它是稍微更符合逻辑。只要有开始标签,就期待结束标签是合乎逻辑的。因此,如果在没有结束标记的情况下不使用开始标记,您的代码会更容易阅读。
All browser (except possibly some very old ones that don't matter) will display both exactly the same. However, <br>
is not xHTML complient.
所有浏览器(可能除了一些无关紧要的非常旧的浏览器)都将显示完全相同的内容。但是,<br>
不兼容 xHTML。
回答by kevinji
Both <br>
and <br />
are acceptable in HTML5, but in the spirit of HTML, <br>
should be used. HTML5 allows closing slashes in order to be more compatible with documents that were previously HTML 4.01 and XHTML 1.0, allowing easier migration to HTML5. Of course, <br/>
is also acceptable, but to be compatible with some older browsers, there should be a space before the closing slash (/
).
二者<br>
并<br />
在HTML5是可接受的,但在HTML的精神,<br>
应该被使用。HTML5 允许关闭斜杠,以便与以前是 HTML 4.01 和 XHTML 1.0 的文档更兼容,从而更容易迁移到 HTML5。当然,<br/>
也是可以接受的,但是为了兼容一些旧的浏览器,在结束斜线(/
)之前应该有一个空格。