Difference between em and rem units in CSS
While setting the size of any element in CSS, we have two choices. The first one is absolute units and the other is relative units. Absolute units are fixed and not relative to anything else. They are always identical in any case. They involve cm, mm, px, etc. On the other side, relative units are relative to something else. It may be the size of the parent element or the size of the main HTML. Relative units cover em, rem, vw, vh, etc. These are scalable units and helps in responsive design. Many of us might get confused between the relative units, especially the em and the rem units. Let’s break down the difference between those two. Basically that both rem and em are scalable and relative units of size, but with em, the unit is relative to the font size of its parent element, while the rem unit is only relative to the root font size of the HTML document. The “r” in rem stands for “root”.
Lets’s understand both of them in detail.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.
1. em Unit: The em unit allows setting the font size of an element relative to the font size of its parent. When the size of the parent element changes, the size of the child changes automatically.
Note: When em units are used on font-size property, the size is relative to the font-size of the parent. When used on other properties, it’s relative to the font-size of that element itself. Here, only the first declaration takes the reference of the parent.
- The font-size of the .child element will be 40px (2*20px).
- The margin of .child will be 60px. That’s 1.5 times the font-size of our element (1.5*40px).
2. rem Unit: The rem is based upon the font-size value of the root element, which is the <html> element. And if the <html> element doesn’t have a specified font-size, the browser default value of 16px is used. So here only the value of the root is considered, and there is no relation with a parent element.
Unlike em, here size is relative for all declarations, not only first. Let’s understand this with our previous example.
- The font-size of the .child element will be 60px (2*30px).
- The margin of .child will be 45px. That’s 1.5 times the font-size of the html element (1.5*30px).
Difference between em and rem unit is given below: em rem
Parameter Relativity em is relative to the font-size of its direct or nearest parent rem is relative to the HTML (root) font-size Compounding Effect May lead to a compounding effect Does not lead to a compounding effect
Hope you got a clear difference between the em and the rem units. It is very easy to remember, r in rem indicates root, is relative to the root (HTML) and em is relative to the parent.