Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex items not wrapping in a column direction container

Tags:

html

css

flexbox

I'd like to use flex-direction:column for a specific layout.

I usually use standard flex-direction:row, so I've got some problems using column. I don't know too much how to control it and didn't find anything useful on google.

I've got a regular UL list and what I want is this:

1 3 5 7

2 4 6

What I currently get is this:

1
2
3
4
5
6
7

My current simplified code is this:

.ul{
  display: flex;
  flex-direction: column;

  li{
    width: 25%;
  }
}
like image 719
Luca Reghellin Avatar asked Dec 27 '16 15:12

Luca Reghellin


People also ask

How do you wrap a column in Flex?

Making things wrap If you want to cause them to wrap once they become too wide you must add the flex-wrap property with a value of wrap , or use the shorthand flex-flow with values of row wrap or column wrap . Items will then wrap in the container.

How do I align Flex items in columns?

The flex columns can be aligned left or right by using the align-content property in the flex container class. The align-content property changes the behavior of the flex-wrap property. It aligns flex lines. It is used to specify the alignment between the lines inside a flexible container.

When the flex direction is column-reverse?

The flex container's main-axis is the same as the block-axis. The main-start and main-end points are the same as the before and after points of the writing-mode. column-reverse. Behaves the same as column but the main-start and main-end are opposite to the content direction.


1 Answers

Apply total height on ul and make it wrap.

Then apply flex-basis on ul li (which is height because we've applied flex-direction: column) to 50%. Like:

ul {
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  height: 100px;
}
ul li {
  flex-basis: 50%; /* which in this case will be '50px' */
}

Have a look at the snippet below:

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  height: 100px;
}
ul li {
  flex-basis: 50%;
}
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
</ul>

Hope this helps!

like image 54
Saurav Rastogi Avatar answered Sep 28 '22 08:09

Saurav Rastogi