Sams Teach Yourself HTML 4 in 24 Hours

Sams Teach Yourself HTML 4 in 24 Hours

By Dick Oliver

Text Alignment

Some HTML tags allow you to specify a variety of options, or attributes, along with the basic tag itself. For example, when you begin a paragraph with the <p> tag you can specify whether the text in that paragraph should be aligned to the left margin, right margin, or center of the page.

To align a paragraph to the right margin, you can put align="right" inside the <p> tag at the beginning of the paragraph. To center a paragraph, use <p align="center">. Similarly, the tag to align a paragraph to the left is <p align="left">. (This is seldom used because paragraphs are always aligned to the left when you use plain old <p>.)

The word align is called an attribute of the <p> tag. You can use the align attribute with just about any HTML tag that contains text, including <h1>, <h2>, the other heading tags, and some tags you will meet later. There are many other attributes besides align. You will find out how to use them as you learn more HTML tags.

Attributes are special code words used inside an HTML tag to control exactly what the tag does.

According to the official HTML 4 standard, it doesn't matter whether tags and attributes are in uppercase or lowercase letters. However, the newer XHTML standard will require tags and attributes to be lowercase, so it's a very good idea to make all your HTML lowercase now for future compatibility. The new standard will also require quotation marks around attribute values.

For example, the following is technically okay now:

<P ALIGN=CENTER>

If you want to stay compatible with upcoming standards and software, you should always use the following instead:

<p align="center">

When you want to set the alignment of more than one paragraph or heading at a time, you can use the align attribute with the <div>, or division, tag. By itself, <div> and its corresponding closing </div> tag actually don't do anything at all—which would seem to make it a peculiarly useless tag!

Yet if you include an align attribute, <div> becomes quite useful indeed. Everything you put between <div align="center"> and </div>, for example, is centered. This may include lines of text, paragraphs, headings, images, and all the other things you'll learn how to put on Web pages in upcoming chapters. Likewise, <div align="right"> will right-align everything down to the next </div> tag.

Figure 5.1 demonstrates the align attribute with both the <p> and <div> tags. The results are shown in Figure 5.2. You'll learn many more advanced uses of the <div> tag in Hour 16, "Using Style Sheets," and Hour 20, "Setting Pages in Motion with Dynamic HTML."

05fig01.gif

Figure 5.1 The align attribute allows you to left-justify, right-justify, or center text.

05fig02.gif

Figure 5.2 The alignment settings in Figure 5.1, as they appear in a Web browser.

Share ThisShare This

Informit Network