Copyright © tutorialspoint.com

# XHTML Tips and Tricks

This chapter lists out various tips and tricks for you which should be taken care while writing an XHTML document.

## The XML Declaration:

An XML declaration is not required in all XHTML documents. XHTML document authors are strongly encouraged to use XML declarations in all their documents. Such a declaration is required when the character encoding of the document is other than the default UTF-8 or UTF-16.

## Empty Elements:

Include a space before the trailing / and > of empty elements, e.g. <br />, <hr /> and <img src="/html/xhtml.gif" alt="xhtml" />.

## Embedded Style Sheets and Scripts:

Use external style sheets if your style sheet uses < or & or ]]> or --.

Use external scripts if your script uses < or & or ]]> or --.

## Line Breaks within Attribute Values:

Avoid line breaks and multiple white space characters within attribute values. These are handled inconsistently by idfferent browsers.

## Isindex Element:

Don't include more than one isindex element in the document head. The isindex element is deprecated in favor of the input element.

## The lang and xml:lang Attributes:

Use both the lang and xml:lang attributes when specifying the language of an element. The value of the xml:lang attribute takes precedence.

## Element Identifiers:

XHTML 1.0 has deprecated the name attribute of the a, applet, form, frame, iframe, img, and map elements, and it will be removed from XHTML in subsequent versions. So start using id element for element identification.

## Using Ampersands in Attribute Values:

The ampersand character ("&") should be presented as as an entity reference &

Example

    <!-- This is invalid in XHTML -->
    http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user.

    <!-- Correct XHTML way of writing this is as follows -->
    http://my.site.dom/cgi-bin/myscript.pl?class=guest&amp;name=user

## White Space Characters in HTML vs. XML:

Some characters that are legal in HTML documents, are illegal in XML document. For example, in HTML, the Formfeed character (U+000C) is treated as white space, in XHTML, due to XML's definition of characters, it is illegal.

## The Named Character Reference &apos;

The named character reference &apos; (the apostrophe, U+0027) was introduced in XML 1.0 but does not appear in HTML. Web developers should therefore use &#39; instead of &apos; to work as expected in HTML 4 Web Browsers.

Copyright © tutorialspoint.com