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

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

    old one - anyway... for all jQuery-banning and shortcut folks out there here is some plus scripting which expands the dimension/getabsoluteheight approaches of the other answers:

    function getallinheight(obj) {
      var compstyle=(typeof window.getComputedStyle==='undefined')?obj.currentStyle:window.getComputedStyle(obj);
      var marginTop=parseInt(compstyle.marginTop);
      var marginBottom=parseInt(compstyle.marginBottom);
      var borderTopWidth=parseInt(compstyle.borderTopWidth);
      var borderBottomWidth=parseInt(compstyle.borderBottomWidth);
      return obj.offsetHeight+
             (isNaN(marginTop)?0:marginTop)+(isNaN(marginBottom)?0:marginBottom)+
             (isNaN(borderTopWidth)?0:borderTopWidth)+(isNaN(borderBottomWidth)?0:borderBottomWidth);
    }
    alert(getallinheight(document.getElementById('measureTool')));
    
    0 讨论(0)
  • 2020-12-01 12:04

    If you can use jQuery:

    $('#divId').outerHeight(true) // gives with margins.
    

    jQuery docs

    For vanilla javascript you need to write a lot more (like always...):

    function Dimension(elmID) {
        var elmHeight, elmMargin, elm = document.getElementById(elmID);
        if(document.all) {// IE
            elmHeight = elm.currentStyle.height;
            elmMargin = parseInt(elm.currentStyle.marginTop, 10) + parseInt(elm.currentStyle.marginBottom, 10) + "px";
        } else {// Mozilla
            elmHeight = document.defaultView.getComputedStyle(elm, '').getPropertyValue('height');
            elmMargin = parseInt(document.defaultView.getComputedStyle(elm, '').getPropertyValue('margin-top')) + parseInt(document.defaultView.getComputedStyle(elm, '').getPropertyValue('margin-bottom')) + "px";
        }
        return (elmHeight+elmMargin);
    }
    

    ​ Live DEMO

    code source

    0 讨论(0)
  • 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)
    }
    
    0 讨论(0)
  • 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));
    
    0 讨论(0)
  • 2020-12-01 12:17

    The earlier solutions are probably ideal. In search of an easy way, I came up with something like that. This wraps the target in a div. The height of the div is already calculated and rounded.

    <div style="margin: 0; padding: 0; border: none;">
        <p class="target">something info ex</p>
    </div>
    

    and in JavaScript:

    var height = document.querySelector(".target").parentElement.offsetHeight;
    
    0 讨论(0)
  • 2020-12-01 12:19

    Use all the props, margin, border, padding and height in sequence.

    function getElmHeight(node) {
        const list = [
            'margin-top',
            'margin-bottom',
            'border-top',
            'border-bottom',
            'padding-top',
            'padding-bottom',
            'height'
        ]
    
        const style = window.getComputedStyle(node)
        return list
            .map(k => parseInt(style.getPropertyValue(k), 10))
            .reduce((prev, cur) => prev + cur)
    }
    
    0 讨论(0)
提交回复
热议问题