To utilize javascript/jQuery for this task, the following pseudo code logic can be followed:
Begin by creating a function that executes the following steps....
Step 1: Eliminate any div elements in the document with a class called fakeSquare
. This can be achieved like so:
$('.fakeSquare').remove();
Step 2: Determine the quantity of red divs in a single row. Save this value as squaresPreRow
. An example calculation could resemble:
var squaresPreRow = floor( window width / square width )
.
Step 3: After the squaresPreRow + 1
red square divs, include two empty divs. For instance...
$("div.redSquare")
.index(squaresPreRow + 1)
.append("<div class="fakeSquare redSquare"></div><div class="fakeSquare redSquare"></div>");
Step 4: Integrate an additional two squares for the third row...
$("div.redSquare")
.index((squaresPreRow * 2) + 1)
.append("<div class="fakeSquare redSquare"></div><div class="fakeSquare redSquare"></div>");
Step 5: Repeat the process once more...
$("div.redSquare")
.index((squaresPreRow * 3) + 1)
.append("<div class="fakeSquare redSquare"></div><div class="fakeSquare redSquare"></div>");
Lastly, ensure to invoke this function when the DOM is loaded and whenever the window dimensions change.
Adjustments may be necessary, but this outline should provide a starting point.