You are here

XHTML

19 January, 2016 - 16:44

HTML has contributed greatly to the growth of the Web because it is easy to learn and very forgiving of errors and non-standard markup. Take a look at Figure 1.10.

Figure 1.10 Sample HTML containing errors and non-standard markup

HTML rendering result:

If you load this HTML into a Web browser, it will display correctly even though there are two mistakes in the code.

Can you find the mistakes?

HTML’s tolerance for such inconsistencies has made it accessible to everyone, from professional Web developers to beginning Web designers. However, its flexibility comes with a price.

Due to these limitations, the World Wide Web Consortium (W3C) now recommends using XHTML instead of HTML to write your webpages.

The next activity aims to help you understand the difference between a webpage written in HTML and in XHTML. Please note that XHTML files also end with a .html extension.

Activity 5

Suggested answers to Activity 5

From the previous activity, you’ve seen that many of the HTML tags you know are still used in XHTML. You just have to be more consistent and disciplined in how you code these tags.

Now let’s look at Figure 1.11, which is a basic template for producing XHTML documents. Don’t worry if you don’t understand the details of each statement within the code. Everything will be explained in the next reading. In the meantime, just focus on the structure of the XHTML document.

Figure 1.11 Basic template for producing XHTML documents

As you can see, the structure of an XHTML document is almost the same as an HTML document. It includes both the head and the body elements. The main difference you may have spotted would be the DOCTYPE declaration at the start of the document and the xmlns attribute of the html element.

The next reading describes the rules for writing valid XHTML. Many of the rules are derived from XML, since XHTML is just another application of XML. This online article was written for the staff of the New York Public Library but the introduction is well-written and can be applied by Web developers everywhere.

Reading 1: 'XHTML guidelines: New York Public Library Online Style Guide'.

In the next activity, we will submit the XHTML webpage from Activity 5 to an online validator.

Activity 6

Suggested answers to Activity 6

You may have realized by now that you have to be more consistent and disciplined in how you code your markup in XHTML than in HTML. To encourage you to stick with the recommended standards, the next reading describes what can be gained from authoring your pages using XHTML instead of HTML.

Reading 2: 'XHTML benefits: New York Public Library Online Style Guide '.

As you can see, XHTML produces valid, well-formed webpages which can be viewed consistently and accurately no matter which platform is used — whether it’s a desktop computer, wireless handheld device, mobile phone or Braille reader.

It’s good to get started on the right foot with XHTML since it doesn’t take a lot of effort once you get used to it. It also comes with the added bonus of producing pages that work more consistently on all browsers.