An Introduction to XHTML - No lazy programming allowed
(Page 2 of 5 )
One of the most striking differences between HTML and XHTML is the conformance to pre-defined rules. HTML allows Web programmers to be fairly lazy in the way they create their mark-up; tags may be written in upper or lowercase or even a combination of the two. Tags that are considered deprecated, or no longer relevant for use, may also be used, as can browser-specific tags, like the <layer> tag in Netscape. It is very easy to get into bad coding habits when working with HTML. For example, the following code is perfectly acceptable in HTML:
<p>Paragraph 1
XHTML is a much stricter language. All element tags must be written in lowercase, and any empty element tags must be closed off properly. An empty element is defined as an element that does not contain any actual text that is displayed by the browser. The <img> element is an excellent example of this. The following code snippet tells the browser to display an image called logo.gif contained in the same directory as the Web page:
<img src="logo.gif">
XHTML insists that any empty elements be closed in the correct manner with a space and a forward slash at the end:
<img src="logo.gif />
The space before the forward slash is not strictly necessary when using the latest browser versions, but for maximum compatibility with older browsers, the space should always be included. When using images in XHTML, remember that the alt attribute is a mandatory requirement, without which, your document will not validate.
Opening and closing, or paired elements do contain text that the browser displays, like the <p> or paragraph element:
<p>This text will appear in the main browser window</p>
XHTML demands that all opening and closing elements are paired correctly. I hope this example easily distinguishes the difference between empty and paired elements. It also specifies that all non-empty elements have both an opening and a closing tag.
Another difference is that all attribute values are enclosed within quotes in XHTML. In HTML quotes are not always used for attribute values, which again allows the programmer to slip into bad habits. Additionally, all attributes must have values; in the case of attributes that by default do not have values, you need to set the value equal to the attribute itself, the noshade attribute for example, must be set in the following format:
noshade="noshade"
Next: Conform to the rules >>
More HTML Articles
More By Dan Wellman