0

A follow-up post from CSS grid with dynamic number of rows and adjustable width. I am trying to add some content to each container-item dynamically by calling populateCell which is fired when the user either clicks on the populate red cell or populate green cell button. The implementation I have for this is the following:

function addCell(color) {
  let container = document.getElementById(color + "-container");
  
  let cell = document.createElement("div");
  cell.innerText = color;
  container.appendChild(cell).className = "container-item";
}

function populateCell(color) {
  let container = document.getElementById(color + "-container");
  let children = container.children;
  let n = Math.floor(Math.random() * children.length); // random child
  children.item(n).innerHTML = `
        <div class="confined-div" style="height: 100%; width: 100%">
          <fieldset>
            <div style="overflow:auto;">
              placeholder
            </div>
          </fieldset>
          <input style="margin: 2px;">
          <button>Button</button>
        </div>
  `;
}
#container {
  border: 1px solid #ddd;
  display: flex;
  gap: 10px;
  padding: 10px;
  height: 50vh;
  resize: vertical;
  overflow: auto;
}

#green-container,
#red-container {
  display: grid;
  overflow: auto;
}
#red-container {
  border: 1px solid red;
  width: 50%;
  resize: horizontal; 
}
#green-container {
  border: 1px solid green;
  flex-grow: 1;
}

.container-item {
  padding: 10px;
  border: 1px solid black;
}
   
.confined-div {
  display: flex;     
  flex-direction: column;
  min-width: 0;
}
<button onclick="addCell('red')">add red cell</button>
<button onclick="addCell('green')">add green cell</button>
<button onclick="populateCell('red')">populate random red cell</button>
<button onclick="populateCell('green')">populate random green cell</button>

<div id="container">
  <div id="red-container"></div>
  <div id="green-container"></div>
</div>

The problem I have is that the content I am adding, i.e., confined-div, does not respect the width and height of the cells which causes the cells to expand and the content inside confined-div does not shrink to fit. Ideally, I would want the layout to stay the same after populating the cells.

I am looking for a generic solution that works for any type of content, for instance a canvas element:

function populateCell(color) {
  let container = document.getElementById(color + "-container");
  let children = container.children;
  let n = Math.floor(Math.random() * children.length); // random child
  children.item(n).innerHTML = `
        <div class="confined-div">
           <canvas></canvas>
        </div>
  `;
}

Is it possible to specify these kind of constraints? Any suggestions are welcome.

EDIT

Changed style of .confined-div as suggested by Temani.

Kevin
  • 3,096
  • 2
  • 8
  • 37
  • `display: flex; flex-direction: column;` to confined-div ? – Temani Afif Aug 02 '22 at 21:18
  • @TemaniAfif It is better, but I think the cells still get expanded a bit and it adds a horizontal scrollbar. – Kevin Aug 02 '22 at 21:25
  • Also, I think the size of the canvas element still needs to be set explicitly in JavaScript with something similar to `canvas.style.width ='100%'; canvas.style.height='100%';` and `canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight;` probably. – Kevin Aug 02 '22 at 21:30
  • The horizontal scrollbar seems to be due to the `width: 100%; height: 100%` property of the fieldset element. I edited the post with your suggestion. Still, if you increase the height with a large amount and add two cells, when one of the cells get populated it changes the layout slightly. – Kevin Aug 02 '22 at 21:39
  • try min-width:0 to .container-item – Temani Afif Aug 02 '22 at 21:48
  • The min-width property should default to 0 according to [here](https://www.w3schools.com/cssref/pr_dim_min-width.asp). – Kevin Aug 02 '22 at 21:53
  • no, we are talking about grid items and min-width has a different story: https://stackoverflow.com/q/43311943/8620333 – Temani Afif Aug 02 '22 at 22:01
  • @TemaniAfif Could you add your suggestion as an answer? I've edited my question again but I am still getting the same results as I have described in one of my comment above. – Kevin Aug 02 '22 at 23:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246983/discussion-between-kevin-and-temani-afif). – Kevin Aug 02 '22 at 23:38

0 Answers0