tag are formatted using properties from three different styles (see Figure 5-6).
5.4.3. Overcoming Conflicts Because of how CSS properties sometimes conflict when several styles apply to the same tag, you'll sometimes find your pages don't look exactly as you planned.
Figure 5-6. Due to inheritance and the cascade, a single tag on a page can get its properties from multiple CSS styles, creating a hybrid style. The final appearance of the two paragraphs below the headline beginning "Just Say No" (see Figure 5-5) comes from properties of three styles listed in this diagram from least to most specific (top to bottom). The crossed-out properties are overridden by more specific styles.
Section 5.4. Tutorial: The Cascade in Action
When that happens you'll need to do a little work to find out why, and rejigger your CSS selectors to make sure the Cascade is working to produce the results you want. 1. Return to your Web page editor and the cascade.html file. You'll now create a new style that you'll use to highlight the introductory paragraph of the #note section. 2. Add the following style to the internal style sheet below the body tag style you created in Section 5.4.2: .intro { font-weight: bold; color: #FF0000; }
Next, you'll apply this class style to a tag. 3. Locate the
. What gives? Following the rules of the cascade, .intro is a basic class selector, while the #note p is a descendent selector composed of both an ID and a tag name. These add up to create a more specific style, so its style properties overrule any conflict between it and the .intro style. In order to make the .intro style work, you'll need to give it a little juice by making its selector more powerful. 5. Return to the cascade.html file in your Web page editor and change the name of the style from .intro to #note p.intro. Now you have a descendent selector composed of an ID, a tag, and a class. This style's more specific than #note p and its properties override those in any less specific style. 6. Preview the page in a Web browser. Voila! The paragraph changes to bright red. If you didn't have a clear understanding of the cascade, you'd be scratching your head wondering why the color property didn't work. In this and the previous four chapters, you've covered the basics of CSS. Now, in Part 2, it's time to take that knowledge and apply it to real design challenges, making Web pages look great.
Part II: Applied CSS
Part II: Applied CSS Chapter 6: Formatting Text Chapter 7: Margins, Padding, and Borders Chapter 8: Adding Graphics to Web Pages Chapter 9: Sprucing Up Your Site's Navigation Chapter 10: Formatting Tables and Forms
Chapter 6. Formatting Text
Chapter 6. Formatting Text Most Web sites still rely on words to get their messages across. Sure, people like to look at photos, movie clips, and Flash animations, but it's the reading material that keeps 'em coming back. People are hungry for news, gossip, how-to articles, recipes, FAQs, jokes, information lists, and other written content. With CSS, you canand shouldmake your headlines and body text grab a visitor's attention as compellingly as any photo. CSS offers a powerful array of text-formatting options, which let you assign fonts, color, sizes, line-spacing and many other properties that can add visual impact to headlines, bulleted lists, and regular old paragraphs of text (see Figure 6-1). This chapter reveals all, and then finishes up with a tutorial where you can practice assembling CSS text styles and put them to work on an actual Web page.
Section 6.1. Formatting Text
6.1. Formatting Text The first thing you can do to make text on your Web site look more exciting is to apply different fonts to headlines, paragraphs, and other written elements on your pages. To apply a font to a CSS style, you use the font-family property:
font-family: Arial;
Note: In real life, when you put a CSS property into action, you must, of course, include all the other necessities of a complete style declaration block and style sheet, as described in Chapter 2: p { font-family: Arial; }, for example. When you see examples like font-family: Arial;, remember that's just the property in isolation, distilled down for your bookreading benefit.
Figure 6-1. Why settle for boring and drab text (top), when you can make your headlines scream and your text sing with a few simple CSS properties (bottom)?
Section 6.1. Formatting Text
UP TO SPEED Know Your Font Types
Section 6.1. Formatting Text
You can find literally tens of thousands of different fonts to express your every thought: from bookish, staid, and classical type faces to rounded, cartoonish squiggles. But almost all fonts fall into one of two categories: serif and sans-serif. Serif fonts are best for long passages of text, as it's widely believed that the serifsthose tiny "hands" and "feet" at the end of a letter's main strokesgently lead the eye from letter to letter, making text easier to read. Examples of serif fonts are Times, Times New Roman, Georgia, and the font in the main body paragraphs of this book. Sans-serif fonts, on the other hand, are often used for headlines, thanks to their clean and simple appearance. Examples of sans-serif fonts include Arial, Helvetica, Verdana, and Formata, which you're reading now. Some people believe that you should use only sans-serif fonts on Web pages because they think the delicate decorative strokes of serif fonts don't display well on the coarse resolution of a computer screen. In the end, your aesthetic judgment is your best guide. Pick the types of fonts you think look best.
6.1.1. Choosing a Font Choose a font that makes your text eye-catching (especially if it's a headline) and readable (especially if it's main body text), as discussed in the box above. Unfortunately, you can't use just any font you want. Well, actually you can use any font you want, but it won't show up on a viewer's computer unless she's installed the same font on her system. So that handcrafted font you purchased from the small font boutique in Nome, Alaska, won't do you any good on the Webunless each person viewing your site has also bought and installed that font. Otherwise, your visitors' Web browsers will show your text in a default font, which is usually some version of Times for body text, and Arial or Helvetica for headlines.
Tip: For one cutting edge method of using any font you'd like for headline text, there's a Flash-driven technique known as sIFR. Find out about it at http://wiki.novemberborn.net/sifr.
One solution's to specify the font you'd like to use, as well as a couple of back-up choices. If your viewer's computer has your first-choice font, then that's what she'll see. But when the first font isn't installed, the browser looks down the list until it finds a font that is. The idea is to specify a list of similar-looking fonts that are common to a variety of operating systems, like so: font-family: Arial, Helvetica, sans-serif;
In this example, a Web browser first looks to see if the Arial font's installed; if it is, then that font's used; if not, the browser next looks for Helvetica, and if that isn't installed, then it finally settles for a generic fontsans-serif. (For more information on common Mac and PC fonts, see Figure 6-2.) When you list a generic font type (like sans-serif or serif), the viewer's browser gets to choose the actual font. But at least you can define its basic character. Here are some commonly used combinations, including a generic font type at the end of each list: ● ● ●
Arial, Helvetica, sans-serif "Times New Roman", Times, serif "Courier New", Courier, monospace
Section 6.1. Formatting Text ● ● ● ● ● ● ●
Georgia, "Times New Roman", Times, serif Verdana, Arial, Helvetica, sans-serif Geneva, Arial, Helvetica, sans-serif Tahoma, "Lucida Grande", Arial, sans-serif "Lucida Console", Monaco, monospace "Marker Felt", "Comic Sans MS", fantasy "Century Gothic", "Gill Sans", Arial, sans-serif
Note: When a font name's more than a single word, it must be enclosed by quotes like this: "Times New Roman".
6.1.2. Adding Color to Text Black and white's great for Casablanca and Woody Allen films, but when it comes to text, a nice skyline blue looks much sharper and classier than drab black. Coloring your text with CSS is easy. In fact, you've used the color property in a few tutorials already. You have several different ways to define the exact color you want, but they all follow the same basic structure. You type color: followed by a color value: color: #3E8988;
In this example, the color value is a hexadecimal number indicating a muted shade of teal (more in a moment on what hexadecimal is). Every graphics program from Fireworks to Photoshop to GIMP lets you select a color using hexadecimal or RGB values. Also, the color pickers built into Windows and Macs let you use a color wheel or palette to select the perfect color and translate it into a hexadecimal or RGB value.
Tip: If your color design sense needs some help, you can find lots of attractive, coordinated collections of colors at www. colorschemer.com/schemes/.
6.1.2.1. Hexadecimal color notation The most common color system used by Web designers is hexadecimal notation. A color valuelike #6600FFactually contains 3 hexadecimal numbersin this example 66, 00, FFeach of which specify an amount of red, green, and blue, respectively. As in the RGB color system described next, the final color value is a blend of the amounts of red, green, and blue specified by these numbers.
Tip: You can shorten the hexadecimal numbers to just three characters if each set contains the same two numbers. For example, shorten #6600FF to #60F, or #FFFFFF to #FFF.
Figure 6-2. While Mac and Windows used to come with very different sets of
Section 6.1. Formatting Text
preinstalled fonts, there's been some convergence in the past few years. These days, you can count on the average Mac or PC to have the following fonts: Arial, Arial Black, Arial Narrow, Comic Sans MS, Courier New, Georgia, Times New Roman, Trebuchet MS, Verdana, and Webdings. If your audience includes Linux fans, then all bets are off, though Helvetica, Times, and Courier are safe bets. For a concise listing of fonts common to the two operating systems, check out www.ampsoft.net/webdesign-l/WindowsMacFonts.html.
Section 6.1. Formatting Text
6.1.2.2. RGB You can also use the RGB red, green, bluemethod familiar in computer graphics programs. The color value consists of three numbers representing either percentages (0100 percent) or numbers between 0255 for each hue (red, green and blue). So when you want to set the text color to white (perhaps to set it off from an ominous dark page background), you can use this: color: rgb(100%,100%,100%);
or color: rgb(255,255,255);
Tip: If all these numbers and digits have your head spinning, then you can always fall back on the classic HTML color keywords. (Just don't expect your site to win any awards for originality.) There are 17 colorsaqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange, purple, red, silver, teal, white, and yellow. In CSS, you add them to your style like so: color: fuchsia;.
Section 6.2. Changing Font Size
6.2. Changing Font Size Varying the size of text on a Web page is a great way to create visual interest and direct your visitors' attention to important areas of a page. Headlines in large type sizes capture your attention, while copyright notices displayed in small type subtly recede from prominence. The font-size property sets text size. It's always followed by a unit of measurement, like so: font-size: 1em;
The value and unit you specify for the font size (in this example, 1em) determine the size of the text. CSS offers a dizzying selection of sizing units: keywords, ems, exs, pixels, percentages, picas, points, even inches, centimeters and millimeters. Units of measurement commonly used with printed materialspicas, points, inches, and so ondon't work well on Web pages because you can't predict how they'll look from one monitor to the next. But you may have occasion to use points when creating style sheets for printer-friendly pages, as described in Chapter 13 (Section 13.1). Only a few of the measurement unitspixels, keywords, ems, and percentagesmake sense when you're sizing text for a computer monitor. The rest of this section explains how they work.
6.2.1. Using Pixels Varying Pixel values are the easiest to understand, since they're completely independent from any browser settings. When you specify, for example, a 36-pixel font size for an