02. Relative Font & Parent Units (rem, em, ch, %)
🟢 Beginner
1. Definition
Relative Sizing Units scale dynamically based on font size or container dimensions: rem (root em = relative to <html> font size), em (relative to current element or parent font size), ch (character width of 0 digit), and % (relative to parent box size).
2. Why It Is Used
Relative font units respect user accessibility settings. If a visually impaired user increases their browser font size to 24px, all rem typography scales proportionally across the website.
3. How It Works
By default, browser root font size is 16px. Therefore 1rem = 16px, 1.5rem = 24px, and 2rem = 32px. em compounds when nested inside parent elements with custom font sizes.
4. Syntax
h1 {
font-size: 2rem; /* 32px default */
margin-bottom: 1em; /* Relative to h1 font-size = 32px */
}
5. Relative Units Reference Table
| Unit | Relative To | Compounding Risk? | Best Practice Usage |
|---|---|---|---|
rem | Root <html> font size | No (Single root target) | Global typography, padding, margins |
em | Current / Parent element font size | Yes (Compounds in nested tags) | Button padding proportional to icon text |
ch | Width of digit 0 in current font | No | Optimal line length wrapping (e.g. max-width: 65ch) |
% | Parent element container dimension | No | Responsive column widths |
6. Beginner Example
p {
font-size: 1rem; /* 16px */
line-height: 1.5; /* Unitless multiplier */
}
7. Intermediate Example
/* Optimal readable prose width */
.article-body {
max-width: 65ch; /* Caps paragraph width at 65 characters */
margin-inline: auto;
}
8. Advanced Scalable Component Example
/* Proportional button padding using em */
.btn-large {
font-size: 1.25rem;
padding: 0.5em 1em; /* Scales padding automatically with font size */
}
9. Line-by-Line Code Explanation
max-width: 65ch;: Uses character-width units to maintain human-centric typography line length (50-75 characters per line).padding: 0.5em 1em;: Usesemunits so padding scales dynamically if button font-size changes.
10. Expected Terminal / Browser Output
Proportionally scaled typography and responsive component spacing that adapts seamlessly to browser accessibility zoom settings.
11. Common Mistakes & Pitfalls
- Overusing
emfor typography: Deeply nested elements usingemcause font sizes to compound rapidly (e.g.1.2eminside1.2eminside1.2embecomes 1.728x larger). Useremfor font sizes.
12. Important Rules & Limitations
- Always use
remfor font-size declarations to avoid compounding bugs while honoring accessibility preferences.
13. Multilingual Explanation: English
rem is relative to the root font size, while em is relative to the current element font size. Use rem for typography and ch for max-width content limits.
14. Multilingual Explanation: Hindi (Roman Script)
rem root <html> ke font size par nirbhar karta hai, jabki em parent ke font size par. Accessible font size ke liye rem ka istemaal karein.
15. Multilingual Explanation: Marathi (Roman Script)
rem unit root font size nusar badalto. Accessibility sathi typographymadhye pixel mivaje rem vapara.
16. Practice Questions
- Why does
remprevent font-size compounding issues associated withem? - How does
max-width: 65chimprove text readability?
17. Practical Coding Challenge
Write CSS rules for an .article-container with font-size: 1.125rem, line-height: 1.6, and max-width: 70ch.