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

UnitRelative ToCompounding Risk?Best Practice Usage
remRoot <html> font sizeNo (Single root target)Global typography, padding, margins
emCurrent / Parent element font sizeYes (Compounds in nested tags)Button padding proportional to icon text
chWidth of digit 0 in current fontNoOptimal line length wrapping (e.g. max-width: 65ch)
%Parent element container dimensionNoResponsive 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;: Uses em units 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 em for typography: Deeply nested elements using em cause font sizes to compound rapidly (e.g. 1.2em inside 1.2em inside 1.2em becomes 1.728x larger). Use rem for font sizes.

12. Important Rules & Limitations

  • Always use rem for 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

  1. Why does rem prevent font-size compounding issues associated with em?
  2. How does max-width: 65ch improve 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.

18. Related Topics