offsetWidth property

all
alt
ch
dir
id
min
rel
top
url
URL
urn
Expand Minimize
This topic has not yet been rated - Rate this topic

Retrieves the width of the object relative to the layout or coordinate parent, as specified by the offsetParent property.

Syntax

JavaScript

p = object.offsetWidth

Property values

Type: Integer

the width, in pixels.

Remarks

You can determine the location, width, and height of an object by using a combination of the offsetLeft, offsetTop, offsetHeight, and offsetWidth properties. These numeric properties specify the physical coordinates and dimensions of the object relative to the object's offset parent.

For more information about how to access the dimension and location of objects on the page through the Dynamic HTML (DHTML) Document Object Model (DOM), see Measuring Element Dimension and Location with CSSOM in Internet Explorer 9.

Examples

This example adjusts the size of a clock's readout to fit the current width and height of the document.


<HTML>
<HEAD><TITLE>A Simple Clock</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function startClock()
{
    window.setInterval("Clock_Tick()", 1000);
    Clock_Tick();
}

var ratio = 4;
function Clock_Tick()
{
    var s = Date();
    var t = s.substring(11,19);
    var doc_height = document.body.offsetHeight;
    var doc_width = document.body.offsetWidth;

    if ((doc_height*ratio)>doc_width)
        doc_height = doc_width / ratio;
        document.all.MyTime.innerText = t;
        document.all.MyTime.style.fontSize = doc_height;
}
</SCRIPT>
<BODY onload="startClock()">
<P ID="MyTime">&nbsp;</P>
</BODY>
</HTML>

This example uses the offsetWidth property and the clientWidth property to show the different ways of measuring the object size.


<DIV ID=oDiv STYLE="overflow:scroll; width:200; height:100"> . . . </DIV>
<BUTTON onclick="console.log(oDiv.clientWidth)">client width</BUTTON>
<BUTTON onclick="console.log(oDiv.offsetWidth)">offset width</BUTTON>

See also

a
abbr
address
area
article
aside
b
bdo
blockQuote
body
br
button
caption
cite
code
col
colGroup
custom
dd
del
div
dl
dt
em
embed
fieldSet
figcaption
figure
footer
form
head
header
hgroup
hn
hr
html
i
iframe
img
input type=button
input type=checkbox
input type=file
input type=hidden
input type=image
input type=password
input type=radio
input type=reset
input type=submit
input type=text
ins
kbd
label
legend
li
map
mark
nav
object
ol
optGroup
option
p
pre
q
rt
ruby
s
samp
section
select
small
span
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
u
ul
var

 

 

Build date: 11/28/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.