all
alt
ch
dir
id
min
rel
top
url
URL
urn
Expand Minimize

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.

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.


<!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="console.log(oID_1.clientWidth)">clientWidth</button>
    <button onclick="console.log(oID_1.offsetWidth)">offsetWidth</button>
  </p>
</body>
</html>

See also

a
abbr
address
area
article
aside
b
bdo
blockQuote
body
br
button
caption
cite
code
col
colGroup
custom
dd
del
dfn
div
dl
dt
em
embed
fieldSet
figcaption
figure
footer
form
frame
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
kbd
label
legend
li
map
mark
menu
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

 

 

Show:
© 2014 Microsoft