473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

node.offsetWidth vs clientWidth vs scrollWidth


for the code

var node = document.getElementById("something")
alert(node.offsetWidth)

is very similar to node.clientWidth and node.scrollWidth

I just wonder why offsetWidth is well documented in the Definitive
Javascript book, but clientWidth and scrollWidth are not mentioned at
all, even when all 3 of them work in IE6, IE 7, Firefox 2, and Safari
3.

Also it seems that scrollWidth is always the same as offsetWidth. I
can't make them different by hiding some part of that element so that
one value is smaller and one is larger.

Thanks very much!

Jun 27 '08 #1
5 21886
On Thu, 24 Apr 2008 11:17:42 +0200, liketofindoutwhy
<li**************@gmail.comwrote:
>
for the code

var node = document.getElementById("something")
alert(node.offsetWidth)

is very similar to node.clientWidth and node.scrollWidth

I just wonder why offsetWidth is well documented in the Definitive
Javascript book, but clientWidth and scrollWidth are not mentioned at
all, even when all 3 of them work in IE6, IE 7, Firefox 2, and Safari
3.

Also it seems that scrollWidth is always the same as offsetWidth. I
can't make them different by hiding some part of that element so that
one value is smaller and one is larger.
I have no intimate knowledge of the subject, but making them all 3
different is no trouble at all:
<html>
<head><title>test</title>
<script type="text/javascript">
function test(elm){
alert('offsetwidth:' + elm.offsetWidth + ', scrollWidth: '
+ elm.scrollWidth + ', clientWidth: ' + elm.clientWidth);
}
</script>
</head>
<body>
<div onclick="test(this)"
style="display:block;width:8em;height:3em;overflow :scroll;">
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaa<br>
</div>
</body>
</html>
Results (FF2)here: 128, 477,112; I suspect, but feel free to correct me:

offsetWidth: width on page
scrollWidth: total width of element from left to right (so in this case
scrollable content).
clientWidth: visible portion of width due to existance of scrollbar.
--
Rik Wasmus
Jun 27 '08 #2
liketofindoutwhy <li**************@gmail.comwrites:
for the code

var node = document.getElementById("something")
alert(node.offsetWidth)

is very similar to node.clientWidth and node.scrollWidth

I just wonder why offsetWidth is well documented in the Definitive
Javascript book, but clientWidth and scrollWidth are not mentioned at
all, even when all 3 of them work in IE6, IE 7, Firefox 2, and Safari
3.
Last time I checked, only the offset* attributes were reliable in all
popular browsers. This may have changed, though. See

http://www.quirksmode.org/viewport/e...imensions.html
Also it seems that scrollWidth is always the same as offsetWidth. I
can't make them different by hiding some part of that element so that
one value is smaller and one is larger.
Scrollwidth isn't about hiding per se, it's about having a content area
wich is scrolled inside an area that's smaller. IOW, it's using
overflow: auto / srcoll.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #3
On Apr 24, 2:35 am, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
I have no intimate knowledge of the subject, but making them all 3
different is no trouble at all:

if it is

<html>
<head><title>test</title>
<script type="text/javascript">
function test(elm){
alert('offsetwidth:' + elm.offsetWidth + ',
scrollWidth: '
+ elm.scrollWidth + ', clientWidth: ' + elm.clientWidth);
}
</script>
</head>
<body>
<div style="display:block;width:8em;height:
3em;overflow:scroll;">
<img onclick="test(this)" src="http://
www.sanrio.co.jp/english/characters/w_chara/pocha180.gif"
</div>
</body>
</html>

then the offsetWidth and scrollWidth is the same... so it looks like
scrollWidth reflects an element with a scroll box, not an element
contained in a scrollbox.

Jun 27 '08 #4
On Apr 24, 3:22 am, liketofindoutwhy <liketofindout...@gmail.com>
wrote:
On Apr 24, 2:35 am, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:

then the offsetWidth and scrollWidth is the same... so it looks like
scrollWidth reflects an element with a scroll box, not an element
contained in a scrollbox.
i mean reflects something different for an element with a scrollbox,
nothing different for an element simply being inside the scrollbox.

Jun 27 '08 #5
On Thu, 24 Apr 2008 12:25:27 +0200, liketofindoutwhy
<li**************@gmail.comwrote:
On Apr 24, 3:22 am, liketofindoutwhy <liketofindout...@gmail.com>
wrote:
>On Apr 24, 2:35 am, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:

then the offsetWidth and scrollWidth is the same... so it looks like
scrollWidth reflects an element with a scroll box, not an element
contained in a scrollbox.

i mean reflects something different for an element with a scrollbox,
nothing different for an element simply being inside the scrollbox.
I'd say that would be so, and for good reason. The width of the image
element is not altered in any way, it just happens to be in a element
which itself is not 'fully extended'.
--
Rik Wasmus
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: BT | last post by:
Hi. I like to know where and how do you set the ClientHeight and ClientWidth properties? I can see Height,Width,Top,Left in the Properties window. Any suggestion would be very helpful. ...
1
by: Robert Oschler | last post by:
I have a "child" document that I use for an IFRAME element that I put into several "parent" documents. These "parent" documents therefore contain the IFRAME whose SRC property is set to the...
1
by: parksch2 | last post by:
I know there are a lot of articles out there about this subject but I have been reading for 3 hours and still can't find the answer. I have some text in a bunch of hidden spans. I need to find the...
3
by: mscir | last post by:
I'm adding text to a div using innerHTML, and watching the width of the div using offsetWidth. In IE the offsetWidth increases when the div gets wider, but in Netscape 7.2 or Firefox 1.0.3 it...
16
by: Joel Byrd | last post by:
I am having this ridiculous problem of trying to set the innerHTML of a div node. It works in all other browsers, but internet explorer is giving me an "Unknown runtime error". This is actually...
3
by: jdlwright | last post by:
Hi, I need to place a DIV exactly over a textarea element, but I can't calculate the correct value that I should set style.width to, because offsetWidth and style.width are calculated differently...
2
by: Noah Sussman | last post by:
Hello, I am writing a function to reposition an element based on whether one of its edges is outside the visible area of the browser window (the code is below). My client has asked for code...
1
by: Beni Rose | last post by:
I'm trying to build some dynamic vertical drop down menus (pop out menus?) and I'm having some trouble. I'm trying to get the width of the span tag that the root level menu item is in, because it's...
0
by: rodchar | last post by:
hey all, i have the following DIV element in my page: <DIV id="OnlyDIV" style="OVERFLOW: hidden; WIDTH: 300px">... This div element contains a datalist. This works fine for me on a regular...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.