Generic Elements (Div and Span) : Divide It Up With A Div
Generic Elements (Div and Span) : Divide It Up With A Div
By putting those elements in a div, I’ve made it clear that they are conceptu-
ally related. It will also allow me to style two p elements within listings dif-
ferently than other paragraphs on the page.
Here is another common use of a div used to break a page into sections
for layout purposes. In this example, a heading and several paragraphs are
enclosed in a div and identified as the “news” division.
<div id="news">
<h1>New This Week</h1>
<p>We've been working on...</p>
<p>And last but not least,... </p>
</div>
1
Generic Elements (div and span)
Now that I have an element known as “news,” I could use a style sheet to
position it as a column to the right or left of the page. You might be think-
ing, “Hey Jen, couldn’t you use a section element for that?” You could! In
fact, authors may turn to generic divs less now that we have better semantic
grouping elements in HTML5.
You can see how the classified spans add meaning to what otherwise might
be a random string of digits. As a bonus, the span element enables us to
apply the same style to phone numbers throughout the site (for example,
ensuring line breaks never happen within them, using a CSS white-space:
nowrap declaration). It makes the information recognizable not only to
humans but to computer programs that know that “tel” is telephone number
information. In fact, some values—including “tel”—have been standardized
in a markup system known as Microformats that makes web content more
useful to software (see the Microformats and Metadata sidebar).
2
Generic Elements (div and span)
<div id="ISBN0321127307">
<img src="felici-cover.gif" alt="">
<p><cite>The Complete Manual of Typography</cite>, James Felici</p>
<p>A combination of type history and examples of good and bad type.
</p>
</div>
<div id="ISBN0881792063">
<img src="bringhurst-cover.gif" alt="">
Not Just for divs
<p><cite>The Elements of Typographic Style</cite>, Robert Bringhurst The id and class attributes may be
</p> used with all elements in HTML5,
<p>This lovely, well-written book is concerned foremost not just div and span. For example,
with creating beautiful typography.</p> you could identify an ordered list as
</div> “directions” instead of wrapping it in
a div.
Web authors also use id when identifying the various sections of a page. In
<ol id="directions">
the following example, there may not be more than one element with the id
<li>...</li>
of “main,” “links,” or “news” in the document. <li>...</li>
<section id="main"> <li>...</li>
<!-- main content elements here --> </ol>
</section> Note that in HTML 4.01, id and
class may be used with all elements
<section id="news"> except base, basefont, head, html,
<!-- news items here --> meta, param, script, style, and title.
</section>
<aside id="links">
<!-- list of links here -->
</aside>
3
Generic Elements (div and span)
Tip Notice how the same element may have both a class and an id. It is also
possible for elements to belong to multiple classes. When there is a list of
The id attribute is used to identify. class values, simply separate them with character spaces. In this example,
The class attribute is used to classify. I’ve classified each div as a “book” to set them apart from possible “cd” or
“dvd” listings elsewhere in the document.
<div id="ISBN0321127307" class="listing book">
<img src="felici-cover.gif" alt="CMT cover">
<p><cite>The Complete Manual of Typography</cite>, James Felici</p>
<p class="description">A combination of type history and examples of
good and bad type.</p>
</div>
This should have given you a good introduction to how div and span ele-
ments with class and id attributes are used to add meaning and organi-
zation to documents. We’ll work with them even more in the style sheet
chapters in Part III.