Explain it simply
Explain it simply
A font is the voice of a website. But the font you choose only exists on a user's phone or computer if it happens to be installed or downloaded. So in CSS, font-family is not a single name but a list called a font stack: "use this if you have it, otherwise the next one". It is like ordering coffee: "a latte if you can, otherwise a cappuccino, and if all else fails, hot water".
Font size can also respect the user's browser setting and adapt to the screen width. This lesson covers both.
Problem → Why → Solution
The problem: fonts that only look right on the designer's machine
The CSS below downloads Inter and uses it for the whole page. English looks great, but Inter has no Myanmar glyphs, so Myanmar text falls back to whatever system font each device has and looks different from phone to phone. The h1 asks for font-weight: 800, yet only the 400 file is loaded, so the browser fakes the bold. And on a slow connection, users stare at invisible text for a few seconds until the font arrives.
@font-face {
font-family: "Inter";
src: url("/fonts/inter-400.woff2") format("woff2");
/* only weight 400 is loaded, no font-display */
}
body { font-family: "Inter"; font-size: 15px; } /* no fallback, no Myanmar font */
h1 { font-weight: 800; } /* weight that was never loaded */Why does this happen?
The browser checks each font in the stack for the character it needs and moves on when the glyph is missing; when the stack runs out, the system picks whatever it likes. Asking for a weight that was never loaded makes the browser synthesize a bold from the regular file, which never matches the design. Without font-display, most browsers also hide the text for a while until the font arrives.
Core concept: the stack, loading and size
font-family is read from left to right, and the browser chooses a font per character, not once per element. With "Noto Sans Myanmar", Arial, sans-serif, Myanmar characters come from Noto Sans Myanmar, and anything that font lacks comes from Arial. Always end the stack with a generic family such as sans-serif, serif, monospace or system-ui, and quote names that contain spaces.
Web fonts are declared with @font-face, one file per weight and style. The weights you request in CSS must match the files you load; otherwise the browser uses the nearest weight or fakes a bold. A single woff2 format covers every modern browser. font-display decides what to show while the font downloads: swap shows the text in the fallback font immediately and swaps in the web font when it arrives. If the fallback and web font differ a lot in size, the swap makes the layout jump, so pick a similar fallback.
If you want no download at all, a system font stack such as system-ui, "Segoe UI", Roboto, sans-serif is the fastest option, because it uses the device's own interface font.
For font sizes, rem multiplies the root (html) element's font-size, which comes from the browser setting and is 16px for most users. If someone enlarges the default font in their browser settings, everything in rem grows with it, while text sized in px stays put. clamp(MIN, PREFERRED, MAX) uses the preferred value but never goes below MIN or above MAX.
font-weight takes numbers from 100 to 900, where normal equals 400 and bold equals 700. The numbers only point at weights that exist in your font files; changing the number cannot create a weight you never loaded.
- Look at the first font in the stack
- Check it has this character's glyph
- If not, move to the next font
- Stack exhausted: generic or system font
It is normal for Myanmar and English characters in the same paragraph to come from different fonts.
General pattern
@font-face {
font-family: "Family Name";
src: url("/fonts/file.woff2") format("woff2");
font-weight: 400;
font-display: swap;
}
selector {
font-family: "Family Name", "Local Fallback", generic-family;
font-size: clamp(min-rem, preferred, max-rem);
}@font-face links one font file to a family name, with one block per weight. End every font-family stack with a generic family and base font sizes on rem.
Example: a Myanmar font stack and a responsive heading
This example is a plain font setup for a Myanmar site. There is no @font-face, so nothing is downloaded: the browser only searches the fonts already on the user's device, in order. The h1 changes size with the screen width.
body {
font-family: "Noto Sans Myanmar", "Myanmar Text", Arial, sans-serif;
font-size: 16px;
line-height: 1.75;
}
h1 {
font-size: clamp(2rem, 5vw, 4rem);
font-weight: 800;
}Code breakdown
- Line 2
font-family: "Noto Sans Myanmar", "Myanmar Text", Arial, sans-serif;Uses Noto Sans Myanmar when it is installed, otherwise Myanmar Text, which ships with Windows. Arial has no Myanmar glyphs, so it only serves Latin letters and digits, and sans-serif is the final safety net.
- Line 3
font-size: 16px;Written in px, so body text stays at 16px even if the user has enlarged the default font in browser settings. font-size: 1rem looks identical for a typical user and respects that setting, so it is the better choice.
- Line 4
line-height: 1.75;A roomy line-height so marks above and below Myanmar letters do not collide between lines.
- Line 8
font-size: clamp(2rem, 5vw, 4rem);5vw is 5% of the viewport width. Below 640px it holds at 2rem (32px), between 640px and 1280px it grows, and above 1280px it stops at 4rem (64px).
- Line 9
font-weight: 800;If the chosen system font has no 800 weight, the browser uses the nearest heavier weight, or synthesizes a bold if there is none.
Body text renders in Noto Sans Myanmar if installed, otherwise Myanmar Text, then Arial and the system sans-serif. The h1 is 32px below a 640px viewport, 50px at 1000px, and 64px from 1280px up.Comparing font-display values
While a web font downloads, the browser has two choices: hide the text, or show it in a fallback font. font-display puts that decision in your hands. Body text usually gets swap or fallback, while purely decorative fonts can use optional.
| Value | While loading | When it arrives |
|---|---|---|
| auto (default) | Browser decides, usually hides briefly | Swaps in |
| swap | Shows the fallback at once | Swaps in whenever it lands |
| fallback | Very short hide, then fallback | Swaps only if it lands within a few seconds |
| optional | Very short hide, then fallback | Too late: not used on this page |
Mix rem into clamp()
In clamp(2rem, 5vw, 4rem) the middle value is pure vw, so between the limits the text barely responds to browser zoom. Mixing in a rem part, as in clamp(2rem, 1.5rem + 2.5vw, 4rem), keeps it responsive to both zoom and the font-size setting.
In real projects
Where this shows up in real projects
Production sites host fonts as woff2 on their own server or CDN and load only the two or three weights they actually use, because every extra weight is one more download. Tools such as next/font in Next.js self-host the files and automatically add font-display and a size-adjusted fallback.
Myanmar sites usually put a Unicode font (Noto Sans Myanmar, Padauk or Pyidaungsu) first in the stack and add Windows's Myanmar Text as a local fallback. Admin dashboards and internal tools often choose a system font stack instead, since it needs no download and matches each platform's native UI. Before release, check font-related layout shift in Lighthouse or the DevTools Performance panel.
Common mistakes
Common mistakes
1. A stack with no generic family
With font-family: "Brand Sans"; alone, a failed font falls back to the browser default, often a serif like Times. Always end the stack with a generic family such as sans-serif.
2. Requesting weights you never loaded
Load only the 400 file, write font-weight: 700 on h1, and the browser draws a fake bold with smeared letter shapes. Write one @font-face per weight the design uses, or limit the design to the weights you load.
3. Pinning body font-size in px
html { font-size: 14px; } or a px size on body ignores users with low vision who raised the default font size in their browser. Leave the root alone and use 1rem for body text.
Best practices
Best practices
- Keep to two font families at most and load only two or three weights.
- Give every web font a font-display value and choose a fallback of similar size.
- Throttle the network to Slow 4G in DevTools and confirm text shows before the font arrives.
Mini challenge
Mini challenge: a self-hosted Myanmar font setup
Load Noto Sans Myanmar from your own server in two weights (400 and 700), and make sure the text is visible immediately, before the font arrives.
- Two @font-face rules for weights 400 and 700, both with font-display: swap
- A body stack of Noto Sans Myanmar, Myanmar Text, system-ui, sans-serif
- A body font-size in rem and a line-height of 1.8
- An h1 sized clamp(1.75rem, 1.25rem + 2.5vw, 3rem) using the loaded weight 700
<!doctype html>
<html lang="my">
<style>
/* TODO: @font-face for weight 400 (/fonts/noto-sans-myanmar-400.woff2) */
/* TODO: @font-face for weight 700 (/fonts/noto-sans-myanmar-700.woff2) */
body {
/* TODO: font stack ending in a generic family, rem size, line-height */
}
h1 {
/* TODO: fluid size with clamp() and a weight you actually loaded */
}
</style>
<h1>မင်္ဂလာပါ</h1>
<p>ဒီစာမျက်နှာက font fallback ကို စမ်းသပ်ဖို့ပါ။</p>
</html>Expected: With the font files missing, the text appears at once in Myanmar Text or a system font; once the files exist, it swaps to Noto Sans Myanmar after download. The h1 is 29px at 360px wide, 45px at 1000px, and stops at 48px from 1120px up.
Show hint
Both @font-face blocks use the same font-family name and differ only in font-weight. The browser matches the h1's 700 to the second block automatically.
Show solution
<!doctype html>
<html lang="my">
<style>
@font-face {
font-family: "Noto Sans Myanmar";
src: url("/fonts/noto-sans-myanmar-400.woff2") format("woff2");
font-weight: 400;
font-display: swap;
}
@font-face {
font-family: "Noto Sans Myanmar";
src: url("/fonts/noto-sans-myanmar-700.woff2") format("woff2");
font-weight: 700;
font-display: swap;
}
body {
font-family: "Noto Sans Myanmar", "Myanmar Text", system-ui, sans-serif;
font-size: 1rem;
line-height: 1.8;
}
h1 {
font-size: clamp(1.75rem, 1.25rem + 2.5vw, 3rem);
font-weight: 700;
line-height: 1.3;
}
</style>
<h1>မင်္ဂလာပါ</h1>
<p>ဒီစာမျက်နှာက font fallback ကို စမ်းသပ်ဖို့ပါ။</p>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A font stack is searched left to right per character; always end it with a generic family.
- Load one file per weight you use in @font-face and add font-display: swap.
- Size body text in rem so the user's browser setting is respected.
- Use clamp(MIN, PREFERRED, MAX) for fluid headings and mix rem into the preferred value.
CSS reference — MDN Web Docs