Corresponding JS Fiddle http://jsfiddle.net/arosen/FMQtR/
Problem
My HTML looks something like this:
<div id='parent'> <div id='one'> A variable amount of text here. </div> <div id='two'> A less important variable amount of text here. </div> </div>
#parent div is a fixed height and cannot be changed. Inside it, I have at least two child divs. The first (or many) will have an unknown amount of text in it, determining its height. Based on the content of the first, I want the latter to occupy the same height as in the parent, but not overflow.
My current CSS example:
#parent { border: 1px solid #000; height: 150px; width: 150px; } #one, #two { border: 1px dashed #333; height: auto; margin: 5px; padding: 5px; overflow: hidden; }
My current JS solution
function() { var $two = $('#two'); var $parent = $('#two').parent() $parent.css('overflow', 'hidden'); var heightDifference = $parent[0].scrollHeight - $parent.height(); $two.css('height', $two.height() - heightDifference); }
I am wondering if there is a CSS layout or HTML solution to this problem, or should I use the JS solution that I have in the script that runs when the last button is clicked.
EDIT My JS script has been updated, since the text does not change once on the page, but depending on the information downloaded from the server, it will not know how much text it will have until the page is displayed.
EDIT 2 Only modern (and IE 9) browsers must be supported.
EDIT 3 The final div must be tall as it is used by other jQuery plugins.
Aaron
source share