XHTML Syntax

tutorial and example
tutorial and example

XHTML Syntax is similar to the HTML and all the elements that are valid in HTML also valid in XHTML. XHTML is stricter than HTML in syntax and case sensitivity.

Following are the important points that we need to remember when we are writing a new XHTML document:

  • XHTML documents must have a DOCTYPE
  • XHTML tags must be in lower case
  • XHTML tags must be closed
  • XHTML attributes must be added properly
  • XHTML documents must be properly formed
  • Attributes cannot be shortened
  • Tags must be properly nested

DOCTYPE Declaration

XHTML documents must have a DOCTYPE declaration and should be first statement of the XHTML document.

There are three types of DOCTYPE declaration in XHTML:

  • Strict
  • Transitional
  • Frameset

DOCTYPE declaration

Case Sensitivity

XHTML is case sensitive markup language, so we need to write all the XHTML tags and attributes in lower case only.

Example

Tags must be closed

In XHTML, each and every tag should have an equivalent closing tag even empty elements should also have closing tags.

Example 1

Example 2

Attribute Quotes

In XHTML, attributes values must be quoted (“or”). Otherwise, your XHTML document is assumed as an invalid document.

Example

Attribute Minimization

In XHTML, attribute minimization is invalid. It means you need to explicitly state the attribute and its value.

Example

HTML vs XHTML Attributes

XHTML uses its own way to write attributes, the following table contains the uses of attributes in two different documents.

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”

XHTML id start

In XHTML, id attribute is used to replace the name attribute. Instead of using name = “name”, XHTML prefers to use id = “id”.

Example

XHTML language Attribute

The language attribute of the script tag is deprecated but can be used.

Example

XHTML Nested Tags

In XHTML, tags must be nested properly. Otherwise our document is assumed as an incorrect XHTML document.

Example

XHTML Element Prohibitions

In XHTML Element Prohibition, we cannot allow other elements inside these 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.