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:
font-family
can accept a list of fonts. If the first font is not available, the browser will try the next one in the list.font-family
values can be a specific font name (e.g., "Arial") or a generic font family (e.g.,serif
,sans-serif
,monospace
).- It's important to provide fallback font options. This ensures text remains legible even if the preferred font is not available.
- Font family names with spaces (e.g., "Times New Roman") must be enclosed in quotes.
- Generic font families like
serif
,sans-serif
,monospace
,cursive
, andfantasy
are useful for setting fallback options. - Fonts can be applied to all text within an element, and multiple elements can inherit the same font family from a parent.
- You can use the
@font-face
rule to define custom fonts that are not installed on the user's system. - Google Fonts is a popular resource for free, open-source web fonts that can be integrated into web pages using the
link
element.
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:
- serif: Fonts with small lines or decorations at the ends of characters, typically used for traditional print or formal designs (e.g., "Times New Roman").
- sans-serif: Fonts without the small lines at the ends of characters, used for clean, modern designs (e.g., "Arial", "Verdana").
- monospace: Fonts where each character takes up the same amount of space, commonly used for coding or technical content (e.g., "Courier New").
- cursive: A font that mimics handwriting with flowing, connected letters.
- fantasy: Fonts with decorative, artistic styles that are often used for fun or creative designs.
Custom Fonts Using @font-face:
- Use the
@font-face
rule to define custom fonts that are not available on the user's system. This allows you to embed fonts directly into your website. - Ensure you have the correct font file formats (e.g., .woff, .woff2, .ttf) for cross-browser compatibility.
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.