TExcelTAD
Home › CSS › CSS Fonts

CSS Fonts

Choosing and sizing fonts has a big impact on how readable and professional a page feels. CSS gives you properties for the font family, size, weight, style and more, plus a font shorthand that sets them all at once.

Font families

font-family takes a comma-separated list of fonts. The browser uses the first one that is available on the device, so always end with a generic family as a fallback.

Generic family Look Examples
serif Small strokes at letter ends Georgia, Times New Roman
sans-serif Clean, no strokes Arial, Helvetica, Verdana
monospace Every character the same width Courier New, Consolas
cursive Handwriting style Comic Sans MS, Brush Script
fantasy Decorative Impact, Papyrus
system-ui The operating system's UI font San Francisco, Segoe UI, Roboto

Font names containing spaces must be quoted: "Times New Roman".

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-size: 20px; }
  p { margin: 6px 0; }
  .serif { font-family: Georgia, "Times New Roman", serif; }
  .sans { font-family: Verdana, Arial, sans-serif; }
  .mono { font-family: "Courier New", Consolas, monospace; }
  .cursive { font-family: "Comic Sans MS", cursive; }
  .fantasy { font-family: Impact, fantasy; }
  .sys { font-family: system-ui, sans-serif; }
</style>
</head>
<body>
  <p class="serif">Serif — classic and bookish</p>
  <p class="sans">Sans-serif — clean and modern</p>
  <p class="mono">Monospace — code_like(0)</p>
  <p class="cursive">Cursive — playful handwriting</p>
  <p class="fantasy">Fantasy — loud and decorative</p>
  <p class="sys">system-ui — looks native</p>
</body>
</html>

Tip: A "font stack" like system-ui, -apple-system, "Segoe UI", Roboto, sans-serif gives fast, native-looking text on every platform without downloading anything.

Font size

font-size accepts absolute and relative units:

Unit Relative to
px Fixed pixels
em The parent element's font size
rem The root (<html>) font size, usually 16px
% The parent's font size
vw 1% of the viewport width
keywords small, medium, large, x-large…

Prefer rem for most sizes: it respects the user's browser font-size setting and doesn't compound the way em does when elements are nested.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .em-parent { font-size: 1.3em; color: #c2185b; }
  .rem-parent { font-size: 1.3rem; color: #1565c0; }
</style>
</head>
<body>
  <div class="em-parent">1.3em
    <div class="em-parent">1.3em inside
      <div class="em-parent">and again — it keeps growing!</div>
    </div>
  </div>
  <div class="rem-parent">1.3rem
    <div class="rem-parent">1.3rem inside
      <div class="rem-parent">stays the same size</div>
    </div>
  </div>
</body>
</html>

Weight, style and variant

Property Common values
font-weight normal (400), bold (700), or numbers 100–900
font-style normal, italic, oblique
font-variant normal, small-caps
line-height Space per line, e.g. 1.5

Not every font ships every weight. If the requested weight is missing, the browser picks the nearest one or synthesises a fake bold.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; font-size: 22px; }
  p { margin: 4px 0; }
  .w3 { font-weight: 300; }
  .w4 { font-weight: 400; }
  .w7 { font-weight: 700; }
  .w9 { font-weight: 900; }
  .it { font-style: italic; }
  .sc { font-variant: small-caps; }
</style>
</head>
<body>
  <p class="w3">Weight 300 — light</p>
  <p class="w4">Weight 400 — normal</p>
  <p class="w7">Weight 700 — bold</p>
  <p class="w9">Weight 900 — black</p>
  <p class="it">Italic style</p>
  <p class="sc">Small Caps Variant</p>
</body>
</html>

The font shorthand

font sets style, variant, weight, size, line-height and family in one declaration. Size and family are required and must come last, in that order; size/line-height share a slash.

css
/* style weight size/line-height family */
font: italic 700 18px/1.4 Georgia, serif;
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  .quote { font: italic 300 22px/1.5 Georgia, serif; color: #444; border-left: 5px solid #ff7043; padding-left: 14px; }
  .label { font: bold 13px/1 system-ui, sans-serif; text-transform: uppercase; letter-spacing: 2px; color: #ff7043; }
</style>
</head>
<body>
  <p class="label">Quote of the day</p>
  <p class="quote">Good typography is invisible; bad typography is everywhere.</p>
</body>
</html>

Note: The shorthand resets any sub-property you leave out to its initial value. Writing font: 18px Arial after font-weight: bold makes the text normal weight again.

Test yourself

Why should a font-family list end with a generic family like sans-serif?

  1. It is required syntax
  2. It is a fallback if none of the named fonts are available
  3. It makes the text bold
  4. It loads a web font

Which unit is relative to the root element's font size?

  1. em
  2. %
  3. rem
  4. px

Which font shorthand is valid?

  1. font: Arial 16px;
  2. font: bold;
  3. font: bold 16px/1.5 Arial, sans-serif;
  4. font: 16px bold Arial;