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

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

    qdev wrote a nice solution, however I think offsetHeight is faster and better supported than getBoundingClientRect(). I also used ES6 to reduce the code size.

    /**
     * Returns the element height including margins
     * @param element - element
     * @returns {number}
     */
    function outerHeight(element) {
        const height = element.offsetHeight,
            style = window.getComputedStyle(element)
    
        return ['top', 'bottom']
            .map(side => parseInt(style[`margin-${side}`]))
            .reduce((total, side) => total + side, height)
    }
    

提交回复
热议问题