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

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

Consider de following markup:

11条回答
  •  我寻月下人不归
    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 =)

提交回复
热议问题