views:

24

answers:

2

Hi, I try to get the exact used Height of a div element. It should be .outerHeight() but this does not work if the last child of the div has a border bottom. I did a little example:

Html:

<div id="iWantThisHeight" class="box">
<div id="div1" style="">innerDiv1</div>
<div id="div2" style="">innerDiv2</div>
</div>

Css:

.box div{
    height:100px;
    margin-bottom:20px;
}

Code example: http://jsfiddle.net/T3b6r/4/ The used height schould be 240px (2x100px for height + 2x20px margin) but the last margin-bottom is ignored. (Margin-top is ignored completly by the way)

Did i oversee something or do i have to check each element for their margins, to get the space used by the div?

A: 

Are you using .outerHeight(true) ? Without the boolean true, margins will not be included.

danielgwood
yes i do, but still ignores the margins from the children
XzenTorXz
I am not quite sure how outerHeight() works, but its possible the final margin-bottom doesn't count because the parent element has already ended - if I inspect the jsfiddle example, Chrome reports the parent height finished at 220px, and the final margin-bottom doesn't count.To force it to count, you could add a line break or similar.
danielgwood
@daniel: that looks like soem weird box model issue, doens't it? Chrome computes it differently whether or not there is a border: http://imgur.com/Voecr.png Might this be resolved by specifying a doctype?
David Hedlund
+2  A: 

If you wrap the container div in a border it returns the height as desired. Just use the same color border as the backdrop it will be against.

See http://jsfiddle.net/T3b6r/7/

Robert
a border wouldn't fit my design. but you hit me on an idea: "display: inline-block;" http://jsfiddle.net/T3b6r/8/
XzenTorXz