<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged styling</title>
<link>https://coderlegion.com/tag/styling</link>
<description>Powered by Question2Answer</description>
<item>
<title>em is a relative unit based on the parent element’s font size.</title>
<link>https://coderlegion.com/1860/em-is-a-relative-unit-based-on-the-parent-elements-font-size</link>
<description>&lt;h2 id=&quot;demystifying-the-css-em-unit-a-step-by-step-guide-with-comparisons-to-rem-and-pixels&quot;&gt;Demystifying the CSS &lt;code&gt;em&lt;/code&gt; Unit: A Step-by-Step Guide with Comparisons to &lt;code&gt;rem&lt;/code&gt; and Pixels&lt;/h2&gt;
&lt;p&gt;CSS Units are used to define the dimensions or size of an HTML element. They are broadly categorised into two: absolute and relative units. Absolute units, as implied in the name, are total or independent. They do not rely on any style definition, that is, 10px remains 10px wherever it is defined. Pixels, points, centimeter, millimeter, and pica are all absolute units in CSS. Relative units, on the other hand, depend on external style definition, and in most cases, a style definition in the parent element. They are difficult to work with as they are less predictable. Em, rem, vh, vw, percentage, vmin, and vmax, among others, are CSS relative units, but of all the relative units mentioned, em is the most notorious. It is also the most common relative unit primarily used in typography.&lt;/p&gt;
&lt;p&gt;The em unit is powerful yet misunderstood, and a good part of this article will be spent demystifying why it is both strong and confusing to developers.&lt;/p&gt;
&lt;h3 id=&quot;what-is-the-em-unit-&quot;&gt;What is the &lt;code&gt;em&lt;/code&gt; unit?&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;em&lt;/code&gt; is a relative unit based on the parent element’s font size. For instance, a div of class &lt;code&gt;.box&lt;/code&gt; with a font size of &lt;code&gt;14px&lt;/code&gt; and a padding of &lt;code&gt;1.2em&lt;/code&gt; has its padding equivalent to (1.2 x 14px).&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.box {
  font-size: 14px;
  padding: 1.2em;
}

// The box div has an a rendered padding of 16.8px
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What if the font-size of the element is declared in &lt;code&gt;em&lt;/code&gt; and not &lt;code&gt;px&lt;/code&gt;? Then, the font size of the parent element declared in &lt;code&gt;px&lt;/code&gt; is used. If the parent element has a font size also not declared in &lt;code&gt;px&lt;/code&gt; but &lt;code&gt;em&lt;/code&gt;, the closest ancestor with a font size declared in &lt;code&gt;px&lt;/code&gt; is used. Let’s see this code block for an illustration.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/* HTML */
&amp;lt;div class=&quot;parent-1&quot;&amp;gt;
  &amp;lt;div class=&quot;parent-2&quot;&amp;gt;
      &amp;lt;div class=&quot;box&quot;&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;// CSS
.parent-1 {
  font-size: 16px;
  padding: 1em;
}

.parent-2 {
  font-size: 1.2em;
  padding: 1em;
}

