CSS em vs. rem: How Font-Relative Units Are Calculated
CSS em and rem are lengths relative to a font size. In font-size, em uses the inherited font size. In properties such as padding, it uses the element’s own computed font size. rem normally uses the font size of the document root, html.
What font size does em use?
| Property | em reference | rem reference |
|---|---|---|
font-size |
Inherited font size | Root font size |
padding, margin, width |
The element’s own font size | Root font size |
If a parent is 20px and its child has font-size: 1.5em, the child is 30px. Its padding: 1em is also 30px. Another nested element with font-size: 1.5em gets 30 × 1.5 = 45px. CSS Values and Units · Font-relative Lengths
How rem follows the root font size
When the root is 16px, 1.5rem is 24px. An intermediate parent’s font size does not change it. html { font-size: 100%; } starts from the browser’s default font size, which can differ from 16px. When rem is used in the root’s own font-size, it resolves against the initial font size. CSS Values and Units · Font-relative Lengths
Compare nested font sizes and padding
Save the following code as em-rem.html and open it in a browser. Use developer tools to select the outer section and the inner div. Check their computed font-size and the outer box’s padding.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare em and rem</title>
<style>
body { margin: 20px; font: 1rem/1.6 system-ui, sans-serif; color: #203348; }
html { font-size: 100%; }
.parent { font-size: 20px; max-width: 650px; }
.em { font-size: 1.5em; }
.rem { font-size: 1.5rem; }
.panel { border: 1px solid #6685a5; padding: 1em; margin-bottom: 1rem; overflow-wrap: anywhere; }
.child { border-left: 4px solid #16818c; padding-left: 8px; }
h1 { font-size: 1.5rem; }
</style>
</head>
<body>
<h1>Reference sizes for em and rem</h1>
<div class="parent">
<section class="panel em">Outer em
<div class="child em">Inner em</div>
</section>
<section class="panel rem">Outer rem
<div class="child rem">Inner rem</div>
</section>
</div>
</body>
</html>
With a browser default of 16px, the outer em text is 30px and the inner text is 45px. Both rem sizes are 24px. The outer panels both use padding: 1em, so they get 30px and 24px of padding respectively.
Change only the html font size from 100% to 125% and refresh. With a 16px browser default, the root becomes 20px and the rem text and outer padding become 30px. The em text stays at 30px and 45px because its shared parent is set to 20px. Restore 100% after comparing.
Choose a unit for pages or components
Use rem for a consistent scale across headings and spacing. Use something like padding: 0.5em 1em when a button’s spacing should grow with its own text. If enlarging the text causes clipping, check fixed heights and wrapping rules as well as the units.
Media queries have a separate reference. In @media (min-width: 40em), em follows the browser’s initial font size instead of the root’s declared value. An initial 16px gives a 640px breakpoint. Changing only the root to 125% in the example does not move it. Media Queries · Units




