HTML 5: Is it
,
, or
?

2018-12-31 04:26发布

I've tried checking other answers, but I'm still confused--especially after seeing W3schools HTML 5 reference.

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

Now I'm wondering how I'm supposed to format my code when practicing HTML 5.

<!DOCTYPE HTML>

Is it <br>, <br/> or <br />?

标签: html html5
24条回答
骚的不知所云
2楼-- · 2018-12-31 04:47

<br> and <br/> render differently. Some browsers interpret <br/> as <br></br> and insert two line breaks

查看更多
伤终究还是伤i
3楼-- · 2018-12-31 04:49

In HTML (up to HTML 4): use <br>

In HTML 5: <br> is preferred, but <br/> and <br /> is also acceptable

In XHTML: <br /> is preferred. Can also use <br/> or <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 />

Reference:

查看更多
与君花间醉酒
4楼-- · 2018-12-31 04:49

In HTML5 the slash is no longer necessary: <br>, <hr>

查看更多
其实,你不懂
5楼-- · 2018-12-31 04:49

As many others have covered, both <br> and <br/> are acceptable.

I guess the tradeoff is the better readability and backward compatibility of <br/> versus sending one less character to the end users with <br>.

And since Google uses <br> so will I.

(Of course keep in mind that they might be serving me <br> because I'm using Chrome which they know supports it. In IE they might still be serving <br/>)

查看更多
爱死公子算了
6楼-- · 2018-12-31 04:49

Ummm.....does anyone know a SINGLE vendor, user-agent, or browser maker that has ever followed the W3C Specifications 100%??? So if HTML5 says it supports all three break element versions, you can bet the vendors support the same and even more sloppier versions!

The ONLY thing that matters in this debate is to CONSISTENTLY use coding that also happens to follow XML specifications as well as HTML specifications when possible. That means you should use the correct XML version of the break tag and encourage all your team to do the same:

<br />

The same space-slash format should apply for the img, a, hr, and meta tags in your code. Why? Because:

  1. Its is backwards compatible with older XHTML user-agents / browsers
  2. The browser vendors support the XML version anyway so the HTML5 specification is moot.
  3. The sloppy implementations of most user-agents today, in the past, and in the future will accept it.
  4. It allows your markup to be comparable with XML standards should you need to go back to creating XHTML/XML documents from your markup.
  5. It's "good coding practice" for ALL WEB DEVELOPERS to keep using solid markup practices that follow XML, including coding in all lower case, quoted attributes, escaped XML characters, etc. etc. Why? In the future if you have to switch to XML data you automatically code and think in XML.
  6. We can only hope that in the future World Wide Web, we move away from private vendor-implemented standards and go back to solid, reliable, verified markup that parses faster, moves data over the wires faster, and make our future Internet a more standardized medium using XML.

Besides, in the robotic and machine world that's here, where robots don't have the same Human-interface coding problems HTML5 solves for us, they will gladly go back to XML data systems and parse such UI web pages much faster when converted to XML data.

查看更多
笑指拈花
7楼-- · 2018-12-31 04:50

If you're interested in comparability (not compatibility, but comparability) then I'd stick with <br />.

Otherwise, <br> is fine.

查看更多
登录 后发表回答