This page is In Progress

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

offsetWidth

Property of dom/HTMLElementdom/HTMLElement

Syntax

var result = element.offsetWidth;
element.offsetWidth = value;

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="JScript">
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="alert(oDiv.clientWidth)">client width</BUTTON>
<BUTTON onclick="alert(oDiv.offsetWidth)">offset width</BUTTON>

View live example

Notes

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.

Syntax

Attributions