Thursday, September 3, 2009

Create better Web sites with CSS and the Font Control property

Takeaway: Explore the CSS font property, which provides an alternative to the older font element and is a great tool for when you need to centralize control over the typeface, color, size and spacing of your HTML pages.

This article is also available as a TechRepublic download, which includes the example code listings in a more manageable text file format.

In times past, altering the typeface of a Web page usually meant enclosing the relevant text in a <font> tag and applying attributes to control its color, size and style. Today, this method is frowned upon, because it mixes visual styling aspects with actual content markup. The recommended method these days is to extract typeface styling information into a separate file, called a Cascading Style Sheet (CSS).

Putting font information into a CSS file has a number of important advantages: it's easy to do, doesn't require any special software, and works uniformly on most major browsers. More importantly, because information is centralized in a single location, altering the visual appearance of your pages is a snap: simply alter the font or color in your primary style sheet and the change will "cascade" across your site instantly.

Sounds interesting? Then keep reading, because this article explores the CSS font property, which provides an alternative to the older <font> element and is a great tool for when you need to centralize control over the typeface, color, size and spacing of your HTML pages.

Controlling typeface

The font-family property defines the font(s) to use for the corresponding element. This typically contains a list of font names, separated by commas; quotes can be used to enclose those names which contain whitespace. The browser will use the first font it finds in the list, or default to the standard browser typeface if none are available.

Listing A is an example of this directive in action:

Listing A

<html>
<head>
    <style type="text/css">
    .quote {
        font-family: "Bookman Old Style", "Verdana";
    }
    </style>
</head>

<body>
    <div class="quote">To be or not to be, that is the question.</div>
</body>
</html>

And Figure A shows you what the output looks like.

Figure A

Font_family

It's important to remember that this directive depends heavily on which fonts are present on the client system; in the example above, if neither Bookman Old Style nor Verdana are present, the default browser font will be used instead. To avoid this, it's a good idea to follow up the list of specific font names with the name of a generic font family, such as serif, sans-serif or cursive; this tells the browser to use the closest match within that category of fonts. Listing B is a revised version of the previous example, which does just that.

Listing B

<html>
<head>
    <style type="text/css">
    .quote {
        font-family: "Bookman Old Style", "Verdana", "cursive";
    }
    </style>
</head>

<body>
    <div class="quote">To be or not to be, that is the question.</div>
</body>
</html>

Controlling color

Font color is controlled using the color property, which accepts either an RGB value in hexadecimal notation or a "color word" such as red, silver or blue. Listing C shows you an example.

Listing C

<html>
<head>
    <style type="text/css">
    .quote {
        font-family: "Bookman Old Style", "Verdana";
        color: purple;
    }
    </style>
</head>

<body>
    <div class="quote">To be or not to be, that is the question.</div>
</body>
</html>

And Figure B shows you what it looks like.

Figure B

Color

Controlling size

Next up, size. Font size is controlled via the font-size property, which accepts either absolute (numerical units or keywords such as xx-small, small, medium, large, x-large) or relative (larger and smaller) values. Size units may be described in terms of points, percentages or ems. Listing D is an example; it magnifies the font to 400% of its normal size:

Listing D

<html>
<head>
    <style type="text/css">
    .quote {
        font-family: "Bookman Old Style", "Verdana";
        color: purple;
        font-size: 400%;
    }
    </style>
</head>

<body>
    <div class="quote">To be or not to be, that is the question.</div>
</body>
</html>

And Figure C shows you what it looks like.

Figure C

Size

Controlling emphasis

Font emphasis is controlled via the font-style and font-weight properties, which broadly correspond to HTML's<i> and <b> elements. The font-style property accepts any of the values normal, italic and oblique, while the font-weight property can either accept values on a numerical "boldness" scale between 100 and 900, or the keywords normal, bold, bolder and lighter. Listing E is an example that demonstrates both these properties.

Listing E

<html>
<head>
    <style type="text/css">
    .quote {
        font-family: "Bookman Old Style", "Verdana";
        color: purple;
        font-weight: bolder;
    }
   
    .attribution {
        font-style: italic;
    }
    </style>
</head>

<body>
    <div class="quote">To be or not to be, that is the question.</div>
    <div class="attribution">-- Hamlet</div>
</body>
</html>

And Figure D what it looks like:

Figure D

Font weight and style

Controlling word, character and line spacing

CSS also makes it possible to control the space between words, characters and lines, via the corresponding word-spacing, character-spacing and line-height properties. As with the font-size property, values may be expressed in points, pixels and ems. Listing F demonstrates with an example.

Listing F

<html>
<head>
    <style type="text/css">
    .quote {
        font-family: "Bookman Old Style", "Verdana";
        color: purple;
        font-weight: bolder;
        font-size: 200%;
        line-height: 50px;
    }
   
    .attribution {
        font-style: italic;
        word-spacing: 15pt;
        letter-spacing: 8px;
    }
    </style>
</head>

<body>
    <div class="quote">To be or not to be, that is the question.</div>
    <div class="attribution">-- Hamlet, Act 3, Scene 1</div>
</body>
</html>

Figure E shows you the output:

Figure E

Spacing

