What are the main differences between XHTML and HTML? [closed]

What are the main differences between XHTML and HTML? Which one is better in your opinion, and why? Do most browsers support both?


Solution 1:

XHTML is based on XML, and thus requires the source to be well-formed. Since XHTML is more strict than HTML, less pre-processing is needed by the rendering engine.

XHTML should be served as application/xhtml+xml for you to take advantage of the benefits, otherwise XHTML will be treated as ordinary HTML. Serving it as 'application/xhtml+xml' is not common on the web due to Internet Explorer, which cannot handle XHTML.

Solution 2:

This is probably the best article I've read on the differences and relative merits of each:

HTML Versus XHTML

Which should we use, HTML or XHTML, and why?

There is also a rather technical comparison on the WHATWG wiki.

Solution 3:

The difference is that XHTML is based on XML while HTML is based on SGML.

Browsers use the SGML parser for content sent with the content type text/html and the XML parser for application/xhtml+xml.

When using the SGML parser, browsers will continue the parsing even when they encounter a syntax error in the file. This is why so many people think they are doing XHTML when they are sending XML-looking files to the browser. This is actually a mistake since this will trigger many parsing errors in the browser and slow the rendering process.

When using the XML parser, browser will stop the parsing when a syntax error is encountered and display an XML error. This is of course only true for browsers that have an XML parser for HTML content, which is not true for Internet Explorer that will only download the file and not display it.

What should you do then?

You should use XHTML if you need the syntax checking and the strict structure that XML impose. You should keep in mind that you must be sending your XHTML content to Internet Explorer with the wrong content type with all the issues implied. You should also keep in mind that your document will break whenever an unvalid content is in the file, so you must take great care to sanitize any user input.

You should use HTML for anything else. HTML just works and is a greatly supported standard today. Even the next standard HTML 5 defines an SGML-based syntax so this will last.

Keep in mind that whatever the format you choose, validating your output is always a good idea to detect syntax error.

Solution 4:

The Extensible Hypertext Markup Language, or XHTML, is a markup language that has the same depth of expression as HTML, but also conforms to XML syntax.

XHTML is "the modern version of HTML 4".

More info : wikipedia and W3C

Solution 5:

As previously stated, XHTML should be (in theory) valid XML. In theory (but not in practice) non-conforming XHTML should not be rendered by the browser.

There is very rarely an advantage to use one over the other, so write whichever you personally prefer - as long as you do it consistently. HTML5 which is going to be the future web-standard will support both XHTML and HTML 4.01-style tags (the prior via optional "XML serialization"), and XHTML2 is looking like it will be pretty much dead in the water with no vendors actively supporting it.