why is <br /> different from <br></br> in XHTML?

馋奶兔 提交于 2019-12-05 13:23:07

Because the XHTML spec HTML Compatability Guidelines specify that br must should be self closing. Apparently Chrome and IE8 are not follwing the spec and closing the open one for you, thus creating a second line break.

Some good answers already, but just to point out that HTML5 actually specifies that <br></br> should be parsed as two <br> start tags when parsing as text/html.

See An end tag whose tag name is "br" in http://dev.w3.org/html5/spec/tokenization.html#parsing-main-inbody

Firefox 3.x only does this in quirks mode, but Firefox 4 does this in Standards mode as well.

Although valid, this is highly unusual code. What is much more likely is developers mistakenly using <br> or </br> when they mean <br/>. For this reason most browsers will interpret both as <br/>.

The extra line between three and four is because there are two <br /> tags between them. The first one moves content onto the next line and the second one moves it one more line down. This is expected behaviour.

Edit Sorry, thought it was strict for the following.
Also, <br /> tags are empty tags and so, must be closed. Because of this, I don't think that <br> is technically xhtml compliant. It should be <br />.

I.E appears to interpret
and both as "link break" and adds a like break for each.

In XHTML — they are the same — and if you serve the document as application/xhtml+xml there will be no difference in browsers (assuming the browser supports XHTML, which IE 8 and lower do not).

If you serve the document as text/html, it will be treated as HTML, not XHTML, and in HTML <br> is an element where the end tag is forbidden. If you include an explicit end tag then some browsers will error correct to assume that </br> is a <br> start tag.

There are various additional rules that must be followed if you are claiming your XHTML is text/html. These are described in the compatibility guidelines. This particular one is for elements that can never have content.

Serving as text/html was a hack intended to be a short term measure during the transition to XHTML proper. Various things (including lack of support from Microsoft) prevented this transition from ever finishing, and the HTML 5 movement has given up on the idea and moved back to optional and forbidden end tags (but adding in /> as syntactic sugar).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!