Tutorials
THE WORLD'S LARGEST WEB DEVELOPER SITE

HTML and XHTML

XHTML is called as HTML written as XML.

What Is XHTML?

 

HTML paragraphs are getting defined using the <p> and </p> tag:

  • XHTML denotes EXtensible HyperText Markup Language
  • XHTML is almost identical and similar to HTML
  • XHTML is more stricter when compared to HTML
  • XHTML is known as HTML defined as an XML application
  • XHTML is supported by most of the browsers

Why XHTML?

 

Many pages of the internet consists of "bad" HTML

This HTML code seems to work fine in most of the browsers (even if it is not follow the HTML rules):

<html>
<head>
  <title>This is bad HTML</title>

<body>
  <h1>Bad HTML
  <p>This is a paragraph
</body>

The present market has many different browser technologies. Some browsers are running on computers, and some browsers are running on mobile phones or other small devices. Smaller devices are usually lacking the resources or power for interpreting "bad" markup.

XML is a known as a markup language where documents should be marked up correctly (be "well-formed").

If you want to learn more about XML, please have a look at our XML tutorial.

Combination of the strengths of HTML and XML, XHTML was developed.

XHTML can be called as HTML redesigned as XML.

The Most Important Differences from HTML:

 

Document Structure

  • XHTML DOCTYPE is mandatory
  • The xmlns attribute in <html> should be mandatory
  • <html>, <head>, <title>, and <body> are also mandatory

XHTML Elements

  • XHTML elements should be properly nested
  • XHTML elements should always be closed
  • XHTML elements should be in lowercase
  • XHTML documents should have one root element

XHTML Attributes

  • Attribute names should be in lower case
  • Attribute values should be quoted
  • Attribute minimization is not allowed or forbidden

<!DOCTYPE ....> Is Mandatory

 

An XHTML document should be having an XHTML DOCTYPE declaration.

A entire list of all the XHTML Doctypes are found in HTML Tags Reference.

The <html>, <head>, <title>, and <body> elements should also be present, and the xmlns attribute in <html> should indicate the xml namespace for the document.

This example illustrates an XHTML document which is having a minimum of required tags:

<!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>Title of document</title>
</head>

<body>
  some content
</body>

</html>

XHTML Elements Must Be Properly Nested

 

In HTML, some elements are improperly nested inside each other,similar to like this:

<b><i>This text is bold and italic</b></i>

In XHTML, all elements should be nested inside each other,similar to like this:

<b><i>This text is bold and italic</i></b>

XHTML Elements Must Always Be Closed

 

This is not correct:

<p>This is a paragraph
<p>This is another paragraph

This is correct:

<p>This is a paragraph</p>
<p>This is another paragraph</p>

Empty Elements Must Also Be Closed

 

This is not correct:

A break: <br>
A horizontal rule: <hr>
An image: <img src="happy.gif" alt="Happy face">

This is correct:

A break: <br />
A horizontal rule: <hr />
An image: <img src="happy.gif" alt="Happy face" />

XHTML Elements Must Be In Lower Case

 

This is not correct:

<BODY>
<P>This is a paragraph</P>
</BODY>

This is correct:

<body>
<p>This is a paragraph</p>
</body>

XHTML Attribute Names Must Be In Lower Case

 

This is not correct:

<table WIDTH="100%">

This is correct:

<table width="100%">

Attribute Values Must Be Quoted

 

This is not correct:

<table width=100%>

This is correct:

<table width="100%">

Attribute Minimization Is Forbidden

 

Not correct:

<input type="checkbox" name="vehicle" value="car" checked />

Correct:

<input type="checkbox" name="vehicle" value="car" checked="checked" />

Wrong:

<input type="text" name="lastname" disabled />

Correct:

<input type="text" name="lastname" disabled="disabled" />

How to Convert from HTML to XHTML

 

HTML paragraphs will be defined using the <p> and </p> tag:

  1. By adding an XHTML <!DOCTYPE> in the first line of every page
  2. By adding an xmlns attribute in the html element of every page
  3. Changing all the element names to lowercase
  4. Closing all empty elements
  5. Changing all the attribute names to lowercase
  6. Adding Quote to all attribute values