Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Flex Column Layout without Specified Height

Just discovered flex today and I'm hoping it'll solve a small visual challenge.

I have a list of items already alphabetically sorted. They all have the same width and, up until now, I've had them floating left. This results in a left-to-right order with wrapping when horizontal space runs out.

What I was hoping to do is have top-down sorting with as many columns as possible with the available width. Seeing as this list is dynamic, the height would be variable. And the height would have to increase as horizontal space is lost (resizing) preventing as many columns.

Given the apparent nature of what flex is trying to accomplish I'd think this would be supported, but thus far I can't figure it out. "display: flex" and "flex-flow: column wrap" seem correct, but it requires a specific height to work, which I can't provide.

Am I missing something?

Edit: I've created a JSFiddle to play with here: https://jsfiddle.net/7ae3xz2x/

ul {
    display: flex;
    flex-flow: column wrap;
    height: 100px;
}
ul li {
    width: 150px;
    list-style-type: none;
    border: 1px solid black;
    text-align: center;
}

If you take the height off the ul, nothing wraps.

It seems the conceptual problem is that "column" flow is all tied to the height of the container instead of to the width, which is what I want. I don't care how tall the area has to be. I care about having as many columns as possible in the available width. Maybe this is just an annoying shortcoming of the flex convention.

like image 985
Rikaelus Avatar asked Aug 13 '15 00:08

Rikaelus


1 Answers

This seems like a job for CSS columns: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Columns

Instead of flex, give the ul a column-width CSS rule:

ul {
  column-width: 150px;
}

Here's a JSFiddle.

like image 132
wiiiiilllllll Avatar answered Sep 17 '22 21:09

wiiiiilllllll