I am trying to create a dynamic grid layout with two columns that is #red-container
and #green-container
. Each container or column should be resizable horizontally and the main container #container
should be resizable vertically. Resizing the container should adjust the height of the child elements. The #red-container
and #green-container
have cells which are added dynamically to the container with the JavaScript function in a row-wise fashion. I have the following working implementation:
function addCell(color) {
let container = document.getElementById(color + "-container");
let cell = document.createElement("div");
cell.innerText = color;
container.appendChild(cell).className = "container-item";
}
#container {
border: 1px solid #ddd;
display: flex;
flex-grow: 1;
flex-direction: row;
height: 50vh;
resize: vertical;
overflow: auto;
}
#red-container {
flex-grow: 1;
display: grid;
grid-auto-flow: row;
margin: 10px;
resize: horizontal;
overflow: auto;
border: 1px solid red;
}
#green-container {
flex-grow: 1;
display: grid;
grid-auto-flow: row;
margin: 10px;
resize: horizontal;
overflow: auto;
border: 1px solid green;
}
.container-item {
padding: 10px;
border: 1px solid black;
}
<button onclick="addCell('red')">add red cell</button>
<button onclick="addCell('green')">add green cell</button>
<div id="container">
<div id="red-container"></div>
<div id="green-container"></div>
</div>
However, there are two problems with it:
- The first time the user clicks
add red cell
it increases the width of#red-container
. - adjusting the width of the red and green container is inconsistent and I am unable to adjust the width of the red container to less than 50% of the width.
Any ideas on how to fix these two problems? Additionally, any advice on writing the css in a more concise manner is welcome (#red-container
and #green-container
contain duplicate code).