HTML Tutorial | HTML Extension | HTML and XHTML
XHTML (Extensible HTML)
XHTML is a markup language with the same expressive power as HTML, but it has stricter syntax than HTML.
Why Use XHTML, a Stricter Version?
Recently, web content has moved beyond the traditional PC-centered environment and is being used more often across many different platforms.
As a result, more environments have limited resources for supporting incorrect HTML syntax.
Because an XHTML document is an XML document and must be syntactically correct, it can be processed automatically with standard XML libraries, unlike HTML.
Changes in XHTML
Document Structure
- The XHTML DOCTYPE must be specified.
- The xmlns attribute of the
<html>tag must be specified. - The
<html>,<head>,<title>, and<body>tags must be used.
Document Elements
- Every tag must be closed.
- Every tag must be closed in the proper order.
- Every element must be written in lowercase.
- An XHTML document must contain one root element.
Document Attributes
- Attribute names must be written in lowercase.
- Attribute values must be enclosed in quotes.
- Attribute-value omission is no longer allowed, so attribute values must be specified.
Differences Between HTML and XHTML
The following examples show the differences between HTML and XHTML.
They are errors that should not be written in XHTML.
- An empty tag without an end tag must include a slash (
/) at the end, preceded by a space.
HTML : <br>
XHTML : <br />
- Non-empty elements must have an end tag.
HTML : <p>First sentence</p><p>Second sentence
XHTML : <p>First sentence</p><p>Second sentence</p>
- Tag elements must be closed in the order they were opened.
HTML : <em><strong>This is some text.</em></strong>
XHTML : <em><strong>This is some text.</strong></em>
- Image tags must include the alt attribute.
HTML : <img src="devkuma_logo.png" />
XHTML : <img src="devkuma_logo.png" alt="Devkuma" />
- All text must be wrapped in tags.
HTML : <body>Text paragraph used in the body</body>
XHTML : <body><p>Text paragraph used in the body</p></body>
- Attribute values must be enclosed in quotes.
HTML : <td rowspan=3>
XHTML : <td rowspan="3">
- Tags and attributes must use lowercase only.
HTML : <BODY><P>Tags and attributes</P></BODY>
XHTML : <body><p>must use lowercase only.</p></body>
- Attribute values cannot be omitted, so they must be specified.
HTML : <textarea readonly>Read-only.</textarea>
XHTML : <textarea readonly="readonly">Read-only.</textarea>
All of the HTML examples above do not meet XHTML requirements, but they work normally in HTML.
How to Convert an HTML Document to an XHTML Document
- Add the following code to the first line.
<!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/2018/xhtml"> - Add the xmlns attribute.
- Change all tag names to lowercase.
- Close all empty tags.
- Change all attribute names to lowercase.
- Enclose all attribute values in quotes.