Sub topic 11
Font size, weight, and style
This chapter assumes you know CSS selectors and the cascade, the box model, and how to set a font family with a fallback stack. It also assumes you know what em and rem mean. Here you will set how large, how heavy, and how slanted text is.
Font size
font-size sets the size of the text. It also becomes the reference for every em value on that element. Use relative units so the text follows the user's browser setting.
html {
font-size: 100%;
}
body {
font-size: 1rem;
}
.button {
font-size: 1.125rem;
padding: 0.5em 1em;
}100% on html keeps the browser default, usually 16px. 1rem is that default, and 1.125rem is 18px. The button's padding uses em, so it grows with the button's own font size.
Avoid px on the root
If you write html { font-size: 16px; }, you replace the user's preference. People who raised their default text size for readability will not see the change on your site. Use 100% or leave the root alone.
How em compounds
An em in font-size is relative to the parent's font size. Nested elements multiply:
ul {
font-size: 1.2em;
}A list inside a list inside a list gets 1.2 × 1.2 × 1.2 of the base size, which is about 1.73 times. This is the most common em bug. Use rem for font sizes, and keep em for spacing that should scale with the element's text.
Font weight
font-weight controls how thick the strokes are. It accepts keywords and numbers.
| Value | Meaning |
|---|---|
| `400` or `normal` | Regular text |
| `700` or `bold` | Standard bold |
| `100` to `900` | Numeric scale, in steps of 100 for most static fonts |
| `lighter`, `bolder` | Relative to the parent's weight |
Numbers from 1 to 1000 are valid, and a variable font can use any value in its range, such as 550. Numeric values are clearer than bolder and lighter, whose results depend on the inherited weight.
h1 {
font-weight: 800;
}
.note {
font-weight: 300;
}The font must actually contain the weight you ask for. If you request 800 and only 400 and 700 are available, the browser picks the closest face. If no bold face exists at all, it may fake one by smearing the glyphs, which looks uneven. Load the weights you use, or use a variable font.
Font style
font-style selects the slant. The values are normal, italic, and oblique.
em,
.quote {
font-style: italic;
}
.reset {
font-style: normal;
}An italic face is usually a separately designed set of letterforms. An oblique face is the upright letters slanted. If a font has no italic, the browser falls back to oblique. As with weight, you must load the italic file if you want a real one.
The font shorthand
The font shorthand sets several properties at once. Size and family are required. The order is style, weight, size, line height, family.
.caption {
font: italic 600 0.875rem/1.4 "Inter", system-ui, sans-serif;
}The /1.4 sets the line height. If you leave it out, the shorthand resets line height to normal, which can silently override an earlier line-height rule. For that reason many people write the longhand properties.
Common errors
- Setting
font-sizeinpxonhtml, which ignores the user's default size. - Nesting
emfont sizes and getting text that grows at every level. - Requesting a weight or italic that was never loaded, so the browser fakes it.
- Using
font:and losing an earlierline-height.
Exercise
A .card has font-size: 1.25em and contains a .card with the same rule. The outer card sits in a body with a 16px base size. What is the font size of the inner card, and what change avoids the problem?
Show answer
The outer card is 16 × 1.25 = 20px. The inner card is 20 × 1.25 = 25px, because em is relative to the parent. Changing the rule to font-size: 1.25rem makes both cards 20px, since rem always refers to the root font size.
Wait: I left an empty mermaid fence, which is invalid. Corrected chapter below.
Font size, weight, and style
This chapter assumes you know CSS selectors and the cascade, the box model, and how to set a font family with a fallback stack. It also assumes you know what em and rem mean. Here you will set how large, how heavy, and how slanted text is.
Font size
font-size sets the size of the text. It also becomes the reference for every em value on that element. Use relative units so the text follows the user's browser setting.
html {
font-size: 100%;
}
body {
font-size: 1rem;
}
.button {
font-size: 1.125rem;
padding: 0.5em 1em;
}100% on html keeps the browser default, usually 16px. 1.125rem is 18px. The button's padding uses em, so it grows with the button's own font size.
Avoid px on the root
If you write html { font-size: 16px; }, you replace the user's preference. People who raised their default text size will not see the change on your site. Use 100% or leave the root alone.
How em compounds
An em in font-size is relative to the parent's font size. Nested elements multiply:
ul {
font-size: 1.2em;
}A list inside a list inside a list gets 1.2 × 1.2 × 1.2 of the base size, about 1.73 times. This is the most common em bug. Use rem for font sizes, and keep em for spacing that should scale with the element's text.
Font weight
font-weight controls how thick the strokes are. It accepts keywords and numbers.
| Value | Meaning |
|---|---|
| `400` or `normal` | Regular text |
| `700` or `bold` | Standard bold |
| `100` to `900` | Numeric scale, in steps of 100 for most static fonts |
| `lighter`, `bolder` | Relative to the parent's weight |
Any number from 1 to 1000 is valid, and a variable font can use any value in its range, such as 550. Numbers are clearer than bolder and lighter, whose results depend on the inherited weight.
h1 {
font-weight: 800;
}
.note {
font-weight: 300;
}The font must contain the weight you ask for. If you request 800 and only 400 and 700 are available, the browser picks the closest face. If no bold face exists at all, it may fake one by thickening the glyphs, which looks uneven. Load the weights you use, or use a variable font.
Font style
font-style selects the slant. The values are normal, italic, and oblique.
em,
.quote {
font-style: italic;
}
.reset {
font-style: normal;
}An italic face is usually a separately designed set of letterforms. An oblique face is the upright letters slanted. If a font has no italic, the browser falls back to oblique. As with weight, you must load the italic file to get a real one.
The font shorthand
The font shorthand sets several properties at once. Size and family are required. The order is style, weight, size, line height, family.
.caption {
font: italic 600 0.875rem/1.4 "Inter", system-ui, sans-serif;
}The /1.4 sets the line height. If you leave it out, the shorthand resets line height to normal, which can silently override an earlier line-height rule. For that reason many people write the longhand properties.
Common errors
- Setting
font-sizeinpxonhtml, which ignores the user's default size. - Nesting
emfont sizes and getting text that grows at every level. - Requesting a weight or italic that was never loaded, so the browser fakes it.
- Using
font:and losing an earlierline-height.
Exercise
A .card has font-size: 1.25em and contains another .card with the same rule. The body font size is 16px. What is the font size of the inner card, and what change avoids the problem?
Show answer
The outer card is 16 × 1.25 = 20px. The inner card is 20 × 1.25 = 25px, because em is relative to the parent. Changing the rule to font-size: 1.25rem makes both cards 20px, since rem always refers to the root font size.