What are the main differences between XHTML and HTML? Which one is better in your opinion, and why? Do most browsers support both?
This is probably the best article I’ve read on the differences and relative merits of each:
Which should we use, HTML or XHTML, and why?
There is also a rather technical comparison on the WHATWG wiki.
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.
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
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.
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”.
Summary: use HTML.
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.
It’s not about which one is better; the HTML still continues to develop, and HTML5 is on its way (for years, though 🙂 ) and delivers some new elements for easier management of new technologies like multimedia in web pages.
On the other hand, XHTML is about strictness of XML, should we say keeping things clean. If you keep your HTML document well-formed (close every opened element, keep things nested in the tree-form), you’re getting the best out of XHTML/XML world and can still be using the HTML format, declaring your documents to be HTML, but still keeping them ‘clean’ (well-formed). I don’t think we should declare each of them ‘better’; they can co-exist. It’s just about us doing our things the right way.
An example of a difference is the break tag.
is proper HTML, but in XHTML where the elements need to validate as XML, you must self-close the tag: