Tuesday 07th of September 2010 11:53:38 PM

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:

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.

By not predefining any tags in the XML Recommendation, the W3C allowed developers full control over customizing their data as they see fit. This makes XML very attractive to encoding data that already exists in legacy databases (by using database metadata, and other schema information). This extensibility of XML makes it such a great fit when trying to get different systems to work with each other.

XML supports shareable structure (using DTDs)

Since the structure of the XML document can be specified in DTDs they provide a simple way to make it easier to exchange XML documents that conform to a DTD. For example, if two software systems need to exchange information, then if both of the systems conform to one DTD, the two systems can process information from each other. DTDs are not as powerful as some kind of schema architecture for XML, they don't support typing, subclassing, or instantiation mechanisms that a schema architecture must have.

DTDs are a simple way to make sure that 2 or more XML documents are of the same "type". Its a very limited approach to making "typed" XML documents shareable across systems. In the future some kind of schema system will be proposed by the W3C that should allow typing, instantiation and inheritance of information (in XML).



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!


<P>This paragraph contains elements of increasing weight: there is an<SPAN>SPAN element which contains a <STRONG>strongly emphasizedelement, and that contains a <B>boldface element</B></STRONG></SPAN>.</P>
Figure 5-12

Figure 5-12. Moving up the weight scale

In the last two nested elements, the computed value offont-weight is increased because of the liberaluse of the keyword bolder. If we were to replacethe text in the paragraph with numbers representing the

<IMG SRC="foo.gif" STYLE="clip: rect(10px, auto, auto, 0);">

The auto values will set the clipping rectangle's bottom to align with the bottom of the image, and the right edge to the right edge of the image. The value of 0 for left keeps the left edge of the clipping rectangle against the left edge of the image, but the 10px for top moves the top edge of the clipping rectangle downward 10 pixels. This will cause the top 10 pixels of the image to become effectively invisible.