Boldface, Italics, and Special Text Formatting


Way back in the age of the typewriter, we were content with plain-text and an occasional underline for emphasis. Today, boldface and italic text have become de rigueur in all paper communication. Naturally, you can add bold and italic text to your web pages too. There are several tags and style rules that make text formatting possible.

The tried-and-true approach to adding bold and italic formatting to text involves the <b> and <i> tags. For boldface text, you put the <b> tag at the beginning of the text and </b> at the end. Similarly, you can make any text italic by enclosing it between <i> and </i>. Although this approach still works fine in browsers and is supported by XHTML, it isn't as flexible or powerful as the newer CSS style rule approach to text formatting.

You can nest one type of formatting inside another. For instance, if you want some text to be both bold and italic, put <b><i> in front of it and </i></b> after it. To avoid confusing some web browsers, be careful to close the tags in the opposite order in which you opened them in. For example, don't do this:

 <b>Bold, <i>bold and elegant,</b> or just plain elegant.</i> 


Instead, do it this way:

 <b>Bold, <i>bold and elegant,</i></b><i>or just plain elegant.</i> 


Although you'll learn much more about CSS style rules in Part III, it's worth a little foreshadowing just so you understand the text formatting options. The font-weight style rule allows you to set the weight, or boldness, of a font using a style rule. Standard settings include normal, bold, bolder, and lighter, with normal being the default. Italic text is controlled via the font-style rule, which can be set to normal, italic, or oblique. Style rules can be specified together if you want to apply more than one, as the following example demonstrates:

 <p style="font-weight:bold; font-style:italic">This paragraph is bold and italic!</p> 


In this example, both style rules are specified in the style attribute of the <p> tag. The key to using multiple style rules is that they must be separated by a semicolon (;).

You aren't limited to using font styles in paragraphs, however. The following code shows how to italicize text in a bulleted list:

 <ul>   <li style="font-style:italic">Important Stuff</li>   <li style="font-style:italic">Critical Information</li>   <li style="font-style:italic">Highly Sensitive Material</li>   <li>Nothing All That Useful</li> </ul> 


Getting back to the traditional <b> and <i> tags, you can code the same bulleted list with those tags instead of using a style rule. You should always close any <b>, <i>, or other formatting tags that occur within an <li> list item or heading before you end the </li>. Most of the <i> and </i> tags in the following list may seem redundant, but adhering to this good form ensures that your pages fully meet the new XHTML standards, which may save you from having to rewrite them in the future:

 <ul>   <li><i>Important Stuff</i></li>   <li><i>Critical Information</i></li>   <li><i>Highly Sensitive Material</i></li>   <li>Nothing All That Useful</li> </ul> 


You can also use the <b> tag and font-weight style rule within headings, but a heavier font usually doesn't have an effect on headings because they are already bold.

By the Way

An alternative to style rules when it comes to bold and italic text involves the <strong> and <em> tags. The <strong> tag does the same thing as the <b> tag in most browsers, whereas the <em> tag acts just like the tag <i> by formatting text as italics.

The <strong> and <em> tags are considered by some to be an improvement over <b> and <i> because they imply only that the text should receive special emphasis, rather than dictating exactly how that effect should be achieved. In other words, a browser doesn't necessarily have to interpret <strong> as meaning bold or <em> as meaning italic. This makes <strong> and <em> more fitting in XHTML because they add meaning to text, along with affecting how the text should be displayed. Even so, as long as the <b> and <i> tags are still fully supported, which they are, you shouldn't have any problem using them.


In addition to <b>, <i>, <strong>, and <em>, there are several other HTML tags for adding special formatting to text. Table 5.1 summarizes all of them (including the boldface and italic tags), and Listing 5.5 and Figure 5.5 demonstrate each of them in action.

Table 5.1. HTML Tags That Add Special Formatting to Text

Tag

Function

<small>

Small text

<big>

Big text

<sup>

Superscript text

<sub>

Subscript text

<em> or <i>

Emphasized (italic) text

<strong> or <b>

Strong (boldface) text

<tt>

Monospaced text (typewriter font)

<pre>

Monospaced text, preserving spaces and line breaks


Figure 5.5. Here's what all the character formatting from Listing 5.5 looks like.


Watch Out!

There used to be a <u> tag for creating underlined text, but there are a couple of reasons not to use it now. First off, people expect underlined text to be a link, and may get confused if you underline text that isn't a link. Secondly, the <u> tag is deprecated, which means that it has been phased out of the HTML/XHTML language. Along with the <strike> tag for creating strikethrough text, the <u> tag is still supported in web browsers and likely will be for quite a while, but the preferred approach to creating underlined and strikethrough text is style sheets. These topics are covered in more detail in Hour 12, "Formatting Web Pages with CSS Style Sheets," and Hour 13, "Digging Deeper into Style Sheet Formatting."


Listing 5.5. Special Formatting Tags
 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">   <head>     <title>The Miracle Product</title>   </head>   <body>     <p>       New <sup>Super</sup><strong>Strength</strong> H<sub>2</sub>O       <em>plus</em> will knock out any stain, <big>big</big> or       <small>small</small>.<br /> Look for new       <sup>Super</sup><b>Strength</b> H<sub>2</sub>O <i>plus</i>       in a stream near you.     </p>     <p>       <tt>NUTRITION INFORMATION</tt> (void where prohibited)     </p>     <pre>                  Calories   Grams   USRDA                  /Serving   of Fat  Moisture Regular           3          4        100% Unleaded          3          2        100%  Organic           2          3         99% Sugar Free        0          1        110%     </pre>   </body> </html> 

The <tt> tag usually changes the typeface to Courier New, a monospaced font. (Monospaced means that all the letters and spaces are the same width.) However, web browsers let users change the monospaced <tt> font to the typeface of their choice (under Tools, Internet Options, Fonts in Microsoft Internet Explorer, and under ToolsToolsToo, Options, General, Fonts & Colors in Mozilla Firefox). The monospaced font may not even be monospaced for some users, though the vast majority of people stick with the standard fonts that their browsers come set up with.

The <pre> tag causes text to appear in the monospaced font, but it also does something else unique and useful. As you learned in Hour 2, multiple spaces and line breaks are normally ignored in HTML files, but <pre> causes exact spacing and line breaks to be preserved. For example, without <pre>, the text at the end of Figure 5.5 would look like the following:

 calories grams usrda /serving of fat moisture regular 3 4 100% unleaded 3 2 100% organic 2 3 99% sugar free 0 1 110% 


Even if you added <br /> tags at the end of every line, the columns wouldn't line up properly. However, when you put <pre> at the beginning and </pre> at the end, the columns line up properly because the exact spaces are keptno <br /> tags are needed.

There are fancier ways to make columns of text line up, and you'll learn all about them in Part III. The <pre> tag gives you a quick and easy way to preserve the alignment of any monospaced text files you might want to transfer to a web page with a minimum of effort.

By the Way

You can use the <pre> tag as a quick way to insert extra vertical space between paragraphs. For example, to put several blank lines between the words up and down, you could type this:

 up<pre> </pre>down 






SAMS Teach Yourself HTML and CSS in 24 Hours
Sams Teach Yourself HTML and CSS in 24 Hours (7th Edition)
ISBN: 0672328410
EAN: 2147483647
Year: 2005
Pages: 345

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net