Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

offsetWidth property

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.

To comply with the Cascading Style Sheets, Level 1 (CSS1) box model, Microsoft Internet Explorer 6 and later calculate the height of objects differently when you use the !DOCTYPE declaration in your document to switch on standards-compliant mode. This difference may affect the value of the offsetWidth propety. When standards-compliant mode is switched on, the width property specifies the distance between the left and right edges of the bounding box that surrounds the object's content. When standards-compliant mode is not switched on, and with earlier versions of Windows Internet Explorer, the width property also includes the border and padding belts that surround the object's bounding box. For more information, see CSS Enhancements in Internet Explorer 6.

Examples

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


<!DOCTYPE html>
<html>

<head>
  <title>A Simple Clock</title>
  <script>
    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>
</head>

<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.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/offsetWidth.htm


<!DOCTYPE html>
<html>

<head>
  <title>offsetWidth</title>
</head>

<body style="text-align: center;">
  <div id="oID_1" y style="overflow: scroll; width: 200px; height: 100px">
    This example shows the difference between
    the dimension retrieved by the <b>clientWidth</b> and the dimension retrieved by the <b>offsetWidth</b>, the latter
    including the width of scrollbars.
  </div>
  <p>
    <button onclick="alert(oID_1.clientWidth)">clientWidth</button>
    <button onclick="alert(oID_1.offsetWidth)">offsetWidth</button>
  </p>
</body>
</html>

See also

a
abbr
acronym
address
applet
area
article
aside
b
bdo
big
blockQuote
body
br
button
caption
center
cite
code
col
colGroup
custom
dd
del
dfn
dir
div
dl
dt
em
embed
fieldSet
figcaption
figure
font
footer
form
frame
frameSet
head
header
hgroup
hn
hr
html
i
iframe
img
input type=button
input type=checkbox
input type=email
input type=file
input type=hidden
input type=image
input type=number
input type=password
input type=radio
input type=range
input type=reset
input type=search
input type=submit
input type=tel
input type=text
input type=url
ins
isIndex
kbd
label
legend
li
listing
map
mark
marquee
menu
nav
nextID
noBR
object
ol
optGroup
option
p
plainText
pre
q
rt
ruby
s
samp
section
select
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
xmp

 

 

Show:
© 2014 Microsoft