<!--
Forum question answer only:
https://www.sitepoint.com/community/t/grid-container-doesnt-fit-content/377861/4
-->
<div id="grid-container">
<div id="content"><span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span></div>
</div>
<div>Another element</div>
#grid-container {
display:grid;
outline: 1px solid green;
overflow:hidden;
}
#content {
background: aqua;
}
(function (d) {
const grid = d.querySelector("#grid-container");
const content = d.querySelector("#content");
var contentHeight = content.offsetHeight / 2;
grid.style.height = contentHeight + "px";
var resizeGrid = debounce(function () {
contentHeight = content.offsetHeight / 2;
grid.style.height = contentHeight + "px";
}, 20);
//
window.addEventListener("resize", resizeGrid);
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this,
args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
})(document);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.