I decided to go for it again - not least because it is an exciting problem.
I think I hacked it.
var binaryMatrix = document.getElementById('binaryMatrix'); var binaryMatrixDivs = binaryMatrix.getElementsByTagName('div'); var largestNumberOfRows = 12; for (var i = largestNumberOfRows; i > 0; i--) { if (binaryMatrixDivs.length % i === 0) { var numberOfRows = i; break; } } function changeDivWidth() { for (var i = 0; i < binaryMatrixDivs.length; i++) { var divWidth = (100 / (binaryMatrixDivs.length / numberOfRows)); var divHeight = (100 / numberOfRows); binaryMatrixDivs[i].style.width = divWidth + 'vw'; binaryMatrixDivs[i].style.height = divHeight + 'vh'; binaryMatrixDivs[i].style.lineHeight = divHeight + 'vh'; } } window.addEventListener('load',changeDivWidth,false);
body { margin: 0; padding: 0; } #binaryMatrix { width: 100vw; height: 100vh; } #binaryMatrix div { display: inline-block; float: left; width: 0; height: 0; line-height: 20vh; text-align: center; } #binaryMatrix div:nth-of-type(odd) { background-color: rgb(191,191,191); } #binaryMatrix div:nth-of-type(even) { background-color: rgb(127,127,127); }
<div id="binaryMatrix"> <div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div><div>0</div><div>1</div> </div>
Notes:
1) You can determine the number of rows by changing the value of numberOfRows . If the numberOfRows parameter numberOfRows set to 5 , then there will always be 5 lines - no matter how short or tall or wide or narrow the window is.
2) Obviously, you need numberOfRows be a factor in the number of your numberOfRows . So 5 lines are good if you have 100 div or 90 div, but if you have 96 sections, you need numberOfRows be 4 or 6 or 12 or some other 96 ratio.
Improvement:
The two notes above were necessary because in the first version of the script you need to manually declare the number of lines. In the second version of the script (see above), you can still manually declare the number of lines if you want (but a little less effort), you can simply manually declare the largest number of lines that you want to see (for example, no more than 14 lines), and the script will count from 14 until it finds a coefficient, and the first factor it reaches is numberOfRows .
This will allow you to change the number of partitions (manually or dynamically) without having to reset numberOfRows each time.
source share