How to get the height of a DIV considering inner element's margins?

前端 未结 11 3176
日久生厌
日久生厌 2021-02-20 04:20

Consider de following markup:

相关标签:
11条回答
  • 2021-02-20 04:29

    This answer may be a little obvious, but if you already know the margin, why not just manually add it to the height?

    0 讨论(0)
  • 2021-02-20 04:33

    I guess you should go throw all element properties and the make a sum only in this way you can know the exactly height... but in the case of the height is set it by for example clear:both property I dont think is posible to know the heigth of an Element.

    A 3 sec thought could be something like:

    var o document.getElementById('id').style;

        var total = ((o.height.split("px")[0] == "") ? 0 : o.height.split("px")[0]) +
     ((o.marginTop.split("px")[0] == "") ? 0 : o.marginTop.split("px")[0]) + 
    ((o.marginBottom.split("px")[0] == "") ? 0 : o.marginBottom.split("px")[0]) + 
    ((o.paddingTop.split("px")[0] == "") ? 0 : o.paddingTop.split("px")[0]) +
        ((o.borderTop.split("px")[0] == "") ? 0 : o.borderTop.split("px")[0]) +
     ((o.borderBottom.split("px")[0] == "") ? 0 : o.borderBottom.split("px")[0])
    

    But I guess you must include also the document.getElementById('id').height value if have it.... is thougth but can help..

    best =)

    0 讨论(0)
  • 2021-02-20 04:36

    Try using clientHeight

    var outerElement = document.getElementById("outerElement");
    if(outerElement.clientHeight) {
    alert("Height is "+outerElement.clientHeight+"px");
    }
    else { alert("Old browser?"); }
    

    I know what you're thinking... "this won't work!" and alas, it doesn't... but if you do something like add a border to outerElement... even for just a moment...

    var outerElement = document.getElementById("outerElement");
    outerElement.style.border = "1px solid black";
    var height = outerElement.clientHeight;
    outerElement.style.border = "none";
    alert("Height is "+height+"px");
    

    Not the most beautiful solution but it works, and if you can figure out why it works (I sure as hell don't know :P) you might be closer to a good solution...

    Some older browsers may not support it though... you'd have to look into it; I can't list 'em.

    0 讨论(0)
  • 2021-02-20 04:36

    If you can set the outer div to style display : inline-block; then scrollHeight will include the margins of child elements.

    It will also work if you set style display : flex; (supported by IE 9+)

    0 讨论(0)
  • 2021-02-20 04:39

    Add clearfix, use jquery height() and remove the clearfix class again.

    That's gonna work. : )

    0 讨论(0)
  • 2021-02-20 04:43

    ok also not pretty but this is how I do it (disclaimer: I stole this from somewhere once)

    var height = elem.clientHeight + getBufferHeight(elem, true);
    
    function getBufferHeight(elem, includeMargins) {
        if (!elem.visible()) {
            return 0;
        }
        //do new Number instead of parseFloat to avoid rounding errors 
        var result = 0;
        if (includeMargins) {
            result =   
                new Number(elem.getStyle('marginTop').replace(/[^\d\.]/g, '')).ceil() +  
                new Number(elem.getStyle('marginBottom').replace(/[^\d\.]/g, '')).ceil();
        }     
        result +=
            new Number(elem.getStyle('borderBottomWidth').replace(/[^\d\.]/g, '')).ceil() +
            new Number(elem.getStyle('borderTopWidth').replace(/[^\d\.]/g, '')).ceil() +    
            new Number(elem.getStyle('paddingTop').replace(/[^\d\.]/g, '')).ceil() +
            new Number(elem.getStyle('paddingBottom').replace(/[^\d\.]/g, '')).ceil();                        
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题