In the following example, I have created a snippet that demonstrates a simple way of maintaining equal heights for elements with a stepped height.
Todo: Modify the script to dynamically set the element or CSS instead of using fixed heights. If you want the height to be recalculated on resize, you will also need to subscribe to the resize event.
function adjustHeight () {
var scriptTag = document.scripts[document.scripts.length - 1];
var parentTag = scriptTag.previousElementSibling;
var childrenTags = parentTag.children;
for (var i = 0; i < childrenTags.length; i++) {
var childTag = childrenTags[i];
childTag.style.height = "auto";
childTag.style.height = (Math.ceil(parseInt(childTag.clientHeight) / 400) * 400) + "px";
}
}
adjustHeight();
(function() {
window.addEventListener("resize", resizeThrottler, false);
var resizeTimeout;
function resizeThrottler() {
if ( !resizeTimeout ) {
resizeTimeout = setTimeout(function() {
resizeTimeout = null;
resizeHandler();
}, 66);
}
}
function resizeHandler() {
adjustHeight();
}
}());
html, body {
margin: 0;
padding: 0;
}
.wrapper {
overflow: hidden;
box-sizing: border-box;
}
.wrapper div {
width: 30%;
margin-right: 5%;
float:left;
border: 1px solid #ebebeb;
padding: 20px;
box-sizing: border-box;
height: 400px;
}
.wrapper div:last-of-type {
margin-right: 0;
}
<div class="wrapper">
<div>Lorem Ipsum 123 123...</div>
<div>Lorem Ipsum 123 123...</div>
<div>Lorem Ipsum 123 123...</div>
</div>
If you prefer all the boxes to have equal heights, the following version calculates the height on the "wrapper" instead of looping through each element.
function adjustHeight () {
var scriptTag = document.scripts[document.scripts.length - 1];
var parentTag = scriptTag.previousElementSibling;
parentTag.style.height = "auto";
parentTag.style.height = (Math.ceil(parseInt(parentTag.clientHeight) / 400) * 400) + "px";
}
adjustHeight();
(function() {
window.addEventListener("resize", resizeThrottler, false);
var resizeTimeout;
function resizeThrottler() {
if ( !resizeTimeout ) {
resizeTimeout = setTimeout(function() {
resizeTimeout = null;
resizeHandler();
}, 66);
}
}
function resizeHandler() {
adjustHeight();
}
}());
html, body {
margin: 0;
padding: 0;
}
.wrapper {
overflow: hidden;
height: 400px;
box-sizing: border-box;
}
.wrapper div {
width: 30%;
margin-right: 5%;
float:left;
border: 1px solid #bbb;
padding: 20px;
box-sizing: border-box;
height: 100%;
}
.wrapper div:last-of-type {
margin-right: 0;
}
<div class="wrapper">
<div>Lorem Ipsum 123 123...</div>
<div>Lorem Ipsum 123 123...</div>
<div>Lorem Ipsum 123 123...</div>
</div>