I have a div containing several child div elements and I want the child elements' width to stretch so that all the child elements fill the width of the containing div, the parent div. I have done a lot of research online about this but haven't found what I'm looking for. For my case there is a vertical scrolling on the parent div when it's children overflow it vertically. I have seen this being done in Zoom and Google Meets. Although what I have witnessed happen in Zoom and Google Meets doesn't have to do with vertical scrolling. There is no vertical scrolling. I tried doing this with jquery/javascript but could not find out why my code isn't working. The child divs width does not stretch so that all the child divs together fit or cover the parent div's width.
$(document).on("ready", function() {
var child = $(".child").length;
var childWidth = $(".child").width() + 10; /* child width plus 10 for margin right */
var parentWidth = $("#parent").width();
for (var i = 1; i <= child; i++) {
childWidth = childWidth + 210;
/*increment child divs width. divs in first row */
var remainingSpace = parentWidth - childWidth; /* remaining space in first row of child divs */
if (remainingSpace < 210) { /* can't fit another 200px plus 10px margin-right div in first row */
var scalar = remainingSpace / i;
/*divide remaining space by number of divs in the first row */
var newChildWidth = 200 + scalar; /* add scalar to width of all child divs */
$(".child").css("width", newChildWidth + "px"); /* apply new width to all child divs */
return false;
/* stop for iteration because childWidth calculation for first row is complete */
}
}
});
#parent {
width: 100%;
/*100% of window width. Which is variable from device to device*/
height: 100%; /* parent height is 100% of window viewport height */
overflow: auto;
text-align: center;
}
.child {
display: inline-block;
width: 200px;
height: 200px;
margin-right: 10px;
margin-bottom: 10px;
}
<div id="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
I would also accept a pure CSS solution if there are any. Thank you in advance.