开发者

Why does self closing tag work in a html document?

I have a html document:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" >

In it I have tags such as

<br />

But Im reading that this tag is an开发者_运维百科 XHTML element. Yet it still works why?


Original answer based on the question as written before a character was moved and completely changed it:

But Im reading that </br> is an XHTML element.

It isn't. Is is the end tag for an element.

<br /> would be a self closing tag (representing an entire element) in XHTML. In HTML 4 it means the same as <br>&gt; (although most browsers don't respect that) and in HTML 5 the / is meaningless syntactic sugar to keep people used to XHTML happy.

In XHTML <br/> means the same as <br></br> (the latter is an error in HTML documents).

Yet it still works why?

Browsers perform enormous amounts of error correction to try to deal with the sort of bad markup that was prevalent in the late 90s.

They are not always consistent in how they recover from different errors (for example, I believe that some browsers will ignore that completely while others will treat it as a line break), so you should never depend on this behaviour.


Browsers failed to implement parsers that correctly handled HTML 4 and earlier.

They should have treated <br/> as "A br element followed by a greater than sign", but instead implemented it as "A br element with a / attribute, what's a / attribute? We'll drop it". This led to the feature being marked as something to avoid.

XHTML then exploited the bug for HTML-Compatible XHTML.

HTML 5 then redefined it as syntactic sugar so the XHTML junkies could keep on using the syntax they were used to.


It's the browser that get rid of these differences. Anyways the </br> with that slash is incorrect both in HTML and XHTML.

Occurring to http://www.w3schools.com/tags/tag_br.asp

 In HTML the <br> tag has no end tag.

 In XHTML the <br> tag must be properly
 closed, like this: <br />.


Self closing tag is valid format in XML

XHTML means all tag must be closed

HTML
<br> valid
<br/> valid
XHTML
<br> invalid
<br/> valid

Edited:

</br> is invalid anyway and you are lucky if browser fix it :)    
</br> is the same as <div id="gd"/>, both are invalid
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