Languages
[Edit]
EN

JavaScript - element's offsetHeight vs clientHeight

4 points
Created by:
jarlh
635

In this short article, we would like to show, what is difference between HTML element's offsetHeight and clientHeight in JavaScript (under web browser).

Quick answer:

offsetHeight === clientHeight + borderTopWidth + borderBottomWidth
Element's offsetHeight vs clientHeight in JavaScript.
Element's offsetHeight vs clientHeight in JavaScript.

Practical example:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    div.element {
        padding: 5px;
        border: 10px solid gold;
        background: orange;
        width: 200px;
        height: 40px;
    }

  </style>
</head>
<body>
  <div id="element" class="element">Example text inside ...</div>
  <script>

    var element = document.querySelector('#element');

    console.log('clientHeight: ' + element.clientHeight);
    console.log('offsetHeight: ' + element.offsetHeight);
/*
    clientHeight === 40px (internal height) + 2 * 5px (padding size)
    offsetHeight === 40px (internal height) + 2 * 5px (padding size) + 2 * 10px (border size)
                  |
                  v
    offsetHeight === clientHeight + 2 * 10px (border size)
*/
  </script>
</body>
</html>

 

See also

  1. JavaScript - element's offsetWidth vs clientWidth

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join