スタイル位置プロパティ
例
<div>要素の位置を相対から絶対に変更します。
document.getElementById("myDIV").style.position = "absolute";
定義と使用法
positionプロパティは、要素に使用されるポジショニングメソッドのタイプ(静的、相対、絶対、または固定)を設定または返します。
ブラウザのサポート
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
構文
位置プロパティを返します。
object.style.position
位置プロパティを設定します。
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
プロパティ値
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 |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
技術的な詳細
デフォルト値: | 静的 |
---|---|
戻り値: | 要素の位置タイプを表す文字列 |
CSSバージョン | CSS2 |
その他の例
例
さまざまな位置タイプの使用:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
例
<h2>要素の位置を返します。
alert(document.getElementById("myH2").style.position);
関連ページ
CSSチュートリアル:CSSポジショニング
CSSリファレンス:positionプロパティ
❮スタイルオブジェクト