Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are XHTML tags valid in HTML?

I understand a line break in HTML is <br> and the XHTML equivalent is <br />.

I know that you cannot use the above HTML tag in a XHTML document, but what about vice versa? Is <br /> valid in HTML?

like image 350
Michael Avatar asked Aug 09 '11 14:08

Michael


2 Answers

It depends which version of HTML you are talking about.

In the latest stable version, HTML 4.01, the syntax means the same as <br>> which means the same as <br>&gt;. This means it is valid, since you can have a > anywhere you can have a line break, (although <meta /> usually isn't valid) but doesn't mean what you want. Limitations in browsers (exploited by the XHTML 1.0 spec) mean that it gets treated as <br> even though it shouldn't be. The mess around this feature means it is marked as to be avoided.

HTML 5 changes that and turns the / into syntactic sugar. Utterly meaningless but allowed so the XML junkies are kept happy.

like image 105
Quentin Avatar answered Sep 19 '22 10:09

Quentin


<br /> is backwards compatible and will not cause rendering problems if used in an HTML document.

Also, note that unless you are serving your documents with the MIME type application/xhtml+xml, putting a <br> in an XHTML document won't cause it to choke. It is a validation error to leave a BR unclosed in an XHTML document, but a very minor one which is unlikely to cause any problems by itself.

EDIT: Oh yes, and in HTML5 the closing / is once again officially optional.

like image 35
Will Martin Avatar answered Sep 22 '22 10:09

Will Martin