CSS Font properties

In this tutorial we will look at how to use CSS font properties in an external style sheet. We will look at how to change the font-weight (eg. normal or bold), font-style (eg. normal or italic), font-size (eg. 12pt, 10px, 1em or 100%), font-family (eg. Arial or Tahoma), font-variant (eg. normal or small-caps), and the line-height. We will also look at how to add comments to our CSS.

There are two ways that we can change the values for different font properties:

  • On multiple lines
  • On one line

When we specify the font properties over multiple lines, it will look like this:

Font properties on individual lines in CSS file.
Font properties on individual lines in CSS file.

When we specify the font properties on one line it looks like this (note that there are required property values and some order to the values – watch the video to find out more):

Font property values on one line in a CSS file.
Font property values on one line in a CSS file.

Watch the video below and then scroll down to find out more about web-friendly fonts and to see the sample code.

The sample code below is for a CSS file with the font property values specified on multiple lines:

 font-family: Georgia;
 font-weight: bold;
 font-style: italic;
 font-size: 1em;

Here is the code for a CSS file with the font property values specified on one line – much neater, right? A line of comments has been added to show the order of the values.

 font: italic bold 1em Georgia; /* font-style, font-weight, font-size, font-family */

And here is the code for the HTML file that references the stylesheet.css file:

<!DOCTYPE html>
 <title>CSS font properties</title>
 <meta charset="utf-8"/>
 <link rel="stylesheet" href="stylesheet.css" type="text/css"/>
 <h1>This is a heading.</h1>
 <h2>This is a smaller heading</h2>
 <p>This is a paragraph</p>
 <p>This is a paragraph</p>

Font-size units:

It is important to have an understanding of the four different units for the font-size property. The four different units are:

  • em – the em is scalable and is used in web documents. An em is equal to the current font-size of the web document. For example, if the document size is 12pt, then 1em will be equal to 12pt. 2em would therefore be equal to 24pt. You can also use decimals eg. 1.5 em. Ems are popular on the web because they are mobile-friendly and are used in ‘responsive’ web design.
  • px – Pixels (px) are fixed-size units that are designed for media that will be read on a screen. 1 pixel is the equivalent of one dot on the computer screen. The downside of pixels is that they do not scale to fit mobile devices or screen readers for the visually impaired.
  • pt – Points (pt) are usually used for print media and are also fixed-size units that cannot scale to fit mobile devices. 1 pt = 1/72 of an inch.
  • % – Percent (%) unit is like the em unit as it is also scalable. Eg. the current font size of 12pt = 100%.

In summary, the em and % units are really what you want to use for the font-size property, especially if you wish to develop mobile-friendly or responsive websites.  Stay away from px and pt as they do not scale for mobile devices. Try getting used to using em and % instead. Find out more information on font-size units here.

And lastly, here is an overview of some of the web-safe fonts that you can include in CSS:

Serif fonts:

  • Georgia, serif
  • Palatino Linotype
  • Book Antiqua
  • Palatino, serif
  • Times New Roman

Sans-serif fonts:

  • Arial
  • Helvetica, sans-serif
  • Arial Black
  • Gadget sans-serif
  • Comic Sans MS
  • Impact
  • Charcoal
  • Lucida Sans Unicode
  • Tahoma
  • Geneva
  • Trebuchet MS
  • Verdana

Monospace fonts:

  • Courier New
  • Courier, monospace
  • Lucida Console
  • Monaco, monospace

Text shadows

Another effect you might like to use (not mentioned in the video) is a text shadow using the text-shadow property. The text-shadow property has four different attributes separated by spaces.

The first attribute is the horizontal shadow (how much the shadow is to the left or right of the text). A positive number (eg. 3px) will display a shadow to the right of the text and a negative number (eg. -3px) will display a shadow to the left.

The second attribute is the vertical shadow (how much the shadow is above or below the text – this can also be a positive or negative number).

The third attribute is the blur radius (how much blur you want for the shadow). And the last attribute is the colour for the shadow.

Here is some sample CSS code for a text-shadow with a horizontal shadow of 3px, vertical shadow of 3px, blur radius and 5 and the colour blue for the shadow.

text-shadow: 3px 3px 5px blue;

And this is what it would look like: