HTML XHTML

XHTML is an acronym of Extensive HyperText Markup Language. It is more strict than ordinary HTML. It is defined as XML application and also supported in all major browsers.

Why we need XHTML above HTML?

Many pages contain bad HTML. Even this HTML code has break the HTML rules yet it works well in most browser. Globally, people use more number of handset than computer and tablet. Having multiple bad HTML elements within a page, smaller device like smartphone gets lack the power to interpret the bad markups.

How to convert HTML into XHTML?

  1. Add <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> to the first line of every page.
  2. Add <html xmlns="http://www.w3.org/1999/xhtml"> to the html element of every page.
  3. Close all HTML empty tags like <br />, <img />, <hr />.
  4. Convert all element names into lowercase like <body> instead of <BODY>.
  5. Quote all attribute values like <input type="text" />.

Require and minimum elements to define an XHTML document

Full code example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title> Name of the Title </title>
</head>

<body>
    Body content
</body>

</html>

Differences between HTML and XHTML

  1. In XHTML, the <!DOCTYPE ... > declaration is mandatory, and <html> (with xmlns attribute), <head>, <title>, and <body> elements are all required, while in an <html> document, only the <!DOCTYPE html> declaration is sufficient, and all the attributes of the <html> start tag are optional.

  2. In XHTML, the elements must be properly nested like
    <b> <i> Bold and italic. </i> </b>

    While in HTML, the elements can be nested improperly like
    <b> <i> Bold and italic. </b> </i>

  3. In XHTML, all elements including empty elements must be properly closed.

    In XHTML, all the following lines are false: <p> This is a paragraph.
    <p> This is also a paragraph.
    An image: <img src="demo.gif" alt="demo">
    Horizontal rule: <hr>
    A Break: <br>


    Inorder to make them valid syntax, we must them as follows:

    <p> This is a paragraph </p>
    <p> This is also a paragraph </p>
    An image: <img src="demo.gif" alt="demo" />
    Horizontal rule: <hr />
    A Break: <br />

  4. XHTML elements and attributes must be defined in lower case.

    In XHTML, the following lines are wrong:

    <P> This is a paragraph </P>
    <p ALIGN="center"> This is also a paragraph </p>

    True Example in XHTML:-
    <p> This is a paragraph </p>
    <p align="center"> This is also a paragraph </p>

  5. In XHTML, many attribute minimizations are forbidden.

    In XHTML, the following minimizations are false.
    <textarea readonly>
    <textarea disabled>

    True Example:-
    <textarea readonly="readonly">
    <textarea disabled="disabled">

If you are newbie to HTML, don't cross over to XHTML because it is more difficult than HTML and if any single error found in your xml document, the overall document's result might not display on the web-browser.