offsetWidth, clientWidth, scrollWidth and Height, respectively in CSS
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:
Recommended Posts:
- HTML | DOM scrollWidth Property
- HTML | DOM offsetWidth Property
- HTML | DOM clientWidth Property
- p5.js | height variable
- CSS | max-height Property
- CSS | height Property
- How to change the height of br tag?
- How to get the <div> height using JavaScript ?
- CSS | Height and Width
- CSS | min-height Property
- HTML | <img> height Attribute
- CSS | line-height Property
- HTML | <th> height Attribute
- HTML | height Attribute
- How to get the width and height of an image ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.