THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Element offsetLeft Property

Element Object Reference Element Object

Example

Get the offsetLeft position of a <div> element:

<div id="test">
<p>Click the button to get offsetLeft for the test div.</p>
<p><button onclick="myFunction()">Try it</button></p>
<p>offsetLeft is: <span id="demo"></span></p>
</div>

<script>
function myFunction() {
    var testDiv = document.getElementById("test");
    document.getElementById("demo").innerHTML = testDiv.offsetLeft;
}
</script>
Try it yourself »

Definition and Usage

The offsetLeft property returns the left position (in pixels) relative to the left side the offsetParent element.

The returned value includes:

  • the left position, and margin of the element
  • the left padding, scrollbar and border of the offsetParent element

Note: The offsetParent element is the nearest ancestor that has a position other than static.

Tip: To return the top position of an element, use the offsetTop property.


Browser Support

Property
offsetLeft Yes 12.0 8.0 Yes Yes Yes

Syntax

Return the left offset position:

object.offsetLeft

Technical Details

Default Value: no default value
Return Value: A Number, representing the left position of the element, in pixels
DOM Version: CSSOM

More Examples

Example

Get the position of a a <div> element:

<div id="test">
<p>Click the button to get the left and top offsets for the test div.</p>
<p><button onclick="myFunction()">Try it</button></p>
<p id="demo">offsetLeft: <br>offsetTop: </p>
</div>
<script>
function myFunction() {
    var testDiv = document.getElementById("test");
    var demoDiv = document.getElementById("demo");
    demoDiv.innerHTML = "offsetLeft: " + testDiv.offsetLeft + "<br>offsetTop: " + testDiv.offsetTop;
}
</script>
Try it yourself »


Element Object Reference Element Object