The HTML DOM Style left property is used for setting the or returning a positioned element left position. To position an element, you have to set its position property to relative, absolute or fixed.
Following is the syntax for −
Setting the left property −
object.style.left = "auto|length|%|initial|inherit"
The above properties are explained as follows −
Value | Description |
---|---|
auto | Thisis default position and sets the browser to left position. |
length | Thisis for defining the left position in length units. It allowsnegative values. |
% | Forsetting the left position of the child in % relative to the widthof the parent element. |
initial | Forsetting this property to initial value. |
inherit | Toinherit the parent property value |
Let us look at an example for the left property −
Example
<!DOCTYPE html> <html> <head> <style> #DIV1{ margin-top:70px; position:absolute; width:100px; height:100px; background-color:red; } </style> <script> var setLeft=50; function changeLeft() { document.getElementById("DIV1").style.left = setLeft+"px"; setLeft+=50; } </script> </head> <h1>Left property example</h1> <body> <div id="DIV1"></div> <p>Change the left position for the below div by clicking the below button</p> <button type="button" id="myBtn" onclick="changeLeft()">Left Position</button> </body> </html>
Output
On clicking the “Left Position” button, the box will move to right by 50 px every time −