How the rem/em/px matrix works
Normalize the source value to pixels, then divide that pixel value by the root font size for rem and by the relevant element font size for em.
px = rem × root size; px = em × element sizeREM uses the root element; EM uses the relevant element or inherited font-size context.
Compare 24px with a 16px root and 20px component
A 24px value equals 1.5rem when the root is 16px, but it equals 1.2em inside a component whose effective font size is 20px.
Copyable code examples
Use these examples as a starting point, then match the values and assumptions to the rendered project.
html { font-size: 16px; }
.component { font-size: 20px; }
.title { font-size: 1.5rem; } /* 24px */const pixels = rem * rootFontSize;
const em = pixels / elementFontSize;Using the result accurately
Use rem for values that should follow a document-level scale and em for component-local relationships. Inspect computed styles when inheritance or responsive rules make the context unclear.
EM behavior depends on the CSS property and inheritance context. When em is used on font-size itself, it resolves from the parent font size rather than the final size of the element.
References: W3C CSS Values and Units: font-relative lengths.