Explain Codes LogoExplain Codes Logo

How to make a div fill the remaining horizontal space?

html
responsive-design
css
layout
Nikita BarsukovbyNikita BarsukovΒ·Dec 10, 2024
⚑TLDR

The quick and smart way is CSS Flexbox: just use display: flex; on the parent container, and let the target div enjoying its freedom with flex-grow: 1;. It's like this:

<div style="display: flex;"> <div style="width: 100px;">Sidebar</div> <div style="flex-grow: 1;">Main content</div> </div>

The second div automatically basks in the glory of the remaining width. Like a king on an empty throne! πŸ‘‘

Dig deeper, knight of code!

While the fast answer is your save-the-day hero, circumstances might call you to wear different caps. Supporting legacy browsers or handling advanced layouts demand more weapons in your armory. Let's sharpen those weapons!

Floats and margins: good old-fashioned charm

Before Flexbox, floats were the MVPs for layout position. Team them up with margin to fill up that remaining space:

.left-div { float: left; /* Float like a butterfly πŸ¦‹ */ width: 100px; /* But sting with a fixed width */ } .right-div { margin-left: 100px; /* Right div grudgingly admits left div's size */ }

Overflow: unleash the beast

Let's talk about the elephant in the room: overflow when you're dealing with floats. The overflow property commands a new Block Formatting Context (BFC) - and the div gracefully expands whilst neatly sidestepping those floated elements:

.right-div { overflow: auto; /* BFC's magic trick πŸŽ©πŸ‡ */ margin-left: 100px; }

Display table: pick up the ancient relic!

For a nostalgic stroll through older browser support, pickup 'display: table;' and pass on 'display: table-cell;' to its descendants. You will end up with something that might smell like Flexbox, but with a vintage aroma:

.table-container { display: table; width: 100%; /* As wide as the horizon πŸŒ… */ } .left-cell { display: table-cell; width: 100px; } .right-cell { display: table-cell; /* Flexible like a gymnast πŸ€Έβ€β™‚οΈ */ }

For an epic showdown with the dragon called "IE", arm the .right-cell with a shield called "auto".

Embrace cross-browser compatibility. In Flexbox we trust, but deliver we must on all platforms.

Comparing Flexbox and Traditional Floats

When Flexbox is your knight in shining armor:

  • Independence: Flex items can stand tall without leaning on their comrades.
  • Adaptiveness: Adaptive screen sizes? Flex items got your back.
  • Synced: Unvarying across different realms (ahem, browsers).

However, the brave old soldier, Floats, still holds the fort:

  • Legacy Support: Compatibility with old browsers? Floats have seen it all.
  • Simplicity: Straightforward layouts need straightforward methods.
  • Familiarity: For those who whizz through traditional CSS layout techniques.

The Grid of Troubles and how to conquer them!

In your heroic jedi-coding journey, you may encounter usual foes:

  • Fight of the Styles: Beware of existing CSS commands trying to pull your layout strings.
  • Case of the Overflow: Employ overflow: hidden or overflow: auto to keep unexpected rebels at bay.
  • Mixed Potion Dangers: Stick to one potion (px, rem, etc.) for foreseeable results.

Never forget, your faithful sidekicks, Chrome DevTools, or Firefox Inspector, are always there to help analyze and conquer battles effectively.