JSFiddle - React, Tailwind, and code Playground
HTML
<div id="videos" class="container">
<div class="video">Video1</div>
</div>
<button id="addVideoButton">Add Video</button>
CSS
div.container
{
height: 200px;
width: 400px;
overflow: hidden;
background: blue;
display: table;
}
div.video
{
float: left;
background: green;
margin: 5px;
display: table-cell;
}
JavaScript
$(function() {
$('#addVideoButton').click(function() {
var parent = $('#videos')[0];
var parentHeight = $(parent).height();
var parentWidth = $(parent).width();
$('<div class="video">Another Video</video>').appendTo(parent);
layout(parent, parentHeight, parentWidth, $('.video'), 1.5);
});
});
function layout(parent, parentHeight, parentWidth, children, ratio) {
var totalArea = parentHeight * parentWidth;
var elements = children.length;
var height = 0,
width = 0,
area = 0,
cols = 0,
rows = 0;
for (height = parentHeight; height > 0; height--) {
width = height * ratio;
area = width * height * elements;
cols = Math.floor(parentWidth / width);
rows = Math.ceil(elements / cols);
if (area <= totalArea && cols * width < parentWidth && rows * height < parentHeight) {
break;
}
}
$(children).width(width - 10).height(height - 10);
$(parent).width(parentWidth).height(parentHeight);
}