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

后端 未结 10 1750
悲&欢浪女
悲&欢浪女 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:22

    var el = document.querySelector('div');
    
    var elHeight = el.offsetHeight;
    elHeight += parseInt(window.getComputedStyle(el).getPropertyValue('margin-top'));
    elHeight += parseInt(window.getComputedStyle(el).getPropertyValue('margin-bottom'));
    
    console.log(elHeight);
    

    https://jsfiddle.net/gbd47ox1/

    I think this solution is more readable, but none of the solutions presented account for sizes that aren't pixels... :(

提交回复
热议问题