The Differences between XHTML and HTML

1 Conversation

Introduction


HTML stands for Hypertext Mark-up Language and XHTML stands for Extensible Hypertext Mark-up Language. Many people will be familiar with HTML, but they may not know about XHTML. They are both fairly similar; they both use tags and most, if not all, of the tags in HTML are availiable in XHTML. XHTML is based on XML1 and as such is much stricter with regards to coding.

The differences


The main differences between HTML and XHTML are shown below.

While there are a few more differences than detailed below, the ones shown are the main ones that are likely to affect most people.

All the differences can be found in the W3C's XHTML specification in the Differences with HTML 4 section

Closing and nesting


All tags must be correctly nested2.


Correct:
<p>This text is <b>bold</b></p>

Incorrect:
<p>This text is <b>bold</p></b>

All tags must be closed. This includes tags that go around text3 and tags that define objects rather than format text4

Empty elements must be closed either with a closing tag or the start tag must end with /> rather than just >

Surrounding tags:


Correct:
<p>This is some text</p>

Incorrect:
<p>This is some text

Empty elements:


Correct:
<br></br> or <br/>

Incorrect:
<br>


So that XHTML documents are backwards compatible with HTML browsers, a space can be placed before the />, e.g. <br />

Tags and attributes


All tags and attribute names must be lower case. This is necessary for all HTML tags used in XHTML because XML is case sensitive5


Correct:
<a href="http://example.com"></a>

Incorrect:
<A HREF="http://example.com"></A>


All attribute values in tags must be quoted, even numbers


Correct:
<table border="200">

Incorrect
<table border=200>


Attribute minimization is not supported by XML. The attribute and value6 must be written in full. So attribute names such as checked for form elements cannot occur on their own in tags


Correct:
<input checked="checked">

Incorrect:
<input checked>7


In HTML, the name attribute was defined for certain tags8. The id atribute was also present. XML only uses the id attribute, therefore XHTML documents must use the id attribute to identify elements.

So that XHTML documents are backwards compatible with HTML browsers, both id and name attributes can be used.

1Extensible Markup Language2This means that tags must not 'overlap', i.e. if a tag was started within another tag it must also be ended within that tag.3Such as <p>, <b>, and <a>4'Empty elements', such as <img> and <hr> which only have attributes and don't surround any text.5e.g. <b> and <B> are classed as different tags in XML6i.e. attribute=value7It should be noted that the <input> tag requires more attributes than shown, but they are not relevant to the example.8<a>, <applet>, <form>, <frame>, <iframe>, <img>, and <map>.

Bookmark on your Personal Space


Entry

A981623

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


Written and Edited by

References

h2g2 Entries

External Links

Not Panicking Ltd is not responsible for the content of external internet sites

Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more