Open In App

offsetWidth, clientWidth, scrollWidth and Height, respectively in CSS

Improve
Improve
Like Article
Like
Save
Share
Report

offsetWidth: It returns the width of an HTML element including padding, border and scrollbar in pixels but it does not include margin width. If the element does not have any associated layout box then it returns zero.

Syntax:

element.offsetWidth

clientWidth: It returns the width of an HTML element including padding in pixels but does not include margin, border and scrollbar width.

Syntax:

element.clientWidth

scrollWidth: It returns the width of the content enclosed in an html element including padding but not margin, border and scroll bar.

Syntax:

element.scrollWidth

Example: This example illustrates the use of offsetWidth, clientWidth and scrollWidth property.




<!DOCTYPE html>
<html>
      
<head>
    <title>
        Use of offsetWidth, ClientWidth
        and scrollWidth property
    </title>
  
    <style>
        #box {
            height: 100px;
            width: 130px;
            border: 5px black;
            padding: 10px;
            margin: 5px;
            overflow: scroll; 
            background-color: aqua;
        }
    </style>
</head>
  
<body>
    <div id="box">
        It is an example of offsetWidth, ClientWidth
        and scrollWidth property
    </div>
      
    <p>Click on button to get result</p>
      
    <button onClick="display()">
        Click Here!
    </button
      
    <div id="result"></div>
      
    <!-- Script to uses offsetWidth, ClientWidth
        and scrollWidth property -->
    <script>
        function display() {
            var ele = document.getElementById("box");
            var osw = ele.offsetWidth;
            var sw = ele.scrollWidth;
            var cw = ele.clientWidth;
              
            document.getElementById("result").innerHTML
                = "offsetWidth: " + osw + "px<br>clientWidth: "
                  + cw + "px<br>scrollWidth : " + sw + "px" ;
        }
    </script>
</body>
  
</html>                    


Output:
Before Clicking the button:

After Clicking the button:

offsetHeight: It returns the height of an HTML element including padding, border and scrollbar in pixels but does not include margin height. If the element does not have any associated layout box then it returns zero.

Syntax:

element.offsetHeight

clientHeight: It returns the height of an HTML element including padding in pixels but does not include margin, border and scrollbar height.

Syntax:

element.clientHeight

scrollHeight: It returns the height of the content enclosed in an html element including padding but not margin, border and scroll bar.

Syntax:

element.scrollHeight

Example: This example illustrates the use of offsetHeight, clientHeight and scrollHeight property.




<!DOCTYPE html>
<html>
      
<head>
    <title>
        Use of offsetHeight, ClientHeight
        and scrollHeight property
    </title>
  
    <style>
        #box {
            height: 100px;
            width: 150px;
            border: 5px black;
            padding: 10px;
            margin: 5px;
            overflow: scroll; 
            background-color: aqua;
        }
    </style>
</head>
  
<body>
    <div id="box">
        It is an example of offsetHeight, ClientHeight
        and scrollHeight property
    </div>
  
    <p>Click on button to get result</p>
      
    <button onClick="display()">
        Click Here!
    </button
      
    <div id="result"></div>
  
    <script>
        function display() {
            var ele = document.getElementById("box");
            var osw = ele.offsetHeight;
            var sw = ele.scrollHeight;
            var cw = ele.clientHeight;
              
            document.getElementById("result").innerHTML
                = "offsetHeight: " + osw + "px<br>clientHeight: "
                  + cw + "px<br>scrollHeight: " + sw + "px" ;
        }
    </script>
</body>
  
</html>                    


Output:
Before Clicking the button:

After Clicking the button:



Last Updated : 28 Mar, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads