XHTML Syntax
XHTML syntax is very similar to HTML syntax and all the valid HTML elements are also valid in XHTML. But XHTML is case sensitive so you have to pay a bit extra attention while writing an XHTML document to make your HTML document compliant to XHTML.
You must remember the following important points while writing a new XHTML document or converting existing HTML document into XHTML document:
- All documents must have a DOCTYPE.
- All tags must be in lower case.
- All documents must be properly formed.
- All tags must be closed.
- All attributes must be added properly.
- The name attribute has changed.
- Attributes cannot be shortened.
- All tags must be properly nested.
DOCTYPE Declaration
All XHTML documents must contain a DOCTYPE declaration at the start. There are three types of DOCTYPE declarations:
- Strict
- Transitional
- Frameset
Here is an example of using DOCTYPE.
Tags must be in lower case
XHTML is case-sensitive markup language. So, all the XHTML tags and attributes must be written in lower case.
Closing Tags are mandatory
An XHTML must have an equivalent closing tag. Even empty elements should also have closing tags. Let’s see an example:
Attribute Quotes
All the XHTML attribute’s values must be quoted. Otherwise, your XHTML document is assumed as an invalid document.
See this example:
Attribute Minimization
XHTML doesn’t allow you to minimize attributes. You have to explicitly state the attribute and its value.
See this example:
A list of minimized attributes in HTML and the way you need to write them in XHTML.
HTML Style | XHTML Style |
---|---|
compact | compact=”compact” |
checked | checked=”checked” |
declare | declare=”declare” |
readonly | readonly=”readonly” |
disabled | disabled=”disabled” |
selected | selected=”selected” |
defer | defer=”defer” |
ismap | ismap=”ismap” |
nohref | nohref=”../nohref” |
noshade | noshade=”noshade” |
nowrap | nowrap=”nowrap” |
multiple | multiple=”multiple” |
noresize | noresize=”noresize” |
The id Attribute
The id attribute is used to replace the name attribute. Instead of using name = “name”, XHTML prefers to use id = “id”.
See this example:
The language attribute
In XHTML, the language attribute of script tag is deprecated so you have to use type attribute instead of this.
See this example:
Nested Tags
XHTML tags must be nested properly. Otherwise your document is assumed as an incorrect XHTML document.
See this example:
Element Prohibitions
The following elements are not allowed to have any other element inside them. This is applicable for all the descending elements.
Element | Prohibition |
---|---|
<a> | It must not contain other <a> elements. |
<pre> | It must not contain the <img>, <object>, <big>, <small>, <sub>, or <sup> elements. |
<button> | It must not contain the <input>, <select>, <textarea>, <label>, <button>, <form>, <fieldset>, <iframe> or <isindex> elements. |
<label> | It must not contain other <label> elements. |
<form> | It must not contain other <form> elements. |