I'm trying to create a layout with elements (input fields in this case), where the user can add as many of them as they want, and also remove them. The elements go into columns and each column can have a maximum of 4 elements. Here's a simple version, without the column functionality https://jsfiddle.net/khfpp2vL/2/
If there's 4 elements, it should look like this:
[1]
[2]
[3]
[4]
If a fifth element is added, it should be in its own column:
[1][5]
[2]
[3]
[4]
And with more elements:
[1][5][9]
[2][6][10]
[3][7]
[4][8]
And so on.
I can't create wrappers and have four elements each, because if elements are removed from the middle, the ones that are left must be able to switch to another column. For example if I remove 3 and 6 from the previous example, it would change to
[1][7]
[2][8]
[4][9]
[5][10]
I've tried css columns, something like this http://jsfiddle.net/NJ4Hw/ (not created by me). But the problem is that I can't control the amount of elements in each column. I can change the column count with javascript when the amount of elements is divisible by 4, so I'll have the right amount of columns, but I you for example remove everything but 9 elements in the fiddle and change the column count to three, it will divide them equally like this:
[1][4][7]
[2][5][8]
[3][6][9]
And I need it to still have 4 elements in the first columns:
[1][5][9]
[2][6]
[3][7]
[4][8]
I also thought of a solution where the parent has a max-height, and only four elements would fit in it vertically, but I couldn't figure out how to arrange them like that. For example if I use floats, it will float 1 and 2 next each other when the space runs out.
So I'm not sure what to try next. Flexbox? Some kind of Javascript solution? Or is there some super simple solution that I haven't thought of?