Open In App

Difference between em and rem units in CSS

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The em is based on the parent font size so that it can change in nested elements, while rem is based on the root font size, keeping the size same throughout the whole page.

1. em Unit

The em unit in CSS is relative to the font size of its parent element. It scales based on the current element’s font size, affecting the size of nested elements according to their parent's size.

When em is used for font-size, it’s based on the parent’s font size. For other properties, it’s based on the element’s font size, except in the first declaration where the parent is referenced.

HTML
<!DOCTYPE html>
<html>

<head> 
    <style>
    	.parent {
        	font-size: 20px;
    	}

    	.child-em {
        	font-size: 2em;
        	margin: 1.5em;
    	}
	</style>
</head>

<body>
    <div class="parent">
        This is parent
        <div class="child-em">
            This is Child in em unit system
        </div>
    </div>
</body>

</html>

Output

em unit
em unit

2. rem Unit

The rem unit in CSS is relative to the font size of the root element (<html>). It provides consistent scaling across the entire document, ensuring that elements are sized relative to a single base font size.

HTML
<!DOCTYPE html>
<html>

<head>
	<style>
	    .parent {
    	    font-size: 20px;
    	}

	    .child-rem {
    	    font-size: 2rem;
        	margin: 1.5rem;
    	}
	</style>
</head>
  
<body>
    <div class="parent">
        This is parent
        <div class="child-rem">
            This is Child in rem unit system
        </div>
    </div>
</body>

</html>

Output

rem unit
rem unit

 Difference between em and rem Units

Aspectem Unitrem Unit
Reference PointRelative to the font size of the parent element.Relative to the font size of the root element (<html>).
InheritanceInherits font size from its parent, affecting nested elements.Consistent across all elements, based on the root size.
Usage in Nested ElementsCan cause cumulative size changes in nested elements.Maintains uniform size, unaffected by nesting.
PredictabilityLess predictable due to inheritance from parent elements.More predictable and consistent across the document.
ApplicationUsed for scaling typography and spacing relative to the parent container.Ideal for consistent sizing based on a single root font size.

Similar Reads