.box {
  font-size: 1em;
  padding: 1em;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://codepen.io/drprime01/embed/bNGvOOB&quot;&gt;https://codepen.io/drprime01/embed/bNGvOOB&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://codepen.io/drprime01/pen/bNGvOOB&quot;&gt;https://codepen.io/drprime01/pen/bNGvOOB&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;.parent-1&lt;/code&gt; div has a font size of &lt;code&gt;16px&lt;/code&gt; and a padding of &lt;code&gt;1em&lt;/code&gt;, therefore, it has an equivalent padding of &lt;code&gt;16px&lt;/code&gt;. The &lt;code&gt;.parent-2&lt;/code&gt; div, which is a child of &lt;code&gt;parent-1&lt;/code&gt;, has a font size of &lt;code&gt;1.2em&lt;/code&gt;. Since its font size is defined in &lt;code&gt;em&lt;/code&gt;, its dependent on &lt;code&gt;parent-1&lt;/code&gt; for the equivalence of its font size in &lt;code&gt;px&lt;/code&gt;. Converting the font size of &lt;code&gt;1.2em&lt;/code&gt; to &lt;code&gt;px&lt;/code&gt; will give &lt;code&gt;19.2px&lt;/code&gt; (1.2 x 16). This font size &lt;code&gt;px&lt;/code&gt; value will now be used to calculate its padding. The equivalence of &lt;code&gt;.parent-2&lt;/code&gt; padding is therefore &lt;code&gt;19.2px&lt;/code&gt;, calculated by multiplying the padding’s &lt;code&gt;em&lt;/code&gt; value by the font size &lt;code&gt;px&lt;/code&gt; equivalence. &lt;code&gt;.box&lt;/code&gt; which is now a direct descendant of &lt;code&gt;.parent-2&lt;/code&gt; and has a font size defined in &lt;code&gt;em&lt;/code&gt; will use &lt;code&gt;parent-2&lt;/code&gt;'s font size &lt;code&gt;px&lt;/code&gt; equivalence as well. 19.2 x 1 equals &lt;code&gt;19.2px&lt;/code&gt; as &lt;code&gt;.box&lt;/code&gt;'s font size. Since its padding is &lt;code&gt;1em&lt;/code&gt;, the &lt;code&gt;px&lt;/code&gt; equivalence will also be &lt;code&gt;19.2px&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Sounds confusing, right? Let’s change the value of &lt;code&gt;.box&lt;/code&gt; font size and padding to &lt;code&gt;1.2em&lt;/code&gt; and &lt;code&gt;1.4em&lt;/code&gt; respectively.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.box {
  font-size: 1.2em;
  padding: 1.4em;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This time around, &lt;code&gt;.box&lt;/code&gt; font size will be calculated by multiplying 1.2 by the font size &lt;code&gt;px&lt;/code&gt; value of &lt;code&gt;.parent-2&lt;/code&gt;, which is &lt;code&gt;19.2px&lt;/code&gt;. &lt;code&gt;.box&lt;/code&gt; will now have a font size &lt;code&gt;px&lt;/code&gt; value of &lt;code&gt;23.04px&lt;/code&gt;. By that conversion, its padding will also change to 1.4 x 23.04 to give &lt;code&gt;32.256px&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If an element’s font size &lt;code&gt;px&lt;/code&gt; value changes, all the styles defined with &lt;code&gt;em&lt;/code&gt; will change to adjust to the new value. Consequently, if the element uses &lt;code&gt;em&lt;/code&gt; for its font size and the parent element uses &lt;code&gt;px&lt;/code&gt;, once the parent element’s font size value changes, the element’s font size will change first, then other styles defined with &lt;code&gt;em&lt;/code&gt; will follow suite, adjusting to the new &lt;code&gt;px&lt;/code&gt; value of the element’s font size. For an element with an &lt;code&gt;em&lt;/code&gt; value greater than 1, the &lt;code&gt;px&lt;/code&gt; equivalence increases, but when the &lt;code&gt;em&lt;/code&gt; value is less than 1, the developer runs into the shrinking font problem as defined in CSS in Depth, and might have to spend hours debugging and checking the browser’s developer’s console for where the issue is coming from.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;em&lt;/code&gt; unit is dynamic in nature and cascades and compounds through nested elements, making it a pain in the ass for developers who do not understand it. It can be a problem for you when you use it for &lt;code&gt;width&lt;/code&gt; or &lt;code&gt;height&lt;/code&gt; and cannot explain why your nested divs are larger than the value you defined, or shrinking. It is therefore advisable to limit the use of &lt;code&gt;em&lt;/code&gt; in typography. What then is the better alternative to &lt;code&gt;em&lt;/code&gt;? Let’s explore the CSS &lt;code&gt;rem&lt;/code&gt; and pixel unit and compare them with &lt;code&gt;em&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;comparing-em-with-rem-and-pixels&quot;&gt;Comparing &lt;code&gt;em&lt;/code&gt; with &lt;code&gt;rem&lt;/code&gt; and Pixels&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;rem&lt;/code&gt; or root &lt;code&gt;em&lt;/code&gt; is a relative of &lt;code&gt;em&lt;/code&gt; but rather than being tied to the &lt;code&gt;px&lt;/code&gt; value of the same element’s or parent element’s font size, it is totally dependent on the font size defined in the root styles.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;:root {
  font-size: 16px;
} 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The CSS &lt;code&gt;rem&lt;/code&gt; unit does not compound like &lt;code&gt;em&lt;/code&gt;, making it more predictable. You can always calculate the &lt;code&gt;rem&lt;/code&gt; value of a style respective to the root’s font size and expect it to be the same. Even when font size is not defined in the root, the default font size is &lt;code&gt;16px&lt;/code&gt; so &lt;code&gt;rem&lt;/code&gt; can be calculated respective to &lt;code&gt;16px&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For the same HTML code block in the em section with nested divs and the &lt;code&gt;.box&lt;/code&gt; div, when &lt;code&gt;rem&lt;/code&gt; is used, the value changes.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// CSS
:root {
  font-size: 14px;
} 

.parent-1 {
  font-size: 16px;
  padding: 1rem;
}

.parent-2 {
  font-size: 1.2rem;
  padding: 1rem;
}

.box {
  font-size: 1rem;
  padding: 1em;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;.parent-1&lt;/code&gt; padding will be equivalent to &lt;code&gt;14px&lt;/code&gt;, not &lt;code&gt;16px&lt;/code&gt; as the case of &lt;code&gt;em&lt;/code&gt;. &lt;code&gt;.parent-2&lt;/code&gt; font size will be (1.2 x 14) &lt;code&gt;16.8px&lt;/code&gt; while its padding will remain &lt;code&gt;14px&lt;/code&gt;. The &lt;code&gt;.box&lt;/code&gt; div will also have a font size of &lt;code&gt;14px&lt;/code&gt;, while its padding, intentionally remained as &lt;code&gt;em&lt;/code&gt;, will have a value of &lt;code&gt;14px&lt;/code&gt;. In this case, it is calculated by multiplying the &lt;code&gt;14px&lt;/code&gt; of the &lt;code&gt;.box&lt;/code&gt; element’s font size after conversion and not the root element’s.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://codepen.io/drprime01/embed/EaxEGGd&quot;&gt;https://codepen.io/drprime01/embed/EaxEGGd&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://codepen.io/drprime01/pen/EaxEGGd&quot;&gt;https://codepen.io/drprime01/pen/EaxEGGd&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pixels, &lt;code&gt;px&lt;/code&gt; are CSS most used absolute unit and they maintain their value irrespective of what element’s font-size is changing.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.box {
  font-size: 14px;
  padding: 16px;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;.box&lt;/code&gt; div will always have a font size of &lt;code&gt;14px&lt;/code&gt; and a padding of &lt;code&gt;16px&lt;/code&gt; no matter what happens. The &lt;code&gt;px&lt;/code&gt; unit is static and precise and does not entertain relativity.&lt;/p&gt;
&lt;h4 id=&quot;side-by-side-comparison-table-&quot;&gt;Side-by-side comparison table:&lt;/h4&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Unit  &lt;/th&gt;&lt;th&gt;Relative to      &lt;/th&gt;&lt;th&gt;Scales with parent &lt;/th&gt;&lt;th&gt;Predictability&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;em&lt;/code&gt;  &lt;/td&gt;&lt;td&gt;Parent font size &lt;/td&gt;&lt;td&gt;Yes                &lt;/td&gt;&lt;td&gt;Contextual&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;rem&lt;/code&gt; &lt;/td&gt;&lt;td&gt;Root font size   &lt;/td&gt;&lt;td&gt;No                 &lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;px&lt;/code&gt;  &lt;/td&gt;&lt;td&gt;Fixed (absolute) &lt;/td&gt;&lt;td&gt;No                 &lt;/td&gt;&lt;td&gt;Absolute&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id=&quot;when-to-use-em-rem-or-pixels&quot;&gt;When to Use &lt;code&gt;em&lt;/code&gt;, &lt;code&gt;rem&lt;/code&gt;, or Pixels&lt;/h3&gt;
&lt;p&gt;The CSS &lt;code&gt;em&lt;/code&gt; unit shines where typography scales with components (e.g., buttons, headings in a card). It allows proportional scaling based on a parent’s size, making it ideal for modular designs. It is also good for spacing and container dimensions, except in situations where you want a fixed size.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.button { 
  font-size: 16px; 
}
.button span { 
  font-size: 0.875em; /* 14px, scales with button */
} 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;rem&lt;/code&gt; works best for global typography or spacing (e.g., site-wide margins, font sizes). It is consistent across the website or app, respects user browser settings, avoids compounding issues.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;:root { 
  font-size: 16px; 
}
p { 
  font-size: 1.125rem; /* 18px, consistent everywhere */
} 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For fine-tuned control (e.g., borders, icons, fixed layouts), use pixels. It provides precision where relativity is not needed, making it ideal for static designs.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.icon { 
  width: 24px; 
  height: 24px; 
}
.border { 
  border: 1px solid black; 
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In summary, &lt;code&gt;em&lt;/code&gt; shines in nested, relational designs but can be tricky to manage, &lt;code&gt;rem&lt;/code&gt; simplifies scaling with a single reference point, while &lt;code&gt;px&lt;/code&gt; is straightforward but less flexible for responsive designs.&lt;/p&gt;
&lt;h3 id=&quot;practical-tips-for-working-with-em&quot;&gt;Practical Tips for Working with &lt;code&gt;em&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;em&lt;/code&gt; unit can be a powerful ally in CSS, but its relative nature means it requires some care to use effectively. Here are some practical tips to help you harness its potential while avoiding common pitfalls.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Avoid deep nesting to prevent unexpected compounding.&lt;/li&gt;
&lt;li&gt;Use browser dev tools to inspect computed values.&lt;/li&gt;
&lt;li&gt;Pair &lt;code&gt;em&lt;/code&gt; with a clear parent font size for predictability.&lt;/li&gt;
&lt;li&gt;Test across screen sizes to ensure scalability.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By keeping these tips in mind—limiting nesting, inspecting computed values, anchoring &lt;code&gt;em&lt;/code&gt; to a clear parent size, and testing thoroughly—you will turn &lt;code&gt;em&lt;/code&gt; from a source of confusion into a dependable tool for flexible, scalable designs.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;The CSS &lt;code&gt;em&lt;/code&gt; unit is a beautiful relative unit for styling your websites. Despite the confusion associated with its use, it stands out as the best, especially in responsive designs. You only need to understand how it works to tap into its full potential. Once you unlock it, there is no limit to what you can achieve with it. Whenever you are confused, always remember that &lt;code&gt;em&lt;/code&gt; is relative to the parent, &lt;code&gt;rem&lt;/code&gt; to the root, and &lt;code&gt;px&lt;/code&gt; is absolute.&lt;/p&gt;
&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://dev.to/drprime01/em-or-rem-which-should-i-use-4ag2&quot;&gt;Em or Rem: Which should I use?&lt;/a&gt; by Dr Prime&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.manning.com/books/css-in-depth-second-edition?a_aid=kjg&amp;amp;a_bid=a7bc24da&amp;amp;chan=mm_website&quot;&gt;CSS In Depth&lt;/a&gt; by Keith J. Grant&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>css</category>
<guid isPermaLink="true">https://coderlegion.com/1860/em-is-a-relative-unit-based-on-the-parent-elements-font-size</guid>
<pubDate>Wed, 16 Apr 2025 20:26:19 +0000</pubDate>
</item>
</channel>
</rss>