DEV Community

aronsantha
aronsantha

Posted on

1

> From flexbox 101 to flex-grow 9999

I love responsive design - making sure my app can scale across all screen sizes. But here's the thing: I also want to avoid media queries whenever possible. Why? Because breakpoint are too rigid, too "hardcoded".


Flexbox 101

So, the other day I had to place two DIVs next to each other - if they fit. And if they don't, then stack them on top of each other.

Easy: Apply display: flex and flex-wrap: wrap on their parent container.

Okay, now how to make sure they fill in the available room both when they are inline, and also when they are stacked?

Add flex-grow: 1 to both DIVs.


The trick:

So far, it's been Flexbox 101. But here comes the tricky part: I want DIV2 to expand as wide as it can, and shrink DIV1 to its minimum.

Removing flex-grow from DIV1 would solve this issue when the DIVs are in a row, but it would prohibit DIV1 from growing when they are stacked.

So what do we do?

Change flex-grow: 1 to flex-grow: 9999 on DIV2.


Now, the two DIVs behave as required:

  • If they fit next to each other: they fill up the container's width, with DIV2 getting priority to take as much room as possible, shrinking DIV1 to its minimum width.
  • If they don't fit in a row: the DIVs wrap under one another, and they each expand to the container's width.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more