XHTML Tutorial
This XHTML tutorial demonstrates how to code web pages using XHTML. It assumes that you're already comfortable with HTML. XHTML is almost identical to HTML with only a handful of differences. XHTML is slightly more strict than HTML and requires a little more attention when coding.
XHTML was developed by the W3C to help web developers make the transition from HTML to XML. So if you want to learn XML, XHTML is a good place to start.
Here are the main things to remember when writing XHTML:
- The root element of the document must be
html
- The root element must contain a
xmlns
declaration for the XHTML namespace - Must have a DOCTYPE declaration, and it must come before the
html
element - All XHTML tags are lower case
- Close all tags (even empty elements)
- Attribute values must be quoted
- Minimization is forbidden
- The
id
attribute replaces thename
attribute - The
language
attribute of thescript
tag is deprecated - Proper nesting of tags
Here's a more detailed explanation of the above XHTML rules.
The Root Element
The root element of the document must be html
. Read more about the <html>
element.
The xmlns
Declaration
All XHTML documents must have an xmlns
declaration for the XHTML namespace (i.e. http://www.w3.org/1999/xhtml
). Read more about the HTML xmlns
declaration.
Example:
DOCTYPE
All XHTML documents must have a DOCTYPE declaration. The document must include the usual html, head, title, and body elements.
Example:
Case
All XHTML tags should be lower case. (HTML tags on the other hand, can be upper or lower case).
Wrong
Right
Closing tags
All tags should have an opening tag and a closing tag. If it's a tag that doesn't have a closing tag (for example, the img
tag, or the hr
tag, use a space and forward slash (/) before the > sign. (Note that the XHTML specification does not require a space before the "/" but this is required to conform with some browsers).
Wrong
Right
Quotes
In XHTML, all attribute values must be quoted. (In HTML on the other hand, you could get away without quoting attribute values).
Wrong
Right
Minimization
Minimization is forbidden. You should explicitly state the attribute and the value.
Wrong
Right
The id attribute
The id attribute replaces the name attribute. Instead of using name=
,use id=
.
Wrong
Right
Script tag
The language
attribute of the script
tag is deprecated.
Wrong
Right
Nesting
All tags must be nested properly.
Wrong
Right
Apart from these strict rules, you can code your XHTML pages just like you've been coding HTML pages. To learn more about coding HTML, check out the HTML tutorial.