<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rina I on Medium]]></title>
        <description><![CDATA[Stories by Rina I on Medium]]></description>
        <link>https://medium.com/@rinai?source=rss-3b8bfe041ccf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Lutv_eUXWnSwQShR</url>
            <title>Stories by Rina I on Medium</title>
            <link>https://medium.com/@rinai?source=rss-3b8bfe041ccf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 19:30:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rinai/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Should You Use em or rem in CSS? A Practical Developer's Perspective]]></title>
            <link>https://medium.com/@rinai/should-you-use-em-or-rem-in-css-a-practical-developers-perspective-e6acb279d479?source=rss-3b8bfe041ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6acb279d479</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[accessibility]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[web-design]]></category>
            <category><![CDATA[design-systems]]></category>
            <dc:creator><![CDATA[Rina I]]></dc:creator>
            <pubDate>Tue, 22 Apr 2025 06:33:53 GMT</pubDate>
            <atom:updated>2025-04-22T06:33:53.672Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Hey everyone!</strong> I wanted to share some insights on a topic that confused me early in my frontend journey — <strong>when to use </strong><strong>em vs </strong><strong>rem in CSS</strong>. It seemed simple at first, but I kept running into unexpected layout bugs, inconsistent spacing, and weird behavior in responsive designs.</p><p>This really hit home during a debate with a colleague who uses em for everything—<strong>font sizes, padding, margins</strong>. His take? Unless the parent font size changes, em behaves the same as rem. And while that’s <em>technically</em> true, I’ve learned that it’s not the best approach in most real-world situations.</p><p>So in this post, I’ll break down <strong>why </strong><strong>rem is often a better choice</strong>, and share real-world examples where relying on em can lead to problems. If you care about <strong>scalable CSS</strong>, <strong>consistent layouts</strong>, and <strong>accessibility</strong>, this is for you!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CytLjBJHozDcEqI1" /><figcaption>Photo by <a href="https://unsplash.com/@alterego_swiss?utm_source=medium&amp;utm_medium=referral">AbsolutVision</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>❓What’s the Difference Between em and rem?</h3><ul><li><strong>em</strong>: Relative to the <strong>font size of the element’s parent</strong>.</li><li><strong>rem</strong>: Relative to the <strong>root element’s font size</strong> (usually html).</li></ul><p>The key issue with em is that it <strong>compounds</strong> through nested elements. If you change the font size on a parent or grandparent, suddenly all your em-based spacing, font sizes, and dimensions change too—sometimes in unpredictable ways.</p><h3>💭Why rem is More Reliable for Layout</h3><p>Using rem for things like <strong>font sizes, spacing (margins, paddings), and layout widths</strong> ensures everything scales from a single base unit. This leads to:</p><ul><li><strong>Predictable sizing</strong> across components</li><li><strong>Easier maintenance</strong> of consistent vertical rhythm</li><li><strong>Better accessibility</strong> (user font scaling doesn’t break layout)</li><li><strong>Modular design tokens</strong> (like spacing systems in Tailwind or design systems)</li></ul><h3>💡When em is Still Useful</h3><p>There <em>are</em> times when em makes sense—usually in tightly scoped, component-specific use cases:</p><ul><li><strong>Button padding</strong>: So it scales naturally with font size</li><li><strong>Border radius</strong> or <strong>icons next to text</strong></li><li><strong>Line height</strong> relative to text size</li></ul><p>In these cases, you <em>want</em> proportional scaling. But for layout and spacing? Stick with rem.</p><h3>🔥 Where em Can Go Wrong: Real-World Examples</h3><p>While em has its place in CSS, using it <em>everywhere</em>—especially for layout and spacing—can lead to subtle bugs and inconsistent behavior. Here&#39;s a breakdown of where things often go sideways when relying too much on em.</p><p>1. <strong>Spacing Compounds Unintentionally in Nested Components</strong></p><pre>.card {<br>  font-size: 1.25em;<br>  padding: 2em;<br>}<br><br>.card .title {<br>  font-size: 1.1em;<br>  margin-bottom: 1em;<br>}</pre><p>If the html font size is 16px, here’s what happens:</p><ul><li>.card font size = 1.25 * 16 = 20px</li><li>.title font size = 1.1 * 20 = 22px</li><li>.title margin-bottom = 1 * 22 = 22px (not 20px like you may expect)</li></ul><p>❗️<strong>Why it’s bad</strong>: The more levels you nest, the more these values <strong>compound</strong>, making it harder to predict or align with a consistent spacing scale. Even worse — if someone later adjusts .card’s font size, margins and paddings inside change <strong>unexpectedly</strong>.</p><h3>2. Responsive Breakpoints Shift When html Font Size Changes</h3><p>Some devs use em in media queries thinking it&#39;s just a flexible unit:</p><pre>@media (min-width: 40em) {<br>  .grid {<br>    gap: 2em;<br>  }<br>}</pre><p>Sounds okay, right? But if you later change the root font size to 18px for accessibility, that 40em breakpoint jumps from 640px to 720px.</p><p>🧠 <strong>Tip</strong>: Use rem instead. Media queries should be based on <strong>viewport width</strong>, not font size.</p><pre>@media (min-width: 40rem) {<br>  .grid {<br>    gap: 2rem;<br>  }<br>}</pre><p>✅ This stays <strong>fixed at 640px</strong>, regardless of user settings or root font size tweaks.</p><p>3. When Button Padding with em Breaks Consistency Across Contexts</p><p>Using em for padding <em>inside a button</em> can be a good idea—after all, if the button’s text grows, the padding scales with it, which often keeps things visually balanced.</p><pre>.button {<br>  font-size: 1em;<br>  padding: 0.5em 1em;<br>}</pre><p>👍 This works well if the button is used in a controlled context, like within a consistent layout or component system.</p><p>But problems start when you reuse that button in containers with different font sizes:</p><pre>.footer {<br>  font-size: 0.8em;<br>}</pre><pre>&lt;div class=&quot;footer&quot;&gt;<br>  &lt;button class=&quot;button&quot;&gt;Click Me&lt;/button&gt;<br>&lt;/div&gt;</pre><p>Now, your .button font size = 0.8em of the root, so all its padding (which is also in em) shrinks too. This might make it look cramped or inconsistent compared to the same button used in other sections.</p><p>❗️ <strong>The issue isn’t using </strong><strong>em in padding itself—it&#39;s using it when the parent font size isn’t consistent across layouts.</strong></p><p>✅ <strong>Safer approach</strong>: Use rem for layout and spacing, and reserve em only when you <em>want</em> a component to scale with text size.</p><pre>.button {<br>  font-size: 1em;       // Text adapts to parent<br>  padding: 0.5rem 1rem; // Padding stays visually consistent<br>}</pre><p>This way, your spacing remains consistent, but you still have room to adjust font size if needed.</p><p>4. <strong>Tooltips and Overlays Break Out of Bounds</strong></p><p>Imagine you’re designing a tooltip component:</p><pre>.tooltip {<br>  font-size: 1em;<br>  padding: 1em;<br>  width: 20em;<br>}</pre><p>Now place it inside a scaled container:</p><pre>.card {<br>  font-size: 1.5em;<br>}</pre><p>Inside .card, that 1em = 24px, not 16px. So:</p><ul><li>Padding = 24px</li><li>Width = 20 * 24 = 480px</li></ul><p>😬 Surprise! Your tooltip is now oversized and may overflow or misalign.</p><p>✅ Use rem instead:</p><pre>.tooltip {<br>  font-size: 1em; // use em here to inherit the font size of the parent<br>  padding: 1rem;<br>  width: 20rem;<br>}</pre><p>Now the size and spacing are <strong>predictable</strong>, no matter the context it’s used in.</p><h3>5. Design Tokens and Spacing Scales Become Useless</h3><p>Design systems often rely on spacing scales: 4px, 8px, 16px, etc. When you base all your margins and paddings on em, those values can quickly deviate:</p><pre>.card { <br>  margin: 2em;<br>}</pre><p>You might <em>think</em> this is 32px, but it’s only 32px if the font size remains 16px. If it changes to 18px or 20px somewhere up the tree, your 2em now equals 36px or 40px.</p><p>This throws off vertical rhythm, component alignment, and makes tokens irrelevant.</p><p>✅ <strong>Use rem-based spacing tokens</strong>:</p><pre>:root {<br>  --space-m: 1rem;<br>  --space-l: 2rem;<br>}<br>.card {<br>  margin: var(--space-l);<br>}</pre><p>This makes your layout system <strong>modular and resilient</strong>.</p><h3>Final Thought</h3><p>The flexibility of em is both its strength and its greatest weakness. It’s great for text-relative sizing <strong>within components</strong>, but it becomes chaotic when applied to layout, spacing, or any context-sensitive component.</p><p>If you want <strong>predictable, scalable, and accessible design</strong>, make rem your layout unit—and reserve em for local, text-related adjustments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6acb279d479" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>