CSS Cascading Style Sheet: Using Fonts In CSSFonts in Stylesheet Table of Contents:
Welcome to our tutorial on the wondrous language known as cascading stylesheets. After the basics of how to use and add stylesheets to Web pages, we can now begin exploring the individual properties that make them more than cool. Lesson 2 is devoted to fonts: calling them by name, controlling text size, specifying all manner of bolds and italics, and adding special effects. Do you think you can do all these things with existing HTML tags? Well, you can\'t. The CSS properties covered in this turorials include:
First thing\'s first: How do you tell the Web browser which font to display? You just type the name of the font after font-family, right? Unfortunately, it\'s a little more complicated than that. Fonts are not always called the same thing across platforms. For example, Courier on a Mac is often called Courier New on a Windows machine. And whereas one machine might have an Italic variant of a font, another might call it Oblique. The more you look, the more inconsistencies you\'ll find. On top of that, our name for a font isn\'t necessarily the same as the computer\'s name for the font, and you have to make sure you\'re using the computer\'s name. For example, Brush Script is actually Brush Script MT. How do you find out what the computer\'s name for the font is? It depends on the platform:
font-family is the CSS property used to call a font by name. The basic syntax looks like this:
Here\'s how your browser would interpret the above code: CSS font control is peachy.You can put as many font names in your list as you want. This feature can be helpful if you\'re not exactly sure how a font is spelled on a different platform: Go ahead and list both spellings. Note that browsers seem to prefer all-lowercase spellings for font names, although sometimes they will recognize capitalized names. As long as you test everything thoroughly, you should be fine (that seems to be the CSS mantra). It\'s a good idea to always use a generic font name as the last on your list. Your options:
More font name tips:
Next up are more ways to size text than you could possibly imagine. Controlling Text Size Are you frustrated by the fact that you can only size text to seven different sizes using ? Then you\'re gonna love this page. font-size Using the font-size property, you have magical control over the size of text with an infinite number of font sizes at your disposal. Three basic ways to specify the size of text are:
Points, Ems, Pixels, and Other Units Stylesheets recognize many different kinds of units that you can use to specify the size of an element. Let\'s look at each one in turn. Points:
text at a size of 16 points. Point size is a unit familiar to print designers and refers to an imaginary box that extends from the bottom of a descender (like "p") to the top of an ascender (like "d"). Points are an excellent unit to specify text size with because they work well across browsers and platforms. The only thing you should be aware of is that, by default, fonts appear larger on PC monitors than they do on Mac monitors. (If this is a big problem, you can always use JavaScript to detect which platform a person is using and then link to a different CSS file depending on the platform. Check here for details about platform detection.) Points, like all other units, work as small or as big as you want (that was 8 points and 80 points, respectively). Em:
B { font-size: 1.5em } would be 30 points. (The text is one and a half times that of its parent.) The em is also an excellent unit for text size, even though IE 3 doesn\'t support it. When you use em, you can be sure that users who need to can still adjust the type size using their browser preferences (which is important for those with less-than-perfect eyesight). Also, pages that are printed will have appropriate text size. Pixels:
There is a price, however. When you use pixels, your Web pages will not print consistently. Sometimes they won\'t print at all, and sometimes they\'ll print with ultra-tiny text. Also, in some browser versions, users won\'t be able to adjust the font size using the browsers\' preferences. Not good. The em is the more flexible unit. Other units: If the previous three don\'t give you what you want, try one of these units:
Keywords: If you don\'t like using those units, you can also declare text size through keywords, like so:
You can also use two relative keywords:
(Note: IE 3 doesn\'t support the smaller or larger attributes.) Percentage Values: A third way to specify text size is through percentage values. Here\'s an example:
B { font-size: 300% } three times as large or 45 points. Percentage values are always based on some inherited value from a parent element. The browsers are a little buggy with percentage values, so test often. Isn\'t choice delightful?! We finally have the freedom to play all we want with text size, thanks to the font-size property. The range at our disposal is wondrous, as you can see below. (Each letter "i" is 5 points bigger than the one before it; we start at 5 pt and end at 100 pt.) i i i i i i i i i i i i i i i i i i i i Try doing that with HTML! And we get some of the same flexibility with bold and italics. Check it out. Special Effects with TextWelcome to the miscellaneous area of our font discussion. Here you\'ll find other font-related stylesheets properties that are perfect to add to your toolbox. font-variant font-variant is a simple one: You use it to display normal text as small caps.
text-transform This property is somewhat related, and it actually works! With text-transform, you can easily control capitalization. Here\'s the basic code:
Here are all the possible values and what they mean: text-decoration For decades (well, it feels like that long anyway), we\'ve had no control over the fact that text links are underlined in Web browsers. I don\'t know about you, but I think it\'s ugly and annoying. Ladies and gentlemen, I give you a solution: text-decoration. First thing\'s first. Here\'s the basic syntax for this property:
The none value is magical. With it, you can remove the line under text links. Here\'s how:
A:active { text-decoration: none } A:visited { text-decoration: none } And here\'s the result: But wait, there\'s more! Not only can you remove underlining from links, you can also use any other CSS property on them using A:link, A:active, and A:visited. So, you can make unvisited links 12-point bolded Arial and visited links 10-point italicized Times. The world is your oyster. OK, OK, not all browsers support this heavenly feature set. Communicator supports these predefined classes with text-decoration but is very buggy with other properties. IE 3 doesn\'t support A:active (and the Mac version doesn\'t support A:visited). IE 4 and 5 do the best job, supporting just about everything. Despite these limitations, you have many opportunities that were never before possible! All hail the makers of CSS. Do you think you\'ve mastered the world of CSS and fonts? How about a quick exercise? |