Is there a convenient way to make a 3-column flexbox layout wrap to a single column WITHOUT passing through a 2 col without a media break?

Viewed 35

So I have a 3 column layout, with content that does not work well with partial wrapping. That is to say:

+----------+----------+----------+
|   THIS   |    IS    |   FINE   |
+----------+----------+----------+

+----------+
|    SO    |
+----------+
|    IS    |
+----------+
|   THIS   |
+----------+

+----------------+---------------+
|      THIS      |       IS      |
+----------------+---------------+
|               NOT              |
+--------------------------------+

+----------------+---------------+
|      NOR       |       IS      |
+----------------+---------------+
|      THIS      |
+----------------+

I'm obviously trying to make it responsive... my issue is trying to persuade it to jump DIRECTLY from the 3 col to the 1 WITHOUT "passing through" 2. Now, I know how to do accomplish this with both nesting columns 1 & 2 into their own flexed container, as well as how to do so with @media breaks... but I'm HOPING someone knows of a secret sauce syntax shorthand, such that even when using a simple, flat structure like

<div id="block1">1</div>
<div id="block2">2</div>
<div id="block3">3</div>

I can achieve

1 2 3

directly to

1
2
3

without the need for the nesting/media break hijinks. To be clear: there's nothing wrong with either, and that's how I'll make it work if need be. But... it just FEELS LIKE there should be a way to handle this directly. Some combination of grow/shrink/basis... SOMETHING. If I'm just wishing on a star here, then hey, thanks for reading anyway, and I'm sorry to wasted your time.

1 Answers

Never mind! Chris Coyer over at css-tricks apparently not only had the same notion, but sat down and worked out the math to do so!

The critical path here appears to be clamp (duh! I totally forgot clamp)!

clamp(100%/(N + 1) + 0.1%, (400px - 100vw)*1000, 100%)

See, when the screen width (100vw) is greater than 400px, (400px - 100vw) results in a negative value, and it gets clamped down to 100%/(N + 1) + 0.1%, which is a positive value. This gives us N items per row.

When the screen width (100vw) is less than 400px, (400px - 100vw) is a positive value and multiplied by a big value that’s clamped to the 100%. This results in one full-width element per row.

Working demo: https://codepen.io/NerdyDeeds/pen/OJvwREp

Related