Full height of a html element (div) including border, padding and margin?

后端 未结 10 1717
悲&欢浪女
悲&欢浪女 2020-12-01 11:42

I need the full height of a div, I\'m currently using

document.getElementById(\'measureTool\').offsetHeight

offsetHeig

10条回答
  •  伪装坚强ぢ
    2020-12-01 12:09

    in Vanilla JS in the function getAllmargin we get the sum of both margin top and bottom and with clientHeight we get the height including all paddings

    var measureTool = document.getElementById('measureTool');
    function getAllmargin(elem){
    //Use parseInt method to get only number
            return parseInt(window.getComputedStyle(elem, null).getPropertyValue('margin-top')) 
            + parseInt(window.getComputedStyle(elem, null).getPropertyValue('margin-bottom'));
    }
    //Finally we get entire height  
    console.log(measureTool.clientHeight + getAllmargin(measureTool));
    

提交回复
热议问题