javascript - How can I set a height, or allow a div to extend to the height of absolutely position elements within? -
i have container div absolutely positioned elements within. elements variable heights. there are, however, set number per row - rows not defined in way in markup.
i unable amend markup in way output library (masonry.js).
how can - using css or javascript (jquery available) - extend height of parent div match of children?
i have fiddle running here showing problem: http://jsfiddle.net/wzx4n/1/ - desired effect blue box 'stretch' height of red boxes. remember though, boxes variable heights, cannot loop through rows , add heights of first column (see example image below). code fiddle below:
css
.container{ position:relative; background:blue; min-height:100px; } .box{ position:absolute; width:75px; height:75px; margin:5px 5px; background:red; border:1px solid black; }
html
<div class="container"> <div style="top:0; left:0px;" class="box"></div> <div style="top:0; left:85px;" class="box"></div> <div style="top:0; left:170px;" class="box"></div> <div style="top:0; left:255px;" class="box"></div> <div style="top:0; left:340px;" class="box"></div> <div style="top:85px; left:0px;" class="box"></div> <div style="top:85px; left:85px;" class="box"></div> <div style="top:85px; left:170px;" class="box"></div> <div style="top:85px; left:255px;" class="box"></div> <div style="top:85px; left:340px;" class="box"></div> <div style="top:170px; left:0px;" class="box"></div> <div style="top:170px; left:85px;" class="box"></div> <div style="top:170px; left:170px;" class="box"></div> <div style="top:170px; left:255px;" class="box"></div> <div style="top:170px; left:340px;" class="box"></div> </div>
looping through each row , working out height of highest element will not work element below may shorter, throwing calculation off. please see example see mean:
please note: other answers question found here not applicable due variable heights on child divs, , because of lack of need pure css solution. such i not believe duplicate question , have searched other similar questions.
you can loop through child elements determine 1 furthest top:
var height = 0; $('.container > div').each(function() { var $this = $(this), bottom = $this.position().top + $this.outerheight(); if(bottom > height) { height = bottom; } });
and set height sum of top of child , height of child.
$('.container').height(height);
Comments
Post a Comment