Sub topic 11
Font size, weight, and style
This chapter covers the three properties that most directly shape how text looks: font-size, font-weight, and font-style. It also covers the font shorthand, which sets them together. It assumes you know basic HTML structure, CSS selectors and the cascade, the box model, and how to link a stylesheet. It also builds on the previous chapters on relative units (em, rem, and percentages) and on choosing font families.
How font size is resolved
The font-size property sets the size of the font's em box. That is the nominal height from which a typeface's glyphs are scaled. It does not set the height of capital letters or the line height. Two fonts at 16px can look noticeably different in size, because designers draw glyphs with different proportions inside the em box.
Every element has a computed font-size, and that value is inherited. If you do not set it, a child uses its parent's computed size. The root value starts from the browser's default, which is 16px in most browsers unless the user changes it.
Choosing units for font size
The units you pick decide whether text respects user settings and how sizes compound.
| Unit | Relative to | Effect on user preferences | Typical use |
|---|---|---|---|
| `px` | Nothing (absolute) | Ignores the browser's default font size setting | Hairlines, borders, rarely text |
| `rem` | Root element's font size | Follows the user's setting | Main type scale |
| `em` | Parent's font size (for `font-size`) | Follows the user's setting, but compounds | Components that scale as a unit |
| `%` | Parent's font size | Same as `em` | Setting the root size |
| `vw`, `clamp()` | Viewport, with bounds | Needs a `rem` term to respect settings | Fluid headings |
The most reliable approach is to leave the root size alone, or set it as a percentage, and size everything else in rem.
html {
font-size: 100%;
}
body {
font-size: 1rem;
}
h1 {
font-size: 2rem;
}
small {
font-size: 0.875rem;
}Setting html { font-size: 100%; } keeps the user's choice. Setting html { font-size: 16px; } overrides it. A visitor who raised their default size for readability then sees no change.
The `em` compounding trap
Inside font-size, an em value is relative to the parent's font size. Nested elements multiply the effect.
ul {
font-size: 0.875em;
}With this rule, a top-level list renders at 87.5% of its parent. A list nested in that list is 87.5% of 87.5%, which is about 76.6%. A third level drops to about 67%. Text gets smaller at each level, which is rarely what you want.
You can avoid this in two ways. Use rem, which always refers back to the root. Or reset nested lists explicitly.
ul {
font-size: 0.875rem;
}
ul ul {
font-size: inherit;
}Remember that em means something different in other properties. In padding, margin, and border-width, one em equals the element's own computed font-size. That is why padding in em can scale with a button's text.
Keyword and relative sizes
CSS also defines size keywords: xx-small, x-small, small, medium, large, x-large, xx-large, and xxx-large. medium matches the browser's default size. The keywords are rarely used for design work because their exact ratios are not guaranteed to match your type scale.
The relative keywords smaller and larger move one step from the parent's size. They are handy in generic components, but the step size is browser-defined.
Fluid font sizes
You can combine rem and viewport units inside clamp() so type grows with the screen but stays within limits.
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
}The rem term in the middle value matters. A pure vw size does not respond when the user zooms text only, so including rem keeps some user control. Browser zoom, which scales everything, still works.
Font weight
font-weight selects how heavy the strokes are. It accepts keywords and numbers.
| Value | Meaning |
|---|---|
| `100` | Thin |
| `200` | Extra light |
| `300` | Light |
| `400` | Regular (same as `normal`) |
| `500` | Medium |
| `600` | Semi-bold |
| `700` | Bold (same as `bold`) |
| `800` | Extra bold |
| `900` | Black |
| `lighter`, `bolder` | One step lighter or heavier than the parent, by a lookup table |
Modern CSS accepts any number from 1 to 1000, not just multiples of 100. Intermediate values like 550 only look different if the font is a variable font, which contains a continuous range of weights in a single file. For a static font, the browser picks the nearest available face.
How the browser matches a weight
When you request a weight, the browser looks through the faces available for that family. For a request between 400 and 500, it tries the requested weight, then heavier weights up to 500, then lighter ones. For a request above 500, it tries heavier weights first. For a request below 400, it tries lighter weights first.
If the exact face is missing, the browser falls back to the closest one. If the closest is much lighter than the request and the request is 600 or above, the browser may synthesize a bold by smearing the glyphs. Synthesized bold usually looks worse than a real bold face, and it spaces letters unevenly. You can see this when you load only the 400 weight of a custom font and then style a heading with font-weight: 700.
@font-face {
font-family: "Inter";
src: url("/fonts/inter.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
h2 {
font-weight: 700;
}In this example, only the 400 face exists, so the h2 gets a synthesized bold. The fix is to load a real 700 face with a second @font-face block, or to load a variable font and declare a weight range such as font-weight: 100 900;.
Weight and semantics
Elements like <strong>, <b>, <th>, and headings have a default bold weight from the browser stylesheet. Setting font-weight: 400 on a heading changes only the appearance. It does not change the meaning in the document outline or for screen readers. Use CSS for how things look and HTML for what they mean.
Font style
font-style chooses between upright, italic, and oblique faces.
normal: the upright face.italic: uses the family's italic face if it has one. Italics are often redrawn letterforms, not just slanted ones.oblique: uses an oblique face if present. If not, the browser slants the upright face. You can add an angle, such asoblique 10deg, which is useful with variable fonts that have a slant axis.
If a family has no italic face, the browser slants the regular face to imitate one. This is called a synthesized italic. It is usually acceptable for short phrases, but it differs from a designed italic, especially in letters like a and f.
em,
i,
cite {
font-style: italic;
}
.note {
font-style: oblique 10deg;
}
blockquote em {
font-style: normal;
}The last rule shows a common technique. Inside a block that is already italic, setting font-style: normal on emphasized text reverses the style. This keeps emphasis visible.
The `font` shorthand
The font property sets several values in one declaration. Its order is strict.
.lead {
font: italic 600 1.25rem/1.5 "Inter", system-ui, sans-serif;
}The order is: style, weight (and other optional keywords), size, then / line-height, then family. Only size and family are required. If you leave either out, the whole declaration is invalid and the browser ignores it.
The shorthand also resets every sub-property it covers that you did not list. That includes line-height, font-variant, font-stretch, and a few others. This causes a frequent bug.
body {
line-height: 1.6;
}
.card {
font: 1rem "Inter", sans-serif;
}Here .card loses its line height. The shorthand resets line-height to normal, which overrides the value inherited from body. Add / 1.6 after the size, or set line-height after the shorthand.
Many teams avoid the shorthand and write each property separately. This makes diffs clearer and avoids silent resets.
Common errors
- Setting the root size in
px. This ignores the user's browser setting. Use100%or leave it unset. - Nested
emsizes. Lists, tables, and components inside components shrink or grow repeatedly. Userem, orinheriton nested elements. - Requesting weights you did not load. The browser synthesizes bold. Load the faces you use.
- Using
font-weight: boldon a heavy font. If the font is already heavy at400, the result may be too thick. Check which weights the family offers. - Overusing
fontshorthand. It resetsline-height. Place it before more specific declarations. - Using
<i>or<b>for emphasis. Use<em>and<strong>when the meaning is emphasis or importance. Style with CSS. - Tiny text. Body text under about
1remis hard to read for many people. Reserve smaller sizes for captions and legal notes, and keep them readable.
A small type scale
You can combine what you have learned in a short set of custom properties. This keeps sizes consistent across the stylesheet.
:root {
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: 1.25rem;
--text-xl: clamp(1.75rem, 1rem + 3vw, 3rem);
}
body {
font-size: var(--text-base);
font-weight: 400;
}
h1 {
font-size: var(--text-xl);
font-weight: 700;
}
.caption {
font-size: var(--text-sm);
font-style: italic;
}Because every value is in rem or clamp() with a rem term, the whole scale respects the user's browser setting.
Exercise
A stylesheet has ul { font-size: 0.8em; }. A list is nested three levels deep inside a body whose font size is 16px. What is the computed font size of the innermost list, and how do you keep all levels at 12.8px?
Show answer
Each level multiplies by 0.8, so the third level is 16 × 0.8 × 0.8 × 0.8 = 8.192px. To keep every level at 12.8px, use rem (ul { font-size: 0.8rem; }), or add ul ul { font-size: inherit; } so nested lists take their parent's size.
Exercise
You wrote .note { font: 1.1rem Georgia, serif; } inside a page where body sets line-height: 1.6 and font-weight: 600. Which two inherited values change for .note, and why?
Show answer
Both line-height and font-weight are reset to their initial values (normal and 400), because the font shorthand resets every sub-property it covers that you did not list. Fix it by listing them, as in font: 600 1.1rem/1.6 Georgia, serif;, or by writing the longhand properties separately.