1

Fiddle

        window.addEventListener("resize", getSize);

          function getSize(){
              var node = document.getElementById('sample');
              var height = node.style.height;
              console.log('height: ', height);//expect 100px, get nothing
          }

    #sample {
     padding: 20px;
     height: 100px;
     overflow-y: auto;
     border: solid blue;
     margin: 10px 300px 10px 25px;
     position: 'relative'

}

node.style.height is always empty/null. Note that I set the height to 100px. Why? How can I get the css height of the node?

P.Brian.Mackey
  • 41,438
  • 63
  • 228
  • 337

2 Answers2

1

To get the height (without margins) of an element use element.offsetHeight.

To access CSS properties directly you have to use getComputedStyle:

const node = document.getElementById('lorem');
const styles = getComputedStyle(node);
const height = styles.height;
Gianluca Mancini
  • 1,272
  • 9
  • 10
1

You need to use clientHeight which is going to compute CSS height + CSS padding

Client Height JSFiddle Example

Docs

David Strada
  • 155
  • 11