- Sprucing Up Your Text
- A Few Formatting Features You'll Use All the Time
- Textras: Fancier Text Formatting
A Few Formatting Features You'll Use All the Time
This section takes you through five more formatting tags that should stand you in good stead throughout your career as a web engineer. You use these tags for adding headings, aligning paragraphs, displaying "preformatted" text, inserting line breaks, and displaying horizontal lines. The next few sections give you the details.
Sectioning Your Page with Headings
Many web designers divide their page contents into several sections, like chapters in a book. To help separate these sections and thus make life easier for the reader, you can use headings. Ideally, headings act as minititles that convey some idea of what each section is all about. To make these titles stand out, HTML has a series of heading tags that display text in larger, bold fonts. There are six heading tags in all, ranging from <H1>, which uses the largest font, down to <H6>, which uses the smallest font.
What's with all the different headings? Well, the idea is that you use them to outline your document. As an example, consider the headings I've used in this chapter and see how I'd format them in HTML.
The overall heading, of course, is the chapter title, so I'd display it using, say, the <H1> tag. The first main section is the one titled "Sprucing Up Your Text," so I'd give its title an <H2> heading. That section contains three subsections, "Some Basic Text Formatting Styles," "Combining Text Formats," and "Accessorizing: Displaying Special Characters." I'd give each of these titles the <H3> heading. Then I come to the section called "A Few Formatting Features You'll Use All the Time." This is another main section of the chapter, so I'd go back to the <H2> tag for its title, and so on.
Webmaster Wisdom
Notice that I force the browser to display a less-than sign (<) by using the character code <, and to display a greater-than sign (>) by using the code >.
The following HTML document (look for headings.htm on the website) shows how I'd format all the section titles for this chapter, and Figure 3.2 shows how they appear in Internet Explorer on the Mac. (Notice that I don't need to use a <P> tag to display headings on separate lines; that's handled automatically by the heading tags.)
<HTML> <HEAD> <TITLE>Some Example Headings</TITLE> </HEAD> <BODY> <H1>From Buck-Naked to Beautiful: Dressing Up Your Page</H1> <H2>Sprucing Up Your Text</H2> <H3>Some Basic Text Formatting Styles</H3> <H3>Combining Text Formats</H3> <H3>Accessorizing: Displaying Special Characters</H3> <H2>A Few Formatting Features You'll Use All the Time</H2> <H3>Sectioning Your Page With Headings</H3> <H3>Aligning Paragraphs</H3> <H3>Handling Preformatted Text</H3> <H3>Them's the Breaks: Using <BR> for Line Breaks</H3> <H3>Inserting Horizontal Lines</H3> <H2>Textras: Fancier Text Formatting</H2> <H3>The <FONT> Tag I: Changing the Size of Text</H3> <H3>The <BASEFONT> Tag</H3> <H3>The <FONT> Tag II: Changing the Typeface</H3> <H3>Changing the Color of Your Page Text</H3> <H3>The <FONT> Tag III: Changing the Color</H3> <H3>The Dreaded <BLINK> Tag</H3> </BODY> </HTML>
Figure 3.2 Examples of HTML's heading tags.
Aligning Paragraphs
Centering text and graphics is a time-honored way to give reports and brochures a professional look and feel. To provide the same advantage to your web pages, the <CENTER> tag gives you centering capabilities for your page headings, paragraphs, lists, and even graphics. Here's how <CENTER> works:
<CENTER> [Headings, text, and graphics that you want centered go here.] </CENTER>
The <CENTER> tag is a nice, simple way to shift things to the middle of a page. How-ever, you can also use the <P> tag and the heading tags by shoehorning some extra text inside the tag. This extra text is called an attribute and it tells the browser to modify how it normally displays the tag.
For example, to center a paragraph, you use the following variation on the <P> tag theme:
<P ALIGN="CENTER">
When the browser stumbles upon the ALIGN attribute, it knows that it's going to have to modify the behavior of the <P> tag in some way. The exact modification is supplied by the value of the attribute, which is "CENTER" in this case. This orders the browser to display the entire contents of the following paragraph centered in the browser window.
Similarly, you can center, say, an <H1> heading like so:
<H1 ALIGN="CENTER">
The advantage to this approach is that you can also use either LEFT or RIGHT with the ALIGN attribute to further adjust your paragraph alignment. The LEFT value aligns the text on the left side of the window (that is, the normal alignment), and the RIGHT attribute aligns the text on the right side of the window.
Page Pitfalls
Always surround attribute values with quotation marks, like so:
<P ALIGN="RIGHT">
And if your page doesn't display properly when you view it in the browser, immediately check to see if you left off either the opening or closing quotation mark.
Handling Preformatted Text
In the previous chapter, I told you that web browsers ignore white space (multiple spaces and tabs) as well as carriage returns. Well, I lied. Sort of. You see, all browsers normally do spit out these elements, but you can talk a browser into swallowing them whole by using the <PRE> tag. The "PRE" part is short for "preformatted," and you normally use this tag to display preformatted text exactly as it's laid out. Here, "preformatted" means text in which you use spaces, tabs, and carriage returns to line things up.
Let's look at an example. The following bit of code is an HTML document (look for pre.htm on the website) in which I set up two chunks of text in a pattern that uses spaces and carriage returns. The first bit of doggerel doesn't make use of the <PRE> tag, but I've surrounded the second poem with <PRE> and </PRE>. Figure 3.3 shows the results. Notice that the lines from the first poem are strung together, but that when the browser encounters <PRE>, it displays the white space and carriage returns faithfully.
<HTML> <HEAD> <TITLE>The <PRE> Tag</TITLE> </HEAD> <BODY> <H3>Without the <PRE> Tag:</H3> Here's some ditty, specially done, to lay it out all formatted and pretty. Unfortunately, that is all this junk really means, because I admit I couldn't scrawl poetry for beans. <H3>With the <PRE> Tag:</H3> <PRE> Here's some ditty, specially done, to lay it out all formatted and pretty. Unfortunately, that is all this junk really means, because I admit I couldn't scrawl poetry for beans. </PRE> </BODY> </HTML>
Webmaster Wisdom
You'll notice one other thing about how the browser displays text that's ensconced within the <PRE> and </PRE> tags: It formats the text in an ugly monospaced font. The only way to get around this is to use something called a "style sheet" to specify the font you want the browser to use with the <PRE> tag. I show you how this works in Part 3, "High HTML Style: Working with Style Sheets."
Figure 3.3 How preformatted text appears in Netscape 6.
Them's the Breaks: Using <BR> for Line Breaks
As you saw in the previous chapter, you use the <P> tag when you need to separate your text into paragraphs. When a browser spies a <P> tag, it starts a new paragraph on a separate line and inserts an extra, blank line after the previous paragraph. However, what if you don't want that extra line? For example, you might want to display a list of items with each item on a separate line and without any space between the items. (Actually, there are better ways to display lists than the method I show you here; see Chapter 4, "The Gist of a List: Adding Lists to Your Page.")
Well, you could use the <PRE> tag, but your text would appear in that ugly, monospaced font. A better solution is to separate your lines with <BR>, the line break tag. A browser starts a new line when it encounters <BR>, but it doesn't toss in an extra blank line. Here's an example (it's the file named breaks.htm on the website):
<HTML> <HEAD> <TITLE>Line Breaks</TITLE> </HEAD> <BODY> <H2>Supply Without the Demand</H2> <HR> <TT>Print-on-demand</TT> is new system that lets bookstores print out a book whenever a customer asks for one. It's <I>really</I> slick. Recently, I was in a store called <b>The Book "Seller"</B> and it had a service called <B>Print- On-Non-Demand</B>® featuring books that <I>very</I> few people would ever order, much less print out. The titles included the following <P> The Complete Idiot's Guide to Village Idiocy<BR> The 10-Minute Guide to Butt-Scanning<BR> Baby's First Book of Java<BR> Leashes Unleashed<BR> Programmer's Guide to Basic Hygiene<BR> Teach Yourself the Presidency in 4 Years </P> </BODY> </HTML>
In the list of books, I added the <BR> tag to the end of each line (except the last one; I don't need it there). As you can see in Figure 3.4, Internet Explorer dutifully displays each line separately, with no space in between.
Figure 3.4 Use the <BR> tag to force a line break in your text.
Inserting Horizontal Lines
If you're particularly eagle-eyed, you might have noticed a horizontal line extending across the browser screen shown in Figure 3.4. What gives? Well, while you weren't looking, I surreptitiously inserted an <HR> tag into the HTML text. <HR>, which stands for "horizontal rule," produces a line across the page, which is a handy way to separate sections of your document.
If you use <HR> by itself, you get a standard line that goes right across the page. How-ever, there are various attributes associated with the <HR> tag that enable you to change the line's size, width, alignment, and more. Table 3.3 shows a rundown.
Table 3.3 Extra Attributes for the <HR> Tag
<HR> Extension |
What It Does |
<HR WIDTH="x"> |
Sets the width of the line to x pixels |
<HR WIDTH="y%"> |
Sets the width of the line to y percent of the window |
<HR SIZE="n"> |
Sets the thickness of the line to n pixels (where the default thickness is 1 pixel) |
<HR ALIGN="LEFT"> |
Aligns the line with the left margin |
<HR ALIGN="CENTER"> |
Centers the line |
<HR ALIGN="RIGHT"> |
Aligns the line with the right margin |
<HR NOSHADE> |
Displays the line as a solid line (instead of appearing etched into the screen) |
Note that you can combine two or more of these attributes in a single <HR> tag. For example, if you want a line that's half the width of the window and is centered, then you'd use the following tag:
<HR WIDTH="50%" ALIGN="CENTER">
Webmaster Wisdom
The <HR> tag draws a plain horizontal line. You might notice some web pages have fancier lines that use color and other neat texture effects. Those lines are actually images; I show you how to add images to your page in Chapter 6, "A Picture Is Worth a Thousand Clicks: Working with Images."