Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set two flex items side by side in a flexbox column

Tags:

html

css

flexbox

I have some elements I am displaying in a column direction within a display: flex container.

This places the containers within into a stacked view.

Say I have 5 stacked elements I need to make two of them display inline or flex direction row.

Without placing the two elements in question into another div that i can apply flex direction row to...

Is it possible to get these elements to sit side by side?

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

.flex-column .column-item:nth-of-type(4),
.flex-column .column-item:nth-of-type(5) {
  width: calc(50% - 10px);
  display: inline-block;
  float: left;
}

.flex-column .column-item:nth-of-type(4) {
  margin-right: 20px;
}
<div class="flex-column">
  <div class="column-item">a</div>
  <div class="column-item">b</div>
  <div class="column-item">c</div>
  <div class="column-item">d</div>
  <div class="column-item">e</div>
</div>
like image 673
Jason Is My Name Avatar asked Nov 20 '18 12:11

Jason Is My Name


People also ask

How do you align two flex items side by side?

Just use flex-direction: row with flex-wrap: wrap . Then make each element long enough to occupy a full row. Reduce the flex-basis on the elements that are to share a row. With flex-grow: 1 defined in the flex shorthand, there's no need to use calc() .

How do you overlap 2 Flex items?

Flexbox is not designed for z-axis alignment (overlapping). Any overlapping would have to come from negative margins, absolute positioning, CSS Grid Layout, JavaScript or something else. The z-index property may also need to play a role. Cards are made to overlap using line-based placement.

How do you equally divide flex into two columns?

Approach: To create a two-column layout, first we create a <div> element with property display: flex, it makes that a div flexbox and then add flex-direction: row, to make the layout column-wise. Then add the required div inside the above div with require width and they all will come as columns.

How do I display flex items in a column?

You can switch them to display in the block direction for the language of your document by selecting flex-direction: column . Then flex-start will then be where the top of your first paragraph of text would start.


2 Answers

I don't think it is possible once you have given your parent the flex-direction:column; property, alternatively you can enable flex-wrap:wrap and control the width of elements using the flex-basis property. This allows you to achieve the effect you want without altering your html structure.

.flex-column {
    display: flex;
    flex-wrap:wrap;
}

.flex-column .column-item {
 flex-basis:100%;}
.flex-column .column-item:nth-of-type(4),
.flex-column .column-item:nth-of-type(5) {
    flex-basis:50%;
   
    
}
<div class="flex-column">
    <div class="column-item">a</div>
    <div class="column-item">b</div>
    <div class="column-item">c</div>
    <div class="column-item">d</div>
    <div class="column-item">e</div>
</div>
like image 96
Aaron McGuire Avatar answered Sep 25 '22 20:09

Aaron McGuire


The layout is fairly simple with flexbox. You don't need flex-direction: column.

Just use flex-direction: row with flex-wrap: wrap.

Then make each element long enough to occupy a full row.

Reduce the flex-basis on the elements that are to share a row.

.flex-column {
  display: flex;
  flex-wrap: wrap;
}

.column-item {
  flex: 1 0 61%;  /* flex-grow, flex-shrink, flex-basis */
  margin: 2px;
  background-color: lightgreen;
}

.column-item:nth-of-type(4),
.column-item:nth-of-type(5) {
  flex-basis: 40%;
}
<div class="flex-column">
  <div class="column-item">a</div>
  <div class="column-item">b</div>
  <div class="column-item">c</div>
  <div class="column-item">d</div>
  <div class="column-item">e</div>
</div>

With flex-grow: 1 defined in the flex shorthand, there's no need to use calc().

Since flex-grow will consume free space on the row, flex-basis only needs to be large enough to enforce a wrap. In this case, with flex-basis: 61%, there's plenty of space for the margins, but never enough space for a second item.


There's an even simpler and more efficient solution using CSS Grid:

.flex-column {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 5px;
}

.column-item:nth-of-type(-n + 3) {
  grid-column: span 2;
}

.column-item {
  background-color: lightgreen;
}
<div class="flex-column">
  <div class="column-item">a</div>
  <div class="column-item">b</div>
  <div class="column-item">c</div>
  <div class="column-item">d</div>
  <div class="column-item">e</div>
</div>
like image 43
Michael Benjamin Avatar answered Sep 23 '22 20:09

Michael Benjamin