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>
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.
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).
Your need to think:
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>
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:
But really, I think you want table layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With