Reading history

CSS Web Typography

6 min read 0 views

Learn web typography in CSS: font stacks, @font-face, line-height, line length, fluid headings with clamp() and Bangla fonts.

Most of the web is text, so good typography makes the biggest difference to how a site feels. The right fonts, comfortable line spacing, sensible line lengths and headings that scale with the screen turn a page from tiring to pleasant to read. This lesson covers font stacks, loading your own fonts with @font-face, the spacing properties that matter most, fluid heading sizes, and fonts for Bangla text.

In this lesson you will learn

  • Font stacks and generic font families
  • Loading custom fonts with @font-face and font-display
  • line-height, letter-spacing and line length
  • Fluid headings with clamp() and text-wrap: balance
  • Choosing fonts for Bangla

You should know colours, units and fonts and clamp().

Font Stacks and Spacing

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Font stacks and readable text</title>
  <style>
    body { margin: 12px; font-size: 14px; }
    .sans  { font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
    .serif { font-family: Georgia, "Times New Roman", serif; }
    .mono  { font-family: ui-monospace, Consolas, "Courier New", monospace; }
    p { margin: 4px 0; }
    .tight { line-height: 1.1; max-width: 360px; background: #fff3e0; }
    .comfy { line-height: 1.6; max-width: 360px; background: #e8f5e9; }
    .caps  { text-transform: uppercase; letter-spacing: 0.12em; font-size: 12px; color: #555; }
  </style>
</head>
<body>
  <p class="sans">system-ui: the operating system's own interface font</p>
  <p class="serif">Georgia, serif: a classic book-style font</p>
  <p class="mono">monospace: every letter is the same width</p>
  <p class="caps sans">Uppercase label with letter-spacing</p>
  <p class="tight sans">line-height 1.1: lines of a long paragraph are squeezed together, so the eye easily jumps to the wrong line while reading.</p>
  <p class="comfy sans">line-height 1.6: lines of a long paragraph have room to breathe, so the eye can follow each line back to the start.</p>
</body>
</html>

Result

  • A font stack is a list of fonts. The browser uses the first one installed on the device and falls back to the next. Always end with a generic family: sans-serif, serif or monospace.
  • system-ui uses the device’s own interface font. It is free, loads instantly and looks familiar to users.
  • Monospace fonts give every character the same width, which suits code and tables of figures.
  • Letter spacing (letter-spacing: 0.12em) makes small uppercase labels easier to read. Using em keeps the spacing in proportion to the font size.
  • line-height controls the space between lines. Around 1.5 to 1.7 is comfortable for paragraphs; compare the squeezed 1.1 version. Write it without a unit, so it scales with the font size.

Loading Your Own Fonts

To use a font that is not installed on the visitor’s device, host the font files with your site and describe them with @font-face:

@font-face {
  font-family: "Hind Siliguri";
  src: url("/fonts/hind-siliguri-400.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;   /* show fallback text at once, swap when the font arrives */
}

body {
  font-family: "Hind Siliguri", system-ui, sans-serif;
}
  • Use the WOFF2 format: it is compressed and supported by every modern browser.
  • Add one @font-face rule for each weight and style you use, such as 400 (regular) and 700 (bold). Each weight is a separate download, so use only what you need.
  • font-display: swap shows text in the fallback font straight away, instead of leaving it invisible while the font downloads.
  • Services such as Google Fonts give you this CSS ready-made. Check each font’s licence before using it.

Fluid Headings, Line Length and Bangla

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Fluid, balanced headings</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 12px; }
    article { max-width: 60ch; }          /* about 60 characters per line */
    h1 {
      font-size: clamp(1.4rem, 4vw, 2rem);  /* grows with the screen, within limits */
      line-height: 1.2;
      text-wrap: balance;                  /* even line lengths in headings */
      margin: 0 0 8px;
    }
    .unbalanced { text-wrap: wrap; }
    p { line-height: 1.6; margin: 0 0 8px; color: #333; }
    .bn { font-family: "Noto Sans Bengali", "Hind Siliguri", "Nirmala UI", sans-serif; }
  </style>
</head>
<body>
  <article>
    <h1 class="unbalanced">Five simple habits that make any web page easier to read</h1>
    <h1>Five simple habits that make any web page easier to read</h1>
    <p>Keep lines between 45 and 75 characters long. The <code>ch</code> unit is the width of the "0" character, so 60ch is a good maximum.</p>
    <p class="bn" lang="bn">বাংলা লেখার জন্য বাংলা ফন্ট আগে লিখুন।</p>
  </article>
</body>
</html>

Result

  • clamp(1.4rem, 4vw, 2rem) lets the heading grow with the window width, but never below 1.4rem or above 2rem.
  • text-wrap: balance makes the lines of a heading roughly equal in length. Compare the first heading, which leaves a short last line, with the balanced second one.
  • max-width: 60ch keeps lines between about 45 and 75 characters, the range most readers find comfortable. The ch unit is the width of the “0” character.
  • For Bangla, list Bangla fonts first, such as “Noto Sans Bengali” or “Hind Siliguri”, and add lang="bn" to the element. Latin-only fonts have no Bangla letters, so the browser would fall back unpredictably.
SettingGood starting value
Body font size1rem (usually 16px)
Body line height1.5 to 1.7
Heading line height1.1 to 1.3
Line lengthmax-width: 60ch to 70ch
Fonts per siteOne or two families

Common Mistakes

  1. No generic fallback at the end of a font stack.
  2. Text set in px everywhere, which ignores the user’s browser font-size setting. Use rem for font sizes.
  3. Very long lines across a wide screen. Limit the width of text columns.
  4. Loading many font weights you never use, which slows the page.

Practice Exercises

Exercise 1. Write a font stack for code blocks that tries Fira Code, then Consolas, then any monospace font.

Show answer
code, pre {
  font-family: "Fira Code", Consolas, monospace;
}

Font names with spaces need quotes; generic names such as monospace must not have them.

Exercise 2. Make an h1 that is 2rem on phones and grows to at most 3.5rem on large screens.

Show answer
h1 {
  font-size: clamp(2rem, 5vw, 3.5rem);
  line-height: 1.15;
  text-wrap: balance;
}

Frequently Asked Questions

Should I use Google Fonts or host fonts myself?

Both work. Hosting the font files on your own server means one fewer connection to another site and full control over caching. Google Fonts is quicker to set up. Either way, load only the weights you need.

What is a variable font?

A variable font is one file that contains a whole range of weights, and sometimes widths. Instead of separate files for 400 and 700, you load one file and can use any weight in between, such as 550.

Summary

  • Font stacks list preferred fonts and end with a generic family.
  • @font-face with WOFF2 and font-display: swap loads custom fonts well.
  • Use a unitless line height of about 1.6 and lines of about 60 characters.
  • clamp() and text-wrap: balance make headings fluid and tidy; list Bangla fonts first for Bangla text.

Next: HTML Form Validation.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *