CSS Fonts
In this page:
selector {
font-family: "Font Name", fallback-family;
font-size: length;
font-weight: normal | bold | number;
font-style: normal | italic;
}
font-family and Fallback Fonts
font-family lists which typeface(s) to use, in order of preference. If the first choice isn't available on a visitor's device, the browser tries the next one in the list, ending with a generic fallback like sans-serif or serif.
- Always end a font-family list with a generic fallback, it guarantees text stays readable even if every named font fails to load.
- Accepted values:
- font-family — comma-separated list; quote names with spaces, e.g. "Open Sans"
- serif | sans-serif | monospace — the most common generic fallbacks
- cursive | fantasy — decorative generic families
- system-ui — the operating system's UI font
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: font-family and Fallback Fonts
<style>
p {
font-family: "Segoe UI", Arial, sans-serif;
}
</style>
<p>Falls back through the list if a font isn't available</p>
font-size and font-weight
font-size sets how large text renders, and font-weight sets how bold it appears, from thin (100) through normal (400) to bold (700) and beyond.
- Use rem units for font-size where possible, they scale consistently relative to the page's base font size and respect a visitor's browser settings.
- Accepted values:
- length — e.g. 16px, 1.25rem, 1.5em
- keyword — xx-small to xx-large (medium is the default)
- larger | smaller — relative to the parent's size
- percentage — relative to the parent's font size
- font-weight: normal (400), bold (700), or a number from 1 to 1000
- font-weight: lighter | bolder — relative to the parent
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: font-size and font-weight
<style>
p {
font-size: 20px;
font-weight: 700;
}
</style>
<p>Large, bold text</p>
font-style and font-variant
font-style toggles italics, most commonly normal or italic. font-variant offers extra typographic options, most notably small-caps, which renders lowercase letters as smaller uppercase letters.
- Use italics sparingly, for emphasis or citations, since overusing them can make text harder to scan quickly.
- Accepted values:
- normal — upright text (initial value)
- italic — the font's italic face
- oblique — slanted; may take an angle
- font-variant: normal | small-caps
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: font-style and font-variant
<style>
em {
font-style: italic;
}
.caps {
font-variant: small-caps;
}
</style>
<p><em>Italic emphasis</em></p>
<p class="caps">Small caps text</p>
Shorthand font Property
The font shorthand combines style, weight, size, and family into one declaration. Size and family are required, in that order, near the end of the value.
- The shorthand is handy once you're comfortable with the individual font properties, but the longhand versions are often clearer while learning.
- Accepted values:
- font-size — required, e.g. 16px
- font-family — required, comes last
- font-style / font-variant / font-weight — optional, before the size
- line-height — optional, after size and a slash, e.g. 16px/1.5
- system fonts — caption, menu, status-bar and others
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Shorthand font Property
<style>
p {
font: italic bold 18px "Segoe UI", sans-serif;
}
</style>
<p>Style, weight, size, and family in one line</p>
Google Fonts and Web Safe Fonts
'Web safe' fonts (like Arial, Georgia, and Verdana) are already installed on nearly every device, so they render reliably without any extra loading. Google Fonts is a free service that lets you load custom typefaces beyond that safe list.
Example: Google Fonts and Web Safe Fonts
<style>
.safe {
font-family: Arial, Georgia, Verdana, sans-serif;
}
</style>
<p class="safe">Web safe fonts render reliably with no extra loading</p>
- Specifying only one font in font-family, with no fallback if that font fails to load.
- Using a font-weight value the loaded font file doesn't actually support, which browsers may fake or ignore.
- Forgetting to link a Google Fonts stylesheet before referencing that font name in font-family.
- font-family, font-size, font-weight, and font-style are the core building blocks of text typography.
- Always list a generic fallback (like sans-serif) at the end of a font-family list.
- Google Fonts and other font services let you use custom typefaces beyond the standard web safe fonts.
Standard CSS font properties are supported by every modern browser; Google Fonts works anywhere a browser can load an external stylesheet.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: