The font-family
property in CSS is used to specify a prioritized list of one or more font family names and/or generic family names for an element. The browser will go through the list of fonts from left to right and use the first font it can find on the system. This ensures that if a user's system does not have the primary font, a fallback font will be used instead.
Example 1: Using a Specific Font
/* This CSS rule targets all paragraph elements. */
p {
/* It sets the font for the paragraphs to "Arial". */
font-family: "Arial";
}
Explanation
This code sets the font for all <p>
elements to "Arial". If Arial is not available, the browser will use its default serif font.
Example 2: Font Family with a Fallback
/* This rule applies to all heading level 1 elements. */
h1 {
/* It specifies "Helvetica" as the preferred font. */
/* If Helvetica is not available, the browser will use any sans-serif font. */
font-family: "Helvetica", sans-serif;
}
Explanation
This CSS rule sets the font for all <h1>
elements to "Helvetica". If the user's system does not have Helvetica, a generic sans-serif font will be used as a fallback.
Example 3: Using a Web Safe Font Stack
/* This CSS rule targets the entire body of the HTML document. */
body {
/* This is a common font stack for maximum compatibility across browsers and operating systems. */
font-family: "Times New Roman", Times, serif;
}
Explanation
This code defines a font stack for the <body>
element. It first tries "Times New Roman", then "Times", and finally any available serif font.
Example 4: Incorporating a Custom Web Font
/* This rule applies to elements with the class "custom-text". */
.custom-text {
/* It sets a custom font named "MyCustomFont". */
/* A generic cursive font is provided as a fallback. */
font-family: "MyCustomFont", cursive;
}
Explanation
This example sets a custom web font named "MyCustomFont" for elements with the class "custom-text". You would need to import this font using @font-face
for it to work.
Example 5: Using a Monospaced Font for Code
/* This rule targets all <code> and <pre> elements. */
code, pre {
/* This font stack is ideal for displaying code, as all characters have the same width. */
font-family: "Courier New", Courier, monospace;
}
Explanation
This CSS rule ensures that all <code>
and <pre>
elements are displayed in a monospaced font, which is standard for presenting code.
Example 6: Prioritizing a Less Common Font
/* This rule applies to elements with the class "special-heading". */
.special-heading {
/* It prioritizes the "Garamond" font. */
/* A generic serif font is the fallback. */
font-family: "Garamond", serif;
}
Explanation
This code sets the font for elements with the class "special-heading" to "Garamond", a classic serif font. If Garamond is unavailable, a default serif font will be used.
Example 7: A Modern Sans-Serif Font Stack
/* This rule targets the root HTML element. */
html {
/* This modern font stack is popular for its clean and readable appearance. */
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
Explanation
This comprehensive font stack is designed to use the native system font of the user's operating system for a more integrated look and feel.