How to use measurement and location properties

Measurement and location values do not have to be static integers. They can be scripted values based on distances and dimensions of other elements, expressed lengths (as in more traditional media) or equations. When working with several elements, you can use the measurement of one element to set the location of another.

You can use the measurement and location properties discussed in this overview to construct more complex creations. For example, to center an element within its container, set its left coordinate to the sum of one-half the width of its container minus one-half the width of the element. The syntax is demonstrated in the following example.

function center(oNode){
   var oParent=oNode.parentElement; - oNode.offsetWidth/2;
<div id="odiv"
   style="position: absolute;">
   Click Here To Center

While the DHTML DOM provides an easy way to obtain the current dimensions and location of an element, you must use cascading style sheet to set these values in most cases. Except for elements that expose height and width properties, such as img and table, you can use various cascading style sheet attributes to set the size of an element. While many properties return values in pixels, you can use some cascading style sheet properties with specific length units, such as inches or centimeters.

The following example moves the H1 element 1 inch every time the user clicks the element:

function moveMe(){
   // Move the object by one inch.;;
<h1 id="oHeader"
   style="position: absolute; top: 1in; left: 1in;"

When moving elements to specific locations in a document, you sometimes have to account for the different properties of the box element. The values of height and width properties include border and padding measurements. Moving one element to the visible corner of another element is relatively easy using the offset properties and techniques described in the preceding example. However, when positioning an element at a specific point in the content of another positioned element, you must include the size of the padding and border. You can use either the client properties or the padding and border properties to establish a location within the content of an element.

The following example shows three different ways to position an element within the content of another element. First, cascading style sheet border and padding properties are used to move an element to the content within a positioned element. Next, DHTML offset and client properties are used to move an element within the content of a positioned element without accounting for the padding.

function fnMove1(){
   // Method 1: Use only CSS properties
   var +
      parseInt( + 
   var + 
      parseInt( + 
function fnMove2(){
   // Method 2: Use DHTML properties.
   var iTop2=odiv.offsetTop + odiv.clientTop;
   var iLeft2=odiv.offsetLeft + odiv.clientLeft;;;
function fnMove3(){
   // Method 3: Use DHTML, CSS, and a TextRectangle.
   var aRects=odiv.getClientRects();
   var oRect=aRects[0];
   var oBnd=odiv.getBoundingClientRect(); + 
      parseInt( + 
      parseInt( Top); + 
      parseInt( + 

<div id="odiv" style="position: absolute; top: 150px; 
  left: 50px; border: 10px outset; padding: 10px; 
  width: 250px; height: 250px; background-color: #EFEFEF;">
Move marker here.

<span id="oMarker" style="top: 200px; left: 200px; 
  position: absolute; border: 2px outset; 
  background-color: #CFCFCF;">

<input type="button" value ="CSS" onclick="fnMove1()">
<input type="button" value ="DHTML" onclick="fnMove2()">
<input type="button" value ="TextRectangle" onclick="fnMove3()">

Community Additions