Skip to main content

Why using CSS units like em can lead to overflow ?

 Yes, that can definitely happen — let me explain why ๐Ÿ‘‡

๐Ÿง  What em really means

  • 1em = the font size of the element (or its parent).

  • So em is relative, not absolute like px.
    This means its actual value depends on context — i.e., where you use it.


⚠️ Why em can cause overflow

There are 3 main reasons:

1. Nested scaling effect

Each nested element inherits and multiplies the em value.

body { font-size: 16px; } .container { font-size: 2em; /* 2 × 16px = 32px */ } .box { width: 10em; /* 10 × 32px = 320px */ }

Even though you might expect 10em = 160px,
it actually becomes 320px because it’s relative to .container, not body.

๐Ÿ‘‰ If you keep nesting elements using em, it quickly becomes bigger than expected, causing overflow.


2. Font-size scaling increases box size

If you use em for layout properties (like width, padding, or margin) and also increase font-size, those properties scale up too.

Example:

.card { font-size: 20px; width: 10em; /* 200px */ }

But if someone later changes:

.card { font-size: 30px; }

Then width: 10em300px, and the element might now overflow its container.


3. Combining em with limited parent space

If the parent has a fixed width (e.g., 200px) but your child uses em based on a large font-size:

.parent { width: 200px; font-size: 20px; } .child { width: 15em; /* 15 × 20px = 300px > parent width */ }

๐Ÿ’ฅ Boom — overflow! The child is wider than the parent.


๐Ÿ’ก How to avoid overflow

✅ Use rem (root em) for consistent sizing
1rem = font size of root <html> (usually 16px). It doesn’t multiply inside nested elements.

✅ Use percentage (%) for width if you want it to fit the parent.

✅ Use max-width: 100% to prevent elements from exceeding their container.

✅ Be careful mixing font scaling (em) with fixed container sizes.

When you use a value like width: 5em; (or font-size: 5em;, padding: 5em;, etc.), the em unit is relative to the font size of the element (or its parent).

So:

  • 1em = current font size

  • 5em = 5 × current font size

For example:

div { font-size: 16px; width: 5em; /* 5 × 16px = 80px */ }

✅ No overflow if the parent container is large enough.

But if your parent element has a small container width, or if the em unit grows too large (say font-size is 20px, so 5em = 100px), then:

  • The box may become wider than its parent, or

  • Text may overflow horizontally (especially if you also have padding/margin in em units).


Common reasons overflow happens with em

  1. The element’s parent has a small width (e.g., inside a flexbox or grid cell).

  2. The element’s font-size is large, so 5em becomes a large pixel value.

  3. Padding or margin is also in em — making the box even bigger.

  4. You didn’t use overflow: hidden or text-overflow: ellipsis to control it.


How to fix it

You can try:

width: 100%; /* fits parent */ max-width: 100%; /* prevent overflow */ overflow: hidden; /* clip extra content */

Or, use relative units like rem, %, or vw depending on your design.

Comments

Popular posts from this blog

Tailwind css and its corresponding vanilla css

Tailwind Class CSS Property Vanilla CSS Equivalent Description bg-white background-color background-color: #ffffff; Sets background color to white. shadow-sm box-shadow box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05); Adds a small shadow under the element. border-b border-bottom-width border-bottom-width: 1px; Adds a bottom border. Default color: #e5e7eb (gray-200). max-w-7xl max-width max-width: 80rem; /* 1280px */ Restricts container width for large screens. mx-auto margin-left , margin-right margin-left: auto; margin-right: auto; Centers the container horizontally. px-4 padding-left , padding-right padding-left: 1rem; padding-right: 1rem; Adds horizontal padding (16px). sm:px-6 Responsive padding (small screens ≥640px) @media (min-width: 640px) { padding-left: 1.5rem; padding-right: 1.5rem; } Increases padding on small screens. lg:px-8 Responsive padding (large screens ≥1024px) @media (min-width: 1024px) { padding-left: 2rem; paddi...

Rest and spread operator in javascript

  Absolutely ๐Ÿ‘ — here are several practical and clear examples of the rest operator ( ... ) in JavaScript, covering functions, arrays, and objects ๐Ÿ‘‡ ๐Ÿงฎ 1. Rest in Function Parameters When you don’t know how many arguments a function will receive: function multiply ( factor, ...numbers ) { return numbers. map ( n => n * factor); } console . log ( multiply ( 2 , 1 , 2 , 3 , 4 )); // Output: [2, 4, 6, 8] ๐Ÿ‘‰ factor gets the first argument ( 2 ), and ...numbers collects the rest into an array [1, 2, 3, 4] . ๐Ÿง‘‍๐Ÿค‍๐Ÿง‘ 2. Rest with Array Destructuring You can collect remaining array elements into a variable: const fruits = [ "apple" , "banana" , "mango" , "orange" ]; const [first, second, ...others] = fruits; console . log (first); // "apple" console . log (second); // "banana" console . log (others); // ["mango", "orange"] ๐Ÿ‘‰ The rest operator gathers all remaining elements afte...

Role of box-sizing and its attributes in css

  ๐Ÿงฑ Default behavior (content-box) By default, browsers use: box-sizing : content-box; This means: Total element width = content width + padding + border So if you have: .container { width : 300px ; padding : 20px ; border : 5px solid black; } Then the total visible width becomes: 300 (content) + 40 ( left + right padding) + 10 ( left + right border) = 350 px ⚠️ The box becomes wider than 300px , which can cause overflow or layout shifts. ✅ With box-sizing: border-box When you use: box-sizing : border-box; the formula changes to: Total element width = width (including padding + border) So the same CSS now behaves like this: width = 300 px (total) → content area = 300 - ( 40 padding + 10 border) = 250 px ✅ The box stays exactly 300px wide — no overflow. ๐ŸŽฏ Why it’s useful Prevents unexpected overflow due to padding/borders Makes responsive layouts easier Keeps your box sizes consistent You can trust width to be the actu...