Style position Property

Example

Change the position of a <div> element from relative to absolute:

document.getElementById("myDIV").style.position = "absolute";
Try it Yourself »

Description

The position property sets or returns the type of positioning method used for an element (static, relative, absolute or fixed).


Browser Support

Property
position Yes Yes Yes Yes Yes

Syntax

Return the position property:

object.style.position

Set the position property:

object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"

Property Values

Value Description
static Elements renders in order, as they appear in the document flow. This is default.
absolute The element is positioned relative to its first positioned (not static) ancestor element
fixed The element is positioned relative to the browser window
relative The element is positioned relative to its normal position, so "left:20" adds 20 pixels to the element's LEFT position
sticky The element is positioned based on the user's scroll position

A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).

Note: Not supported in IE/Edge 15 or earlier. Supported in Safari from version 6.1 with a Webkit prefix.
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit


Technical Details

Default Value: static
Return Value: A String, representing the position type of an element
CSS Version CSS2

More Examples

Example

Using different position types:

function myFunction(x)  {
  var whichSelected = x.selectedIndex;
  var posVal = x.options[whichSelected].text;
  var elem = document.getElementById("myDiv");
  elem.style.position = posVal;
}
Try it Yourself »

Example

Return the position of a <h2> element:

alert(document.getElementById("myH2").style.position);
Try it Yourself »

Related Pages

CSS tutorial: CSS Positioning

CSS reference: position property


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.