I have been manually generating grid cells in a specific pattern by copying and adjusting <div>
elements. Although this method works, I am interested in creating an algorithm that can automatically generate the desired layout. The left box in the example below illustrates the dynamic calculation I am aiming for.
function createGridCells(){
let cells = '';
for(let a = 1; a <= 10; a++){
for(let b = 1; b <= a; b++){
cells += `<div style="background:#fd8362; grid-area: ${a % b} / ${a % b} / ${a % b} / ${a % b}">
${b}
</div>`
}
}
return cells;
}
document.getElementById("grid-body-algorithm").innerHTML = createGridCells();
#grid-body,
#grid-body-algorithm {
float:left;
margin-right:30px;
box-shadow: 0 0 10px rgba(0, 0, 0, .2);
text-align: center;
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(5, 1fr);
justify-content: center;
align-content: end;
border-radius: 5px;
width:150px;
height:150px;
color:#444444;
padding:1px;
}
#grid-body > *,
#grid-body-algorithm > *{
border:1px solid white;
}
<div id="grid-body">
<div style="background:#fd8362; grid-area: 1 / 1 / 1 / 1">1</div>
<div style="background:#ffad97; grid-area: 2 / 1 / 2 / 1">1</div>
<div style="background:#ffad97; grid-area: 1 / 2 / 1 / 2">2</div>
<div style="background:#ffded6; grid-area: 3 / 1 / 3 / 1">1</div>
<div style="background:#ffded6; grid-area: 2 / 2 / 2 / 2">2</div>
<div style="background:#ffded6; grid-area: 1 / 3 / 1 / 3">3</div>
<div style="background:#fff0ec; grid-area: 4 / 1 / 4 / 1">1</div>
<div style="background:#fff0ec; grid-area: 3 / 2 / 3 / 2">2</div>
<div style="background:#fff0ec; grid-area: 2 / 3 / 2 / 3">3</div>
<div style="background:#fff0ec; grid-area: 1 / 4 / 2 / 4">4</div>
</div>
<div id="grid-body-algorithm">
</div>