10.5. Generic Elements (div and span)The generic div and span elements provide a way for authors to create custom elements. The div element is used to indicate block-level elements, while span indicates inline elements. Both generic elements rely on id and class attributes to give them a name, meaning, or context. 10.5.1. The Versatile divThe div element is used to identify and label any block-level division of text, whether it is a few list items or an entire page.
<div>...</div> Attributes
Deprecated attributes
By marking a section of text as a div and giving it a name using id or class attributes, you are essentially creating a custom HTML element. In this example, a heading and a list are enclosed in a div identified as "sidebar." <div > <h1>List of links</h1> <ul> <li>Resource 1</li> <li>Resource 2</li> <li>Resource 3</li> </ul> </div> Because a div is a block-level element, its contents will start on a new line (even text not contained within other block-level elements). Otherwise, div elements have no inherent presentation qualities of their own. The div really shines when used in conjunction with Cascading Style Sheets. Once you've marked up and named a div, you can apply styles to all of its contents or treat it as a box that can be positioned on the page, for instance, to form a new text column. A div may also be called on by script, applet, or other processing by user agents. 10.5.2. The Useful spanLike the div element, span allows authors to create custom elements. The difference is that span is used for inline elements and does not introduce a line break.
<span>...</span> Attributes
This is a simple example of a span used to identify a telephone number. Jenny: <span class="telephone">867.5309</span> Markup like this has a number of uses. Most commonly, it is a "hook" that can be used to apply style sheet rules. In this example, all elements labeled as telephone may receive the same presentational instructions, such as to be displayed in bold, blue text. The span also gives meaning to an otherwise random string of digits to user agents who know what to do with telephone information. This is discussed a bit more in the next section. 10.5.3. Element Identifiers (class and id)The previous examples show how the id and class attributes are used to turn generic div and span elements into elements with specific meanings and uses. It should be pointed out that class and id attributes may be used with nearly all (X)HTML elements, not just div and span. This section discusses the id and class element identifiers and their distinct uses. 10.5.3.1. id identifierThe id attribute is used to give an element a specific and unique name in the document. In the earlier div example, id was used to label a section of the page as "sidebar." That means there may be no other element with in that document (although, it is okay if it appears in other documents on the same site). ID values must be unique. The HTML 4.01 Recommendation specifies the following uses for id attribute:
10.5.3.2. class identifierThe class attribute is used for grouping similar elements. Multiple elements may be assigned the same class name, and doing so enables them to be treated similarly. In the span example above, the telephone number was identified as telephone with the class attribute. This implies that there may be many more telephone numbers in the document. A single style sheet rule could then be used to make them all bold and blue. Changing them all to green requires editing just one line of code. This offers an obvious advantage over changing color one by one with the deprecated font element. In addition to being inefficient to maintain, font doesn't add any semantic cues for user agents. According to the HTML 4.01 specification, the class attribute may be used:
In HTML 4.01, id and class attributes may be used with all elements except base, basefont, head, html, meta, param, script, style, and title. In XHTML, id support has been added to those elements. 10.5.3.3. Tips on using classThere is a heady exhilaration that comes with the ability to create your own custom elements using id and class. The class attribute in particular is prone to misuse. These tips should provide some basic guidance for keeping your markup clean.
|