tags:

views:

105

answers:

4
+3  Q: 

Use html or xhtml?

Are most of the websites using HTML or XHTML?

Isn't XHTML "better" than HTML?

I know the declarations are different.

XHTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>TODO supply a title</title>
    </head>
    <body>
        <p>
            TODO write content
        </p>
    </body>
</html>

HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    TODO write content
  </body>
</html>

Questions:

  • What are the three declaration lines for in the XHTML file?
  • Isn't the doctype declaration line for html only? Why does XHTML use one also?
  • Should I use XHTML or HTML and why?
+5  A: 

This is a complex question. I would say there is no concrete reason why you should use one over the other. The best thing is to take as much information about them as possible, form your own understanding and opinions and make your decision based on that.

Doctype Declarations

There is a fair bit of confusion surrounding this. I will attempt to summarise.

  • HTML (4 and 5) should always have the appropriate doctype.
  • XHTML ought to have a doctype, although it isn't strictly required if you are serving your pages with an XHTML content type (application/xhtml+xml) - be aware IE does not support this though!
  • XHTML served with an HTML content type (text/html) does need the doctype to be present
  • This list of doctypes may be of help

HTML 4, HTML 5, XHTML 1, XHTML 1.1...?

HTML 4 is the latest version of the HTML standard. XHTML 1 is a reformulation of this standard in XML, and became the more preferred one. XHTML 1.1 is a slightly updated standard which is best avoided due to the requirement for serving with the XHTML content type (as previously mentioned, well-used browsers will choke on this and your site will be useless). HTML 5 is not yet a standard but it is definitely heading that way. In addition, it is designed to be handled in a sensible way by older browsers not yet supporting it. It should just work, albeit in a diminished form if you use many of the new features.

If it were my decision, I'd seriously consider using HTML 5. It is the latest standard and nobody says you have to use all the new bits. But you are saving yourself work in the future if you decide to move towards using them then - you won't have to convert everything you have (doctypes etc). It's also quite permissive (by design).

If you decide against HTML5, there is nothing wrong with using either HTML 4.01 or XHTML 1. They are essentially slight variations on the same standard, and both are well established in terms of browser support.

If you really feel hardcore, check out the actual specs. Tough reading, but there's a lot of information there.

Declarations in the XHTML (added to answer the new question posed in the question)

<?xml version="1.0" encoding="UTF-8"?>

This is the XML declaration. It states that this is an XML file, encoded using UTF-8. This ought to be present when serving XHTML files with the application/xhtml+xml content-type. If you are serving it as HTML (text/html), it's probably best to avoid this. Certainly some versions of IE (can't remember which) will revert to quirks mode upon seeing this instead of a doctype at the beginning

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

This is the doctype. See above.

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

This is the opening tag for the html element (the root element of the document). The xmlns attribute is specifying the namespace. This attribute (with this value) is required in an XHTML document, as noted in the specification.

Splash
+1  A: 

Html 5 will stay up to date for longer as it is the latest development, it's also one of the simplest to use.

<!DOCTYPE html>

is the doctype, and this javascript is usually required for internet explorer

  <!--[if lte IE 8]>
  <script type="text/javascript" src="http://html5shiv.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt;
  <![endif]-->

XHTML is the most common at the moment though - and probably the best supported by tutorials

Edd Turtle
A: 

One thing to take into consideration: General portability of your markup!

Example: If you one day decide to offer PDF download for some of your content, you will have much less trouble, if your site uses XHTML. PrinceXML for example, THE number one PDF conversion engine, needs the input to be correct XHTML in order to create PDF documents.

tharkun
+1  A: 

There are already good replies for the other questions, but for this one...

Should I use XHTML or HTML and why?

Simple: IE (as of v.8) still does not support XHTML (if you send it using the correct MIME type). So, you are stuck to HTML 4 if you want to stay compatible with most browsers.

IMHO, this very old document is still valid : Sending XHTML as text/html Considered Harmful

And yet, XHTML is much better than HTML to create structured documents separating content from presentation. I think the best solution is to use an XML language adapted to your context (which is even better than XHTML for these issues) and transform it to HTML with XSLT.

Damien