CSS位置プロパティ
例
<h2>要素を配置します。
h2
{
position: absolute;
left: 100px;
top: 150px;
}
以下の「自分で試してみてください」の例をもっと見てください。
定義と使用法
このposition
プロパティは、要素に使用される配置方法のタイプ(静的、相対、絶対、固定、またはスティッキー)を指定します。
デフォルト値: | 静的 |
---|---|
遺伝性の: | いいえ |
アニメート可能: | 番号。アニメート可能について読む |
バージョン: | CSS2 |
JavaScript構文: | object .style.position = "absolute" |
ブラウザのサポート
表の数字は、プロパティを完全にサポートする最初のブラウザバージョンを示しています。
Property | |||||
---|---|---|---|---|---|
position | 1.0 | 7.0 | 1.0 | 1.0 | 4.0 |
注:このsticky
値は、InternetExplorerまたはEdge15以前のバージョンではサポートされていません。
CSS構文
position: static|absolute|fixed|relative|sticky|initial|inherit;
プロパティ値
Value | Description | Play it |
---|---|---|
static | Default value. Elements render in order, as they appear in the document flow | |
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:20px" 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 |
その他の例
例
通常の位置を基準にして要素を配置する方法:
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
例
より多くのポジショニング:
#parent1 {
position: static;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child1 {
position:
absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
#parent2 {
position: relative;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child2 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
関連ページ
CSSチュートリアル:CSSポジショニング
HTML DOMリファレンス: positionプロパティ