The font-size Property
The CSS font-size property is used to specify the height of the characters in a font. Choosing the right font size and unit is one of the most critical decisions in web design, directly impacting readability and accessibility.
Units for font-size
Font sizes can be set using several different types of units, which fall into two main categories: absolute and relative.
1. Absolute Units (Fixed)
Absolute units are fixed measurements that do not change based on any other element or user setting.
| Unit | Description | Best Use Case |
|---|---|---|
px (Pixels) | A fixed number of dots (pixels) on the screen. | Small elements that must be pixel-perfect, such as thin borders or box shadows. Avoid for font size. |
Avoid using px for font sizes on body text. If a user tries to zoom their browser text using system settings, px-based fonts will often fail to resize, creating an accessibility barrier.
2. Relative Units (Scalable)
Relative units are the best practice for typography because they allow the text to scale up or down relative to another size, respecting user settings and promoting responsiveness.
| Unit | Description | Calculation | Best Use Case |
|---|---|---|---|
em | Relative to the font size of the parent element. | Component-level scaling (e.g., a button inside a card). | |
rem | Relative to the font size of the root element (<html>). | Global sizing consistency, main typography. The recommended unit. | |
% (Percent) | Relative to the font size of the parent element. | Similar to em, but % is often used for explicit scaling. |
Why rem is Recommended
Using rem (Root EM) is the modern standard because it prevents sizing issues caused by complex nesting.
- Browser Control: By default, is the browser's root font size (i.e., ). If a user changes their browser's default font size to , then automatically becomes across your entire site.
- Predictability: Unlike
em, where the size multiplies on every nested parent (the "compounding problem"),remalways refers back to the single<html>root size, making calculations simple and predictable.
Example Scaling with rem
If your site's root size is set to the default :
| Desired Size | rem Value | Calculation |
|---|---|---|
| Standard Body Text | 1rem | |
| Large Heading | 2.5rem | |
| Small Caption | 0.875rem |
Interactive font-size Demo
Use the live editor to change the font-size values for both the parent (.parent-box) and the child (.child-text). Observe how the em value scales relative to its parent, while the rem value remains constant relative to the root ().