Thursday 11th of March 2010 02:18:10 AM

CSS Tutorials > Controlling Fonts with CSS

Controlling Fonts with CSS

In this tutorial we will look at how to control font properties using style sheets. This is a really useful feature of CSS because it means that you can avoid having all those <font> tags in your Web pages, and it allows you to easily control all your fonts simply by editing one style sheet file.

We'll look at the different font properties that can be used with CSS, and explain each property with the aid of some real-life examples. Each example is shown as it renders in your browser.

There are six properties that can be used to control fonts - font-family, font-style, font-variant, font-weight, font-size and font. Let's look at each of these in turn.

1. font-family

The font-family property is used to set the font face used for the text (e.g. Times or Arial). The allowed values are:

Value

Example

family-name

{ font-family: Times }

generic-family

{ font-family: serif }

You can specify one or more family names or generic families for the font.

Family names include names such as times, arial, gill and helvetica.

Generic families include the following:

  • serif (e.g. Times)
  • sans-serif (e.g. Helvetica)
  • cursive (e.g. Zapf-Chancery)
  • fantasy (e.g. Western)
  • monospace (e.g. Courier)

It's always a good idea to include at least one generic family, in case the browser can't find a specific font (e.g. not all computers will have Zapf-Chancery installed).

Examples:

p { font-family: "times new roman", serif }

`Take some more tea,' the March Hare said to Alice, very earnestly.

p { font-family: helvetica, arial, sans-serif }

`Take some more tea,' the March Hare said to Alice, very earnestly.



2. font-style

This property controls whether the font is slanted (italicised) or not. The options are:

Value

Example

normal

{ font-style: normal }

italic

{ font-style: italic }

oblique

{ font-style: oblique }

This allows you to set the font style to normal (upright), or italic / oblique (slanted). italic and oblique are usually the same thing on most browsers.

Examples:

p { font-style: oblique }

`Take some more tea,' the March Hare said to Alice, very earnestly.

p { font-style: normal }

`Take some more tea,' the March Hare said to Alice, very earnestly.



3. font-variant

Allows you to choose between normal and small-caps lettering. Small-caps are capital letters used in place of lowercase ones. The original capitals are shown as slightly bigger capitals.

The options are:

Value

Example

normal

{ font-variant: normal }

small-caps

{ font-variant: small-caps }

Examples:

p { font-variant: small-caps }

`Take some more tea,' the March Hare said to Alice, very earnestly.

p { font-variant: normal }

`Take some more tea,' the March Hare said to Alice, very earnestly.



4. font-weight

This controls whether the font is normal weight, bold or light. The options for font-weight values are:

Value

Example

normal

{ font-weight: normal }

bold

{ font-weight: bold }

bolder

{ font-weight: bolder }

lighter

{ font-weight: lighter }

100, 200, ... 900

{ font-weight: 700 }

bolder and lighter will make the font one step bolder or lighter than the default or inherited weight.

The numbers 100 - 900 are a numerical representation for weight, where 100 is the lightest and 900 is the heaviest (boldest). Usually, normal is represented by 400, and bold by 700.

Examples:

p { font-weight: bold }

`Take some more tea,' the March Hare said to Alice, very earnestly.

p { font-weight: 200 }

`Take some more tea,' the March Hare said to Alice, very earnestly.



5. font-size

The font-size value specifies the size of the font used to display the text. There are four types of values that can be used for font-size:

The background color of just about any element can be set, fromBODY all the way down to inline elements such asEM and A. Even form elementsshould be affected by the property, although not all user agents cando this correctly. Also,background-color is not inherited. Its defaultvalue istransparent,

Value

Example

absolute-size

{ font-size: large }

relative-size

{ font-size: smaller }

length

{ font-size: 12pt }

percentage

{ font-size: 150% }

absolute-size values are words specifying definite font sizes. Possible values are: xx-small, x-small, small, medium, large, x-large and xx-large . Although the exact font size represented by, say, large, may vary from browser to browser, you can be sure that large will always be bigger than medium, for example.

relative-size values are relative to the inherited or default font size. Possible values are: larger and smaller. For example, if the inherited font size (from a parent style) is large, a relative-size of larger will set the font size to x-large.

length values are specified using the CSS length units such as em, px, cm and pt. For a full description of these, see the tutorial CSS Units.

percentage values are relative to the parent element's font size. For example, if the inherited font size is 12pt and a percentage value of 150% is specified, the resulting font size will be 18pt. See the tutorial CSS Units for details.

Examples:

p { font-size: 10pt }

`Take some more tea,' the March Hare said to Alice, very earnestly.

p { font-size: larger }

`Take some more tea,' the March Hare said to Alice, very earnestly.

p { font-size: 10px }

`Take some more tea,' the March Hare said to Alice, very earnestly.

p { font-size: 150% }

`Take some more tea,' the March Hare said to Alice, very earnestly.



6. font

The final property is font, which can be used as a shorthand for setting all the previous properties in one line, plus the line-height text property. The following values may be used. Note that the values should be used in the order shown; for example, font-size should come after font-weight.

Value

Example

font-style

{ font: italic }

font-variant

{ font: small-caps }

font-weight

{ font: bold }

font-size
[ / line-height (optional) ]

{ font: 12pt }
{ font: 120%/150% }

font-family

{ font: Times }

Any values not supplied are set to their default values.

Examples:

h3 { font: bold italic 14pt Arial, sans-serif }

`Take some more tea,' the March Hare said to Alice, very earnestly.

body { font: normal small-caps 120%/200% Times }

The Cat only grinned when it saw Alice. It looked good- natured, she thought: still it had VERY long claws and a great many teeth, so she felt that it ought to be treated with respect.



The End

That's the end of this tutorial. We hope you found it useful. If you're still stuck and would like further help, check out our online Help Forums, where you can get assistance from members of my and other webmasters.

If you would like to offer us feedback on this or any of the tutorials, please contact us. Have fun!


6.2.4. Repeats with Direction (Revisited)

In the previous section on repetition, we explored the values repeat-x , repeat-y, and repeat, and how they affect the tiling of background images. In each case, however, the tiling pattern always started from the top left corner of the containing element (e.g., BODY). That isn't a requirement, of course; paragraph is 23pt. However, a <BIG>larger element</BIG> within the paragraph does not cause the value of 'line-height' to change, which can lead to some interesting effects.</P>

The result shown in Figure 4-26 may look like a browser bug, but it isn't. It's exactly how the example markup should be displayed.

Figure 4-26

Figure 4-26. Possible behavior with the line-height property and inline elements of different sizes

This is by no means the only oddity which arises from using line-height. As backwards as it may seem, in Figure 4-27, the value of line-height block-level element (either directly, or by implication, as with floated elements) is an inline element. Setting box properties for inline elements gets into even more interesting territory than we've already covered. Some good examples of inline elements are the EM tag and the A tag, both of which are nonreplaced elements, and images, which are replaced elements.