Of course, these examples are just the tip of the iceberg when it comes to working with fonts in CSS. However, they should have given you some insight into how these properties work in practice, and you should now know enough to begin experimenting on your own. So what are you waiting for?

How do I check how different fonts and styles look without recoding?

Author: Jack Wallen

One of the issues that can plague Web designers (not to be confused with Web developers) is that of font choice. Choosing the proper font and font style for your Web site can make or break a site's overall design. And I am sure you've seen it — a great site with fantastic elements, images, color schemes, style, and content that is rendered either ugly or unreadable simply because of a poor font choice.

The problem for designers, which quickly trickles down to developers, is that font choice can be dictated by browser choice, browser release choice, and CSS usage. These layers add up to create a fairly complex problem that is often solved by making a choice, seeing if it works, and then tweaking the CSS file over and over until the choice is perfect (or as close to perfect as you can get). This tweaking can take some time, unless you employ an amazingly helpful tool like FontFriend.

FontFriend is a Web-browser bookmarklet that will make your design work so much easier that you will wonder what you ever did without it. With this tool you can simply open up your Web site, click on FontFriend, change the font and font styles as FontFriend makes the changes in real time, and then, once you have found the right font and font styles, apply those changes to your CSS files as needed.

Now, before I get into how to use this tool (it's quite simple actually), I will warn you that the larger and more complex the site, the slower FontFriend will be. If the site is too large and too complex FontFriend could take your browser down. Fear not, though, even if FontFriend takes down your browser, it will not make any changes that are permanent or damaging. So, in the event of a crash, you will only need to fire your browser back up.

This blog post is also available in PDF format in a free TechRepublic download.

Installing FontFriend

If you have ever installed a bookmarklet you will not have any problems installing FontFriend. You don't actually "install" FontFriend. What you do is click on the bookmarklet link (from the FontFriend site) and drag it to your Bookmark Toolbar. Now this bookmarklet can be used in Firefox 3/3.5b and Safari 3 and 4. FontFriend does not support Internet Explorer, and, as the site says, "any compatibility with IE is mostly accidental and, frankly, surprising."

NOTE: With this "installation" don't mistake your regular tool bar with the bookmarks tool bar. You cannot install this bookmarklet anywhere but on a bookmark toolbar.

Once you have FrontFriend on your bookmark tool bar it will show up on your tool bar simply as FontFriend. You can change this name if you like by opening up the Organize Bookmarks, clicking on the FontFriend bookmarklet entry (it will be located in the Bookmarks Toolbar folder) and making the changes (Figure A).

Make sure, however, that you do not make any changes to the location. If you edit the location of FontFriend you will most likely break it. The only fields you should concern yourself with are Name, Keyword, and Description.

Figure A

The location is the "code" that actually makes FontFriend work.

Using FontFriend

When you navigate to your site, you need to click on the FontFriend bookmark in your Bookmark tool bar. When you do this FontFriend will open an overlay on your page at the bottom left, as you can see in Figure B.

Figure B

You can minimize this overlay by clicking the "F" in the upper-left corner.

Once this overlay is open, you can click on different font elements and the changes will update on the page in real time. As mentioned earlier, the more complex the page, the longer it will take for the change to take effect.

Let's take a look at a changed page. First I will show you the FontFriend page with the default fonts (Figure C).

Figure C

Here is the default font for the FontFriend page.

Now let's take a look at the same page with a different font and font style. Figure D shows FontFriend laying out the same site with a bad font.

Figure D

This font is Cambria in small caps.

This time around we'll see the same site re-rendered with a font that makes the page much nicer (Figure E).

Figure E

This time the page is rendered with the Tahoma font.

Remember, you can do more than just change the font on the page. You can also set the text to all Small Caps, Initial Caps, Bold, Normal, Upper Case, Lower Case as well you can edit body text, paragraph text, header text and even roll your own. If you decide to roll your own, make sure you take a look at the jQuery JavaScript Library link within the FontFriend overlay so you can see the types of tags you can use.

Resetting to default

Resetting the page back to the default style is simple. At the bottom right of the overlay you will find a very faint "S." If you click that "S," the site will return to its default styles.

Moving the overlay

There are times that you will want to see the text under the overlay. You can do this by moving the overlay around with the four arrows. When you click one of the arrows, the overlay will move that corner of the Web page being viewed. If the overlay is in the bottom right and you click the up arrow, the overlay will move to the upper-right corner. Click the left arrow, and the overlay will move to the upper-left corner, and so on.

Final thoughts

FontFriend is one of those tools that will make a designer's job easier. You will find that FontFriend takes some of the tedium and guesswork out of finalizing your CSS for the sites you are designing/developing.

Great Persons-Great Sayings...

 

For Nice, Quotes, Fun SMS and Shayaris Visit SMS Messages
 
 
 
 
 
 
 
 
 
        
 

Colors of the Earth


 
Play 10,000 + Free Games Here :  Games
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   
 
 

Wooden Scale Models


 
Play 10,000 + Free Games Here :  Games
 
 
 
 
 
 
 
 
 
   
 
 
.

__,_._,___


 

ITWORLD
If you have any question then you put your question as comments.

Put your suggestions as comments