Reputation: 189
I have used jQuery code to expand the height of Divs within a container. Here is the JSFiddle link.
However when resizing the browser width the Divs height is fixed and the text overflows the container. Can the following code be changed to expand the container height and Divs within it so that the text stays within?
jQuery code:
$(window).load(function () {
$(window).resize(function () {
$(document).ready(function () {
var heightArray = $(".container>div").map(function () {
return $(this).height();
}).get();
var maxHeight = Math.max.apply(Math, heightArray);
$(".container>div").height(maxHeight);
$(".container>div").height(maxHeight);
});
}).trigger('resize');
});
Style:
.container {
height: auto;
float:left; }
#half {
width:48%;
margin:0.5%;
padding:0.5%;
float:left;
background-color:#1589FF; }
HTML:
<div class="container">
<div id="half">
<h1>About</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales urna non odio egestas tempor. Nunc vel vehicula ante. Etiam bibendum iaculis libero, eget molestie nisl pharetra in. In semper consequat est, eu porta velit mollis nec. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales urna non odio egestas tempor. Nunc vel vehicula ante. Etiam bibendum iaculis libero, eget molestie nisl pharetra in. In semper consequat est, eu porta velit mollis nec. Curabitur posuere enim eget turpis feugiat tempor. Etiam ullamcorper lorem dapibus velit suscipit ultrices. Proin in est sed erat facilisis pharetra.</div>
<div id="half">
<h1>News</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales urna non odio egestas tempor. Nunc vel vehicula ante. Etiam bibendum iaculis libero, eget molestie nisl pharetra in. In semper consequat est, eu porta velit mollis nec. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales urna non odio egestas tempor. Nunc vel vehicula ante. Etiam bibendum iaculis libero, eget molestie nisl pharetra in.
</div>
</div>
Upvotes: 0
Views: 373
Reputation: 5438
You need to use offsetHeight instead of height take a look at this jsfiddle
$(window).load(function () {
$(window).resize(function () {
$(document).ready(function () {
var heightArray = $(".container>div").map(function () {
return $(this).offsetHeight ; //height();
}).get();
console.log(heightArray);
var maxHeight = Math.max.apply(Math, heightArray);
console.log(maxHeight);
$(".container>div").height(maxHeight);
// $(".container>div").height(maxHeight);
});
}).trigger('resize');
});
Upvotes: 1
Reputation: 1471
You are setting a fixed height when resizing the first time so you have to set height auto then resize the divs so they know what the desired size should be. Otherwise the max height you are getting is the fixed height you set the first time.
function resizeDivs() {
var heightArray = $(".container>div").map(function () {
return $(this).height();
}).get();
var maxHeight = Math.max.apply(Math, heightArray);
$(".container>div").height(maxHeight);
$(".container>div").height(maxHeight);
}
$(function () {
resizeDivs();
});
$(window).resize(function () {
$(".container>div").css('height', 'auto');
resizeDivs();
});
jsfiddle
Upvotes: 1
Reputation: 33870
It can be easyly done by add 1 line of code, check this fiddle.
I added that line right when the resize function is called :
$(".container>div").height("");
It will set the height to auto and calcul will reset the right heigth.
Upvotes: 0