CSS Font Family

The CSS font-family property is used to specify the font style for text within an element. It defines which font or group of fonts to use when rendering text. Font families can be categorized into generic families (serif, sans-serif, etc.) or specific font families (e.g., Arial, Times New Roman).

Key Points on CSS Font Family:

Syntax for CSS Font Family:

Syntax Example

/* Font family syntax */
        .element {
            font-family: font1, font2, generic-family;
                 }
        
        /* Example */
        p {
            font-family: 'Arial', sans-serif;
        }

Examples of CSS Font Family:

The following examples demonstrate how to use the font-family property with different font options:

Code Example: Font Family with Specific Fonts


            p {
            font-family: 'Times New Roman', serif;
              }

Output

This paragraph uses 'Times New Roman', and falls back to a serif font if unavailable.

Code Example: Font Family with Generic Font Family


            p {
            font-family: Arial, sans-serif;
              }

Output

This paragraph uses 'Arial', and falls back to a generic sans-serif font if 'Arial' is unavailable.

Code Example: Font Family with Multiple Fallbacks


            p {
            font-family: 'Helvetica Neue', Helvetica, sans-serif;
              }

Output

This paragraph uses 'Helvetica Neue', and falls back to 'Helvetica' or a generic sans-serif font if unavailable.

Code Example: Font Family with Cursive


            p {
            font-family: cursive;
              }

Output

This paragraph uses a cursive font style, falling back to the system's default cursive font if unavailable.

Common Font Family Values:

Custom Fonts Using @font-face:

When using custom fonts or web-safe font families, always consider web accessibility and performance. For consistency across devices, it’s best to define multiple font family options and ensure the content remains readable if the preferred font is unavailable.