How do I retrieve an HTML element's actual width and height?

前端 未结 14 1951
后悔当初
后悔当初 2020-11-22 02:29

Suppose that I have a

that I wish to center in the browser\'s display (viewport). To do so, I need to calculate the width and height of the &l
14条回答
  •  天命终不由人
    2020-11-22 03:00

    It is easy to modify the elements styles but kinda tricky to read the value.

    JavaScript can't read any element style property (elem.style) coming from css(internal/external) unless you use the built in method call getComputedStyle in javascript.

    getComputedStyle(element[, pseudo])

    Element: The element to read the value for.
    pseudo: A pseudo-element if required, for instance ::before. An empty string or no argument means the element itself.

    The result is an object with style properties, like elem.style, but now with respect to all css classes.

    For instance, here style doesn’t see the margin:

    
      
    
    
    
      
    
    
    

    So modified your javaScript code to include the getComputedStyle of the element you wish to get it's width/height or other attribute

    window.onload = function() {
    
        var test = document.getElementById("test");
        test.addEventListener("click", select);
    
    
        function select(e) {                                  
            var elementID = e.target.id;
            var element = document.getElementById(elementID);
            let computedStyle = getComputedStyle(element);
            var width = computedStyle.width;
            console.log(element);
            console.log(width);
        }
    
    }
    

    Computed and resolved values

    There are two concepts in CSS:

    A computed style value is the value after all CSS rules and CSS inheritance is applied, as the result of the CSS cascade. It can look like height:1em or font-size:125%.

    A resolved style value is the one finally applied to the element. Values like 1em or 125% are relative. The browser takes the computed value and makes all units fixed and absolute, for instance: height:20px or font-size:16px. For geometry properties resolved values may have a floating point, like width:50.5px.

    A long time ago getComputedStyle was created to get computed values, but it turned out that resolved values are much more convenient, and the standard changed.
    So nowadays getComputedStyle actually returns the resolved value of the property.

    Please Note:

    getComputedStyle requires the full property name

    You should always ask for the exact property that you want, like paddingLeft or height or width. Otherwise the correct result is not guaranteed.

    For instance, if there are properties paddingLeft/paddingTop, then what should we get for getComputedStyle(elem).padding? Nothing, or maybe a “generated” value from known paddings? There’s no standard rule here.

    There are other inconsistencies. As an example, some browsers (Chrome) show 10px in the document below, and some of them (Firefox) – do not:

    
    
    

    for more information https://javascript.info/styles-and-classes

提交回复
热议问题