Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get same column width using Flexbox?

Tags:

html

css

flexbox

Is there a way to get same column width on different rows using Flexbox ?

I would like to have the same width for cell--id (The width must have the width of the largest cell--id).

Example:

<div class="container">
  <div class="row">
    <div class="cell cell--id">1</div>
    <div class="cell">Test</div>
  </div>
  <div class="row">
    <div class="cell cell--id">2222</div>
    <div class="cell">Test</div>
  </div>
</div>

CSS :

<style>
   .row { display: flex }
</style>
like image 952
Steffi Avatar asked Sep 17 '15 09:09

Steffi


People also ask

How do I fix the width on my Flexbox?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.

How do I make columns equal width in CSS?

The CSS to make all the columns equal in width is as follows. The table-layout: fixed; rule specifies that the table is to take its dimensions from the <table> element's width and the width of the first row of cells (or the <col> elements, if you have them).


2 Answers

Your need to think:

  • in rows of one or more columns => row is the flexible container
  • or in columns of one or more rows => column is the flexible container
  • the contents are flexed items (and can be nested flexible containers)

In your case you need to give .row a width and make flexbox split the cells evenly over the row:

.row  { display: flex; width: 300px }
.cell { flex: 1 } /* each cell width will be: available space divided by the number of cells */

Ok, here's the POC:

        .row    { display: flex; width: 500px }
        .cell   { flex: 1 }
<div class="container">
  <div class="row">
    <div class="cell cell--id">1</div>
    <div class="cell">Test</div>
  </div>
  <div class="row">
    <div class="cell cell--id">2222</div>
    <div class="cell">Test</div>
  </div>
  <div class="row">
    <div class="cell cell--id">2222222222222</div>
    <div class="cell">Test</div>
  </div>
</div>
like image 75
Rene van der Lende Avatar answered Oct 26 '22 20:10

Rene van der Lende


You're basically describing a table, so you should probably be using table layout instead, e.g.

.row { display: table-row }
.cell { display: table-cell }
<div class="container">
  <div class="row">
    <div class="cell cell--id">1</div>
    <div class="cell">Test</div>
  </div>
  <div class="row">
    <div class="cell cell--id">2222</div>
    <div class="cell">Test</div>
  </div>
</div>

If you're dead set on flexbox, I see two options:

  1. if your rows are going to be the same height, you could swap to column-major - i.e. put your column divs outside your row divs
  2. manually manage the size of the columns, similar to this: http://inlehmansterms.net/2014/10/11/responsive-tables-with-flexbox/

But really, I think you want table layout.

like image 37
CupawnTae Avatar answered Oct 26 '22 20:10

CupawnTae