126 Chapter 7 – Formatting Text
Layout Techniques with HTML
Web Design in a Nutshell, eMatter Edition
Centering Text Elements
There are two methods for centering text elements horizontally on a page: the
<center>tag, and thealignattribute. Bear in mind that these tags have been
deprecated by the HTML 4.0 Specification in favor of style sheet controls (although
browsers will continue to support them for a while).
The align attribute
The preferred way to center elements is to use the block-level tags’align
attribute with its value set tocenter. Thealignattribute can be added to the
paragraph tag (<p>), any heading tag (<h1>through<h6>), or a page division
(<div>). Be sure to close the tags at the end of the element.
In Figure 7-17, each element is centered individually usingalign=center.
As an alternative, you could enclose all three elements in a<div>tag with
align=center. Unfortunately, thealignattribute in a<div>tag is only recog-
nized by Internet Explorer version 3.0 and higher and by Navigator version 4.0, so
Figure 7-16: Use of the <wbr> tag within <nobr> text
Figure 7-17: Centering text
<NOBR>Good food gives more than immediate<WBR> pleasure.
It's a key to your family's health and vitality.</NOBR>
<WBR> tag breaks line at
designated point only if
entire line extends beyond
browser window.
<H2 ALIGN="center">Choosing the Best Foods</H2>
<P ALIGN="center">Good food gives more than immediate pleasure.
It's a key to your family's health and vitality, affecting the
way young people grow, the way you feel right now, and good
health in later years.</P>
<P ALIGN="center">There are simple guides to help you plan meals
that are interesting and varied.</P>