You could accomplish the distribution with CSS and JS only. Here's the HTML code:
<input type="text" id="inputWidth"=> give me width</input>
<input type="text" id="inputHeight"=> give me height</input>
<input type="text" id="inputNum"=> give me number</input>
<input type="text" id="inputContWidth"=> give me container width</input>
<button onclick="addShapes()">Submit</button>
<div id="container">
</div>
And the CSS code for the distribution. This is both to distribute the items and to make them visible:
#container {
position: relative;
background-color: gray;
height: auto;
display: flex;
flex-wrap: wrap;
align-content: center;
justify-content: center;
}
.item {
position: relative;
background-color: blue;
border-style: solid;
}
Then use Javascript to add as many items as needed using the inputs:
function addShapes() {
removeItems();
var inputWidth = document.getElementById("inputWidth").value;
var inputHeight = document.getElementById("inputHeight").value;
var inputNum = document.getElementById("inputNum").value;
var inputContWidth = document.getElementById("inputContWidth").value;
document.getElementById("container").style.width = inputContWidth;
for (var i = 0; i < inputNum; i++) {
var element = document.createElement("div");
element.style.width = inputWidth;
element.style.height = inputHeight;
element.className = "item";
document.getElementById("container").appendChild(element);
}
}
function removeItems() {
var elements = document.getElementsByClassName("item");
while (elements.length > 0) {
elements[0].parentNode.removeChild(elements[0]);
}
}
And here's the codepen: http://codepen.io/gingerdeadshot/pen/JXZNGX