Font Size: Absolute and Relative Units
You'll learn how to set text size using different CSS units and understand when each is most appropriate.
The Core Idea
Font size can be defined using absolute units (like px) that stay fixed, or relative units (like em, rem, and %) that scale based on other values. Think of absolute units as saying "I want this text exactly 16 pixels tall," while relative units say "I want this text 1.5 times bigger than the base size."
The Four Common Units
px (Pixels): A fixed unit that doesn't change. Setting font-size: 16px; means the text will always be 16 pixels tall, regardless of user preferences or parent elements.
rem (Root em): Relative to the root <html> element's font size. If the root is 16px (browser default), then font-size: 2rem; equals 32px. This is ideal for consistent, scalable sizing throughout your site.
em: Relative to the parent element's font size. If a parent has font-size: 20px, then a child with font-size: 1.5em; becomes 30px. Beware: ems can compound when nested!
% (Percentage): Works like em — relative to the parent's font size. font-size: 150%; on a child with a 20px parent equals 30px.
When to Use Each
- Use
remfor most typography — predictable, accessible, and respects user preferences - Use
emfor components that should scale relative to their context - Use
pxsparingly, mainly for borders or specific design requirements - Use
%when you want clear proportional relationships
Key Takeaway: Choose rem for global consistency, em for contextual scaling, and avoid px for font sizes to ensure accessibility and responsive design.