3

I have two rows: one with 4 buttons and one with 3 buttons.

I want the second row's first column to match with the first row's second column in flexbox.

Here is my code, any help is appreciated.

<div> 
  <div style="display: flex"> 
    <button style="flex: 1"> A </button> 
    <button style="flex: 1"> B </button>
    <button style="flex: 1"> C </button> 
    <button style="flex: 1"> D </button> 
  </div> 
  <div style="display: flex"> 
    <button style="flex: 2"> B </button>
    <button style="flex: 1"> C </button> 
    <button style="flex: 1"> D </button> 
  </div> 
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Patrick Klitzke
  • 1,519
  • 2
  • 14
  • 24
  • Did you want the 2nd row button to be that big? If so, are the other 2 buttons going to shrink or wrap to a third row? – zer00ne Apr 05 '16 at 03:56

3 Answers3

6

Try using an invisible flex item in the second row.

For more accurate sizing, use the flex-basis property (similar to width, in this case).

With flex-grow, you'll have a harder time aligning the columns in both rows. flex-grow is more concerned with the distribution of free space than precise sizing of flex items. (More details.)

<div>
  <div style="display: flex">
    <button style="flex: 1 1 25%">A</button>
    <button style="flex: 1 1 25%">B</button>
    <button style="flex: 1 1 25%">C</button>
    <button style="flex: 1 1 25%">D</button>
  </div>
  <div style="display: flex">
    <button style="flex: 1 1 25%; visibility: hidden;">A</button>
    <button style="flex: 1 1 50%">B</button>
    <button style="flex: 1 1 12.5%">C</button>
    <button style="flex: 1 1 12.5%">D</button>
  </div>
</div>

jsFiddle

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
1

May not be the right solution, but a suggestion/work around !!

Suggestion 1:

<div>
  <div style="display: flex">
    <button style="flex: 1">A</button>
    <button style="flex: 1">B</button>
    <button style="flex: 1">C</button>
    <button style="flex: 1">D</button>
  </div>
  <div style="display: flex">
    <button style="flex: 1;visibility: hidden">B</button>
    <button style="flex: 1">B</button>
    <button style="flex: 1">C</button>
    <button style="flex: 1">D</button>
  </div>
</div>

Suggestion 2:

<div>
  <div style="display: flex">
    <button style="flex: 1">A</button>
    <button style="flex: 1">B</button>
    <button style="flex: 1">C</button>
    <button style="flex: 1">D</button>
  </div>
  <div style="display: flex">
    <button style="flex: 1; margin-left: 25%">B</button> <!-- 25% = 100/(No. of buttons in previous row) -->
    <button style="flex: 1">C</button>
    <button style="flex: 1">D</button>
  </div>
</div>
Pugazh
  • 9,453
  • 5
  • 33
  • 54
1

Well I'm afraid this is going to be downvoted, but I read the arguments for/against using tables for layout. I came here after reading those arguments... one of which is "less code", however in this case it looks like possibly more code which is also more complicated to understand.

I also struggled to get columns aligned across multiple rows.

I read the accepted answer, looked at the other responses, then went back, removed my div's and used a table.

There are situations where a table is just plain old easier.

Iannazzi
  • 1,348
  • 2
  • 18
  • 27