In this tutorial, we’ll be learning about working with fonts in CSS!

The font property is a shorthand property which can combine a number of sub-properties in a single declaration. For example:

font: normal italic small-caps bold 16px/120% "Helvetica", sans-serif;

This is equivalent to:

font-stretch: normal;
font-style: italic; 
font-variant: small-caps; 
font-weight: bold; 
font-size: 16px; 
line-height: 120%; 
font-family: "Helvetica", sans-serif;

Let’s review each of these properties in detail!

CSS Font properties

font-family

The font-family property sets the font family that the element will use.

The selected font is not a single font face but one of a “family”, as a font is composed of a number of sub-fonts. Such as bold, italic, light, etc.

body {
    font-family: Helvetica;
}

The font family name matches either a font that is embedded on the page or available on the user’s system.

We could also choose multiple fonts like so:

body {
    font-family: Helvetica, Arial, sans-serif;
}

In this case, the browser will choose the next font, if the first cannot be used. This might happen if it’s either not found on the users local machine, or if the server that the font is hosted in is down.

Font types are typically classified as Serif, Sans-Serif, or Monospace fonts. Here’s some of most popular:

Serif: Georgia, Times New Roman

Sans-Serif: Arial, Helvetica, Verdana, Lucida Grande , Trebuchet MS

Monospace: Courier, Courier New, Lucida Console

line-height

The line-height property sets the amount of space above and below our element.

p {
    line-height: 1.5;
}

We can also use the keyword values normal,none as well as a number, length (any valid CSS unit), or percentage (being the elements’ font size multiplied by the %).

font-weight

The font-weight property sets the width (or thickness) of each of the characters of a font. You can use the following values:

  • normal
  • bold
  • bolder
  • lighter

Note that bolder & lighterare relative to the elements’ parent.

Numeric values can also be used:

  • 100
  • 200
  • 300
  • 400 (equivalent to normal)
  • 500
  • 600
  • 700 (equivalent to bold)
  • 800
  • 900

With 100 being the lightest font, and 900 the boldest.

For values other than 400 or 700 to have an effect, the font being used must have built-in faces that match these weights.

font-stretch

With font-stretch we can choose a narrow or wide face of the font. Assuming the font being used contains corresponding font faces.

The possible values are:

  • ultra-condensed
  • extra-condensed
  • condensed
  • semi-condensed
  • normal
  • semi-expanded
  • expanded
  • extra-expanded
  • ultra-expanded

font-style

The font-style property accepts one of three possible values: normal, italic, and oblique.

For example, to set our font to italic:

p {
  font-style: italic;
}

There is very little difference between using italic and oblique. Both apply a sloping effect to text.

font-size

The font-size property is used to determine the size of fonts. For example:

p {
  font-size: 20px;
}

You either use a valid length value (such as px, em, rem a percentage, etc), or a predefined value keyword.

The available keyword values are:

  • xx-small
  • x-small
  • small
  • medium
  • large
  • x-large
  • xx-large
  • smaller
  • larger

With both smaller & larger being relative to the parent element.

Note that font-size is a mandatory value. When using the font shorthand property, the size must be set (or it will be invalid)!

font-variant

The font-variant property is a bit of a relic. It was originally used to set text to small caps, it had 3 values:

  • normal
  • inherit
  • small-caps

Small caps sets the text to “smaller caps”, that is smaller than the regular uppercase letters.

Related posts:


Tim profile image

A little about me..

Hey, I’m Tim! 👋

I’m a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out my Complete Guide to Freelancing if you'd like to find out more.

While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.

Thanks for reading! 🎉