Global Attributes
Although most element attributes tend to be unique to the element, some are almost universal and usable with any element. Table 3.1 summarizes these attributes, showing which elements do take the attributes and how each attribute is used.
Table 3.1 Global XHTML Attributes
Attribute |
Purpose |
Used With |
class |
Space-separated list of classes of the element |
All elements except <base />, <head>, <html>, <meta />, <param />, <script>, <style>, and <title>. |
dir |
Direction for weak or neutral text |
All elements except <base />, <br />, <frame>, <frameset>, <hr />, <iframe>, <param />, and <script>. |
id |
Unique, document-wide identifier |
All elements except <base />, <head>, <html>, <meta />, <script>, <style>, and<title>. |
lang |
Specifies document language context (for backward compatibility with existing HTML) |
All elements except <base />, <br />, <frame>, <frameset>, <hr />, <iframe>, <param />, and <script>. |
style |
Binds style information to the element |
All elements except <base />, <head>, <html>, <meta />, <param />, <script>, <style>, and <title>. |
title |
Advisory title |
All elements except <base />, <head>, <html>, <meta />, <param />, <script>, and <title>. |
xml:lang |
Specifies document language context (as per XML 1.0) |
All elements except <base />, <param />, and <script>. |
The global attribute you'll probably use most often is the style attribute, which is used to assign style information to an element. To color a level 2 heading red, for example, you could use the XHTML:
<h2 style="color: red">Red Heading</h2>
NOTE
Note the use of Cascading Style Sheet instructions in the code above to apply formatting information. One of the intentions of XHTML is to expunge elements like <font> which have been used to specify formatting information.
The id attribute is also useful when you need to have a unique identifier for an element. This situation comes into play when you write scripts to support dynamic HTML documents because you frequently want to change the properties of some object in the document. To do this, you must be able to address the element that marks up the text via JavaScript, JScript, or VBScript, and the best way to do that is to give the element a unique name. Then it becomes fairly simple to address the element via the browser's object model.
lang can be helpful in situations in which you are marking up content in multiple languages. The value of lang gives browsers a "heads-up" as to what language is being used. lang is usually set equal to a two-character language code that denotes the language being used. For example, fr denotes French; de denotes German, and so on. In cases where variants on a language exist, you'll see expanded language codes, such as en-US for English spoken in the United States or en-Br for English spoken in Britain. The xml:lang attribute can take on the same values but was introduced as a separate attribute for easier compatibility with the XML standard.
dir refers to the directionalityleft-to-right or right-to-leftof text when it cannot otherwise be deduced from the context of the document. dir can take on values of ltr (left-to-right) or rtl (right-to-left).
The title attribute enables you to specify descriptive text to associate with the element. This information might be helpful to nonvisual browsers, such as those that generate speech or Braille output.
Finally, the class attribute enables you to create different classes of the same element. For example, you might have
<a href="xrefs.html" class="cross-reference"> ... </a> <a href="defns.html" class="definition"> ... </a> <a href="biblio.html" class="bibliography"> ... </a>
This creates three classes of the <a> element. After these classes are established, you can reference them elsewhere in your document. One popular application of this is in a style sheet:
a.cross-reference {color: navy} a.definition {color: yellow} a.bibliography {color: fuschia}
The style information shown here would color cross-reference links navy blue, definition links yellow, and bibliography links fuschia.