Loading...
Convert pixels to em units based on the parent element's font size. Perfect for component-level scaling in CSS.
Formula
em = px รท parent font size
The em unit is one of the oldest relative units in CSS. It takes its name from typography, where an "em" was the width of the capital letter M in the current typeface. In CSS, 1em simply equals the computed font-size of the element's parent.
This parent-relative behavior is what makes em powerful and, at the same time, confusing. When you write padding: 1.5em on a button, the actual padding depends on what font size that button has. Change the font size, and the padding changes with it. This is exactly the behavior you want for components that need to scale as a unit.
But here is where it gets tricky: when you use em for the font-size property itself, it references the parent's font size, not the element's own size. And when you nest multiple levels, each em multiplies on top of the previous one. This is called the "cascade effect," and it is the main reason many developers prefer rem for font sizes.
This is the number one thing that trips people up with em. Watch how the same 1.2em declaration produces different pixel values at each nesting level:
font-size: 16px
font-size: 1.2em
font-size: 1.2em
font-size: 1.2em
Each level multiplies: 16 x 1.2 = 19.2, then 19.2 x 1.2 = 23.04, then 23.04 x 1.2 = 27.65. The same CSS declaration produces a bigger font at every level. This is why rem is safer for font sizes in complex layouts.
.btn {
font-size: 1rem; /* base: 16px */
padding: 0.5em 1.25em; /* 8px 20px - scales with font */
border-radius: 0.25em; /* 4px - scales with font */
}
.btn-lg {
font-size: 1.25rem; /* 20px */
/* padding automatically becomes 10px 25px */
/* border-radius becomes 5px */
/* Everything scales proportionally! */
}
.btn-sm {
font-size: 0.875rem; /* 14px */
/* padding becomes 7px 17.5px */
/* No need to redeclare padding or radius */
}.icon {
width: 1em; /* Same size as the text */
height: 1em;
}
/* Icon in a heading = big icon */
h1 { font-size: 2rem; }
h1 .icon { /* 32px x 32px */ }
/* Icon in body text = small icon */
p { font-size: 1rem; }
p .icon { /* 16px x 16px */ }/* This causes the cascade problem */
.list { font-size: 0.9em; }
.list .list { font-size: 0.9em; }
/* Second list: 0.9 x 0.9 = 0.81em of root */
/* Third list: 0.81 x 0.9 = 0.729em of root */
/* Text keeps shrinking! */
/* Fix: use rem for font-sizes */
.list { font-size: 0.9rem; }
/* Now every nested list is 0.9rem, period. */Most senior developers today follow a simple pattern: use rem for all font-size declarations (to avoid the cascade problem), and use em for padding, margin, and gaps within components (so spacing scales proportionally with text).
This gives you the best of both worlds. Your text sizes are predictable and accessible (rem), while your component spacing adapts automatically when you create different size variants (em). A large button with bigger text automatically gets more padding. A small label with tiny text gets tighter spacing. All from a single CSS class.
If you are just getting started with relative units, begin with rem everywhere. Once you are comfortable, start using em for padding and margins inside reusable components. That is the pattern most CSS frameworks like Tailwind and Bootstrap follow internally.
Divide the pixel value by the parent element's font size. If the parent is 16px and you want 24px, the calculation is 24 / 16 = 1.5em.
Em is relative to the nearest parent element's font size. Rem is always relative to the root html element. Em compounds when nested (an em inside an em multiplies), while rem stays consistent everywhere on the page.
It depends on the parent. If the parent element has font-size: 16px, then 1em = 16px. If the parent is 20px, then 1em = 20px. The pixel value of 1em changes based on context.
Use em when you want an element to scale relative to its parent. The classic example is button padding: if you set padding in em, the padding grows proportionally when you increase the button's font size.
When you nest elements that use em for font-size, the values multiply. A 1.2em font inside another 1.2em element becomes 1.44em (1.2 times 1.2) relative to the root. This compounding makes em tricky for font sizes in deeply nested structures.
Yes, and many experienced developers do. A common approach is rem for font sizes (consistent across the page) and em for padding and margins within components (scales with the component's text size).
When you use em for padding or margin, it is relative to the font-size of the element itself, not the parent. So an element with font-size: 20px and padding: 1em will have 20px of padding.
In media queries, em is relative to the browser's default font size (usually 16px), not the parent element. A media query at 48em triggers at 768px regardless of any font-size declarations in your CSS.
Yes. Em has been part of CSS since the very beginning. Every browser, including mobile browsers and older versions of Internet Explorer, supports em units fully.
In Chrome DevTools, right-click the parent element, click Inspect, and look at the Computed tab for the font-size property. That number is what you divide your pixel value by to get em.
Generally, rem is better for font sizes because it avoids the cascade problem. But em works well for setting font sizes within a component where you want everything to scale together when the parent size changes.
If no font size is explicitly set on the parent, the browser default of 16px is inherited. So 1em would equal 16px in that case, the same as 1rem.
Disclaimer: This tool provides approximate conversions for reference purposes. Results may vary slightly depending on browser rendering, root font size, and viewport settings. Always verify values in your specific development environment.