开发者

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

Here's the complete source of an HTML page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head></head>
<body>
one<br>
two<br />
three<br></br>
four    
</body>
</html>
开发者_StackOverflow

Can anyone explain why an extra blank line appears between the "three" and the "four" when I view the page in IE8 or chrome?

I thought standards were supposed to make all browsers look the same and as far as I can see this page conforms to the XHTML transitional standard


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