Foolproof Flexbox: Fixing Flowing Frustrations

I recently encountered a frustrating issue while using flexbox for a 2 column layout on Brush Ninja. One column could have any width, while the other had a fixed width for a skyscraper advert. However, I faced a problem where the flexible-width column would overflow and make the content too wide. This issue only arose after adding the skyscraper advert.

To be honest, this problem caused quite a panic for me. I didn’t realize it was happening until after I published the website to production.

Initially, I considered using CSS grid and minmax to prevent overflows. However, I wanted my layout to dynamically adjust and resize when removing the advertisement. Flexbox seemed like a better fit for my needs.

After some trial and error, I stumbled upon the fix for flexbox that was closely related to using minmax. Adding min-width:0 did the trick! Initially, I tried adding max-width, but that didn’t restrict the size.

It turns out that by default, HTML elements have min-width:0, unless you’re using flexbox or CSS grid - in which case it is set to min-width:auto. This auto value allows items to overflow their containers. By explicitly setting min-width:0 on the columns we want to contain, we can resolve this issue once and for all.

Understanding Why It Works

When using flexbox or CSS grid layouts, elements are given an automatic minimum width (min-width:auto). This means they can exceed their container’s boundaries if necessary. While this behavior can possibly be useful in certain scenarios (I have yet to think of any), it’s a problem when we want our content to stay within specific limits.

By adding min-width:0 to our desired columns within flexbox, we override the default behavior and ensure that they do not overflow. This simple adjustment enables our content to remain within the designated width, even if we dynamically remove or resize elements.

I’m thinking about changing my CSS framework, ElementalCSS, to force this behavious since I can’t think of a reason why the default it preferable. Alternatively I might add a generic class to opt in to this behaviour.

Putting It Into Practice

To implement this solution in your own flexbox layout, follow these steps:

  1. Identify the column(s) where you want to prevent overflowing.
  2. Add the CSS rule min-width:0 to those column(s).

For example, let’s say you have a container with two columns, and you want only the first column to be flexible while preventing it from overflowing:

.container {
	display: flex;

.flexible-column {
	min-width: 0;

By applying min-width:0 specifically to the flexible-column, you can ensure that its content stays within bounds and doesn’t cause any frustrating overflows.

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

20 Feb 2014

Fixing CSS Transitions in Google Chrome

I had a weird problem yesterday where I had applied a css transition to an element that was fading from opacity:0.1 to opacity:1 – and along the way it seemed to change in size or position.The reason for the change...
29 May 2020

Does WordPress Need a Grid System?

Justin, from WPTavern, has recently posted an article considering whether WordPress needs a grid system. As a designer, I am inclined to say yes.I have been using the Layout Grid plugin, that Justin mentions, for a couple of months now...
29 Nov 2012

CSS Highlight Colour

When I design a website I really like to add small touches that most people won’t notice. In fact there’s a blog I follow that is dedicated to these small areas of delight. One of the things I like to...
25 Dec 2012

Learning to Accept CSS3: Creating CSS3 Windows

People are resistant to change. It’s strange since I consider myself quite forward thinking, but I still find it takes me a while to accept new things. I’ve been using CSS3 effects for quite a while now – as have...
11 Nov 2012

Clearing CSS Floats Without Markup

This has done the rounds loads of times before – but clearing css floats is something I need to do all the time, and since I have to look up the code on every project I thought I would add...