Pin Me

How to Declare Fonts and Font Styles With CSS

written by: Bruno Kos•edited by: Amy Carson•updated: 2/1/2011

If you're defining fonts in CSS with some WYSIWYG editor, you might not understand how CSS typography actually works. The purpose of this article is to introduce you into this important CSS topic.

  • slide 1 of 7

    Introduction

    Typography, as a very important part of a website’s design, is well supported by CSS2. More specifically, all important font properties (font family, font size, font weight, etc) are supported and included in this version of CSS. This article deals with defining fonts in CSS; more specifically, it covers font-family, font-size, font-weight, font-style, font-variant and font CSS properties.

  • slide 2 of 7

    Font-family CSS Property

    The purpose of font-family CSS property is to set font family names, ranked by priority. This property is responsible for the way some piece of text is displayed. When rendering a text, web browser will use the first font from the list. Since there is a possibility that the browser will not support any of the predefined fonts, a generic family name should be included as well, and should have the lowest priority. Since a generic font will always be rendered, it is pointless to include any other font after the generic one.

    How to declare a font family in CSS? An example follows:

    p {

    font-family: Arial, Helvetica, sans-serif;

    }

    In this example, there are three fonts defined. Which one will the web browser choose? Once a web browser loads text which exists between <p> and </p>, it will try to use an Arial font to render it. If an Arial is not available, it will try to load Helvetica. If this one is not available as well, the browser will finally use its default sans-serif font. While declaring fonts, it is important to remember that the font family names are separated with a coma sign.

  • slide 3 of 7

    Font-size CSS Property

    Font-size CSS property specifies the font size of a text which it is applied to. This value does not accept only numerical and absolute values, but it can accept percentages as well. Let's take a closer look of these three possibilities. First, an example of numerical value follows:

    p {

    font-size: 10px;

    }

    If this CSS code is used, a web browser will render text wrapped with <p> element at 10px.

    Furthermore, an example of an absolute value follows:

    p {

    font-size: large;

    }

    In other words, font size is defined with some of the available keywords (xx-small, x-small, small, medium, etc). The sizes of these values are not defined, but small should be bigger than x–small, medium should be bigger than small, etc).

    Finally, a percentage value can be also used, such as in the following example:

    p {

    font-size: 70%;

    }

    If this property is set, the size of the text wrapped with <p> element will be 80% of the parent element’s font size.

  • slide 4 of 7

    Font-weight CSS Property

    Font-weight CSS property specifies the weight of a text which it is applied to. This property accepts both numeric values and keywords (such as bold, bolder, normal, inherit, etc). Examples for both possibilities follow:

    p {

    font-weight: 400;

    }

    And:

    p {

    font-weight: bold;

    }

    Although this property can accept various numeric values, most fonts are not available in these weights. Instead, they can be rendered only with 400 (normal) and 700 (bold) weights. Some numeric values also match particular keywords. For example, bold equals 700, while normal equals 400.

  • slide 5 of 7

    Font-style CSS Property

    Font-style CSS property specifies the style of a text which it is applied to. This CSS property accepts four values: italic, normal, oblique and inherit. An example follows:

    p {

    font-style: italic;

    }

  • slide 6 of 7

    Font-variant CSS Property

    Font-variant CSS property specifies the variant of a text which it is applied to. This CSS property accepts three values: normal, small-caps and inherit. Small-caps are defined as fonts in which lowercase letters are rendered as smaller versions of their corresponding uppercase letters. An example follows:

    p {

    font-variant: small-caps;

    }

  • slide 7 of 7

    Font CSS Property

    Finally, defining fonts in CSS can be achieved with a comprehensive Font CSS property. This property can accept the values of several other CSS properties, such as the font family, font variant, font weight, etc. When declaring these values, a declaration order should be respected according the following syntax:

    font: { [font-style] [font-variant] [font-weight] font-size [ /line-height ] font-family | caption | icon | menu | message-box | small-caption | status-bar | inherit } ;

    An example follows:

    p {

    font: bold small-caps 0.6em/1.4 Helvetica, Arial, sans-serif;

    }

    Font size and font family values are mandatory. If some value from the list (syntax) is not specified, it will be set to an initial value, rather than its inherited value.