How do you find the dimensions of a “display: none” element?

后端 未结 6 831
轻奢々
轻奢々 2021-02-13 18:58

I have some child elements inside a div that gets the CSS display: none applied to it and I want to find out what the child element dimensions are. How can I do thi

6条回答
  •  野性不改
    2021-02-13 19:24

    Use window.getComputedStyle()

    var o = document.getElementById('output');
    var wmd1 = document.getElementById('whats-my-dims1');
    var wmd2 = document.getElementById('whats-my-dims2');
    o.innerHTML = 'wmd1: "' + window.getComputedStyle(wmd1).getPropertyValue("width") 
    + '", "' 
    + window.getComputedStyle(wmd1).getPropertyValue("height") 
    + '", wmd2: "' 
    + window.getComputedStyle(wmd2).getPropertyValue("width") + '", "' 
    + window.getComputedStyle(wmd2).getPropertyValue("height") + '"';
    #some-hidden-div{
      display: none;
    }
    .whats-my-dims{
      display:block;
      width: 69px;
      height: 42px;
      background-color: #f00;
    }
    Processing... :p
    Sooo... How do I get the width and height of whats-my-dims1?

    jsfiddle https://jsfiddle.net/h9b17vyk/3/

提交回复
热议问题