tags:

views:

706

answers:

8

whats the main differences between xhtml and html? which one is better in your opinion and why? is browsers compatible with both of them?

+7  A: 

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

Guido
Really, there is no reason not to generate valid xhtml...unless you want to make it more difficult to embed your work/screen scrape it.You probably don't want to do that, though.
Marcin
"XHTML is the modern version of HTML": I must go against that, this is plain wrong. XHTML is HTML implemented with XML, instead of SGML. This is not modern, this is different. For example, your document needs to be well-formed according or your browser will just display an XML error.
Vincent Robert
You jump to conclusion too quickly. IE doesn't support XHTML *at all*, most server-side tools/templates aren't strict enough to reliably output XML, and there's very little that XHTML can do what HTML can't, the way to go isn't that clear.
porneL
Exactly, porneL. And you get into all sorts of ambiguities with regards to what Content Type to serve your pages as. Are they text/xml? Or are they text/html, but formatted like XML? Then all of a sudden one of the major browsers go into Quirks Mode and your layout goes crazy. Nice mess.
conny
A: 

An example of a difference is the break tag.

<br>

is proper HTML, but in XHTML where the elements need to validate as XML, you must self-close the tag:

<br />
davebug
is "self-close the tag" prevent problems?
alsadk
+7  A: 

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.

Andy
XHTML is in (relatively) widespread use today! Serving XHTML as application/xhtml+xml is not common because Internet Explorer 6 screws up with that MIME Type. I think it's already working in Internet Explorer 7.
Christoph Schiessl
What I meant was serving it as 'application/xhtml+xml' is not in widespread use.
Andy
A: 

it's not about which one is better, the HTML still continues to develop, and HTML5 is on its way (for years, though :) ) an 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.

zappan
so is browsers compatible with both of them
alsadk
not completely, and not with all versions. xhtml 1.0 is well supported, xhtml 1.1 not so much. html 4 and 4.01 is well supported, but should be used in 'strict' mode to assure almost similar rendering accross browsers, html 5 support is coming to the scene (so older browsers may have issues)
zappan
If you want to serve document as text/html, you *can't* use XML serializer! Syntax of CDATA blocks is incompatible, and XML serializer is allowed to output <br></br> or <xhtml:p>, which break in HTML mode. That's a big problem, because you have to use unreliable markup-is-text tools.
porneL
Internet explorer is the deal breaker. You can't serve XHTML as XML and so it doesn't offer many advantages
Casebash
+6  A: 

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.

Charles Roper
+2  A: 

Summary: use HTML.

There are many differences, and many smart people (that know what they are talking about :D) have already summed up most of the pros and cons.

cic
+2  A: 

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.

Halo
Actually, sending XHTML as application/xhtml+xml will trigger the error. Sending XHTML as text/html will in practice work because your XML document will be parsed by an SGML parser that is actually very forgiving.
Vincent Robert
+2  A: 

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.

Vincent Robert