Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change order of elements in a flexbox column?

Tags:

html

css

flexbox

When elements are sorted by column, they go down the left column and then down the right.

How do you configure flexbox to display elements going down vertically rather than in columns?

e.g. I want the bottom configuration here, not the top:

enter image description here

I've looked into flex-direction with no success. Is this even possible with flexbox?

I have this currently:

#flex-container {
  background-color: #000;
  width: 100%;
  -webkit-column-count: 2;
  -moz-column-count: 2;
  column-count: 2;
  text-align: center;
}
.flex-element {
  display: inline-block;
  width: 100%;
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  break-inside: avoid;
  -webkit-column-gap: 10px;
  margin-bottom: 10px;
  background-color: #fff;
}
<div id="flex-container">
  <div class="flex-element">
    <p>1</p>
  </div>
  <div class="flex-element">
    <p>2</p>
  </div>
  <div class="flex-element">
    <p>3</p>
  </div>
  <div class="flex-element">
    <p>4</p>
    <p>--</p>
  </div>
  <div class="flex-element">
    <p>5</p>
    <p>--</p>
    <p>--</p>
  </div>
  <div class="flex-element">
    <p>6</p>
  </div>
  <div class="flex-element">
    <p>7</p>
  </div>
</div>

https://jsfiddle.net/ckecz95r/

like image 269
user3420034 Avatar asked Oct 13 '15 18:10

user3420034


People also ask

What flexbox property changes the order of the Flex items?

The order property and accessibility Use of the order property has exactly the same implications for accessibility as changing the direction with flex-direction . Using order changes the order in which items are painted, and the order in which they appear visually.

Can we change the order of Flex items using bootstrap?

Use justify-content utilities on flexbox containers to change the alignment of flex items on the main axis (the x-axis to start, y-axis if flex-direction: column ). Choose from start (browser default), end , center , between , or around . Responsive variations also exist for justify-content .

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.


1 Answers

It seems that what you're looking for is a row alignment with wrapping after every two items. This will create a column with two items per row, and the numbering will go left-right, left-right, etc., instead of down one column, then down the next column.

First, make the div a flex container:

#flex-container {
    display: flex;          /* create flex container */
    flex-direction: row;    /* default rule; can be omitted */
    flex-wrap: wrap;        /* overrides default nowrap */
    width: 100%;
}

Then, specify that each flex item should cover 50% of the container width:

.flex-element {
    flex-basis: calc(50% - 20px);  /* take 50% width less margin */
    align-self: flex-start;        /* disable stretch default height */
    margin: 10px;                  /* for demo purposes */
    background-color:#fff;         /* for demo purposes */

Revised Demo

like image 119
Michael Benjamin Avatar answered Oct 19 '22 23:10

Michael Benjamin