px sets an absolute, fixed size unaffected by any parent's font size, while em is relative to the current element's parent's computed font size, meaning em values compound as they nest, since each nested element's 1em refers to its own parent's size, not the original root size. rem, by contrast, is always relative specifically to the root html element's font size, regardless of how deeply nested the element is, avoiding the compounding effect em can produce and making rem generally the more predictable choice for consistent sizing throughout a document.
1Understanding Font-size
px sets an absolute, fixed size unaffected by any parent's font size, while em is relative to the current element's parent's computed font size, meaning em values compound as they nest, since each nested element's 1em refers to its own parent's size, not the original root size. rem, by contrast, is always relative specifically to the root html element's font size, regardless of how deeply nested the element is, avoiding the compounding effect em can produce and making rem generally the more predictable choice for consistent sizing throughout a document.
Prefer rem over em for most font-size declarations — rem always references the root element's font size directly, avoiding the compounding effect where nested em values multiply together unpredictably as elements are nested more deeply.
html {
font-size: 16px;
}
.title {
font-size: 2rem;
}2Practical Example
Here is a real-world application of Font-size showing how it is used in production CSS code.
.parent {
font-size: 20px;
}
.child {
font-size: 1.5em;
}3Best Practices
Follow these guidelines when working with Font-size:
1. Use rem for most font-size declarations, for predictable sizing that doesn't compound unexpectedly through nested elements
2. Set the root html element's font-size explicitly, or rely on its default, typically 16px, as the reference point every rem value is calculated against
3. Consider viewport-relative units like vw sparingly, and typically combined with clamp(), for text that should scale fluidly with screen size within sensible minimum and maximum bounds
Tip: Prefer rem over em for most font-size declarations — rem always references the root element's font size directly, avoiding the compounding effect where nested em values multiply together unpredictably as elements are nested more deeply.
html {
font-size: 16px;
}
.title {
font-size: 2rem;
}