English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Basic Tutorial

HTML Media

HTML Reference Manual

HTML5 Basic Tutorial

HTML5 API

HTML5 Media

XHTML Introduction

XHTML is HTML written in XML format

What is XHTML?

  • XHTML stands for Extensible Hypertext Markup Language

  • XHTML vs HTML 4.01 Almost the same

  • XHTML is a stricter and purer version of HTML

  • XHTML is defined as HTML in the way of XML applications

  • XHTML is 2001 Year 1 W published monthly3C Recommended Standard

  • XHTML is supported by all mainstream browsers

Why use XHTML?

Many web pages on the Internet contain "poor" HTML.

If viewed in a browser, the following HTML code runs very normally (even though it does not follow HTML rules):

<html>
<head>
<meta charset="utf-8">
<title>This is a non-standard HTML</title>
<body>
<h1>Non-standard HTML
<p>This is a paragraph
</body>

XML is a markup language that must be properly marked and well-formed.

Today's technology industry has some different browser technologies. Some run on computers, while others may run on mobile phones or other small devices. Small devices often lack the resources and capabilities to interpret "poor" markup languages.

So - By combining the strengths of XML and HTML, XHTML was developed. XHTML is a re-designed HTML as XML.

The most important difference compared to HTML:

document structure

  • The XHTML DOCTYPE ismandatory

  • The XML namespace attribute in <html> ismandatory

  • <html>, <head>, <title>, and <body> are alsomandatory

element syntax

  • XHTML elements mustcorrectly nested

  • XHTML elements must alwaysclosed

  • XHTML elements mustin lowercase

  • An XHTML document must havea root element

attribute syntax

  • XHTML attributes must usein lowercase

  • XHTML attribute values must beEnclosed in quotes

  • XHTML attribute minimization is alsoProhibited

<!DOCTYPE ....> is mandatory

An XHTML document must have an XHTML document type declaration (XHTML DOCTYPE declaration).

You can find a complete XHTML document type.

The <html>, <head>, <title>, and <body> elements must also be present, and the xmlns attribute in <html> must be used to define the XML namespace for the document.

The following example demonstrates an XHTML document with the minimum 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>
  <meta charset="utf-8">
  <title>Document title</title>
</head>
 
<body>
Document content
</body>
 
</html>

XHTML elements must be properly nested

In HTML, some elements can be nested without nesting, like this:

<b><i>Bold and italic text</b></i>

In XHTML, all elements must be properly nested, like this:

<b><i>Bold and italic text</i></b>

XHTML elements must have closing tags

Incorrect example:

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

Correct example:

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

Empty elements must contain a closing tag

Incorrect example:

Line break:<br
Horizontal line: <hr>
Image: <img src="happy.gif" alt="Happy face">

Correct example:

Line break:<br />
Horizontal line: <hr />
Image: <img src="happy.gif" alt="Happy face" />

XHTML elements must be lowercase

Incorrect example:

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

Correct example:

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

Attribute names must be lowercase

Incorrect example:

<table WIDTH="100%">

Correct example:

<table width="100%">

Attribute values must be quoted

Incorrect example:

<table width=100%>

Correct example:

<table width="100%">

Attribute abbreviations are not allowed

Incorrect example:

<input checked>
<input readonly>
<input disabled>
<option selected>

Correct example:

<input checked="checked">
<input readonly="readonly">
<input disabled="disabled">
<option selected="selected">

How to convert HTML to XHTML

  • Add an XHTML <!DOCTYPE> to your webpage

  • Add the xmlns attribute to each HTML element on each page.

  • Change all elements to lowercase

  • Close all empty elements

  • Change all attribute names to lowercase

  • All attribute values should be quoted

  • Use W3C Validator to test your XHTML

    Please enter your website URL in the input box below: