スタイルトッププロパティ
例
<button>要素の一番上の位置を設定します。
document.getElementById("myBtn").style.top = "100px";
定義と使用法
topプロパティは、配置された要素の最上位位置を設定または返します。
このプロパティは、パディング、スクロールバー、境界線、マージンなど、要素の上部の位置を指定します。
ヒント: positioned要素は、positionプロパティがrelative、absolute、またはfixedに設定されている要素です。
ヒント:配置された要素の下部の位置を設定または戻すには、bottomプロパティを使用します。
ブラウザのサポート
Property | |||||
---|---|---|---|---|---|
top | Yes | Yes | Yes | Yes | Yes |
構文
最上位のプロパティを返します。
object.style.top
最上位のプロパティを設定します。
object.style.top = "auto|length|%|initial|inherit"
プロパティ値
Value | Description |
---|---|
auto | Lets the browser set the top position. This is default |
length | Defines the top position in length units. Negative values are allowed |
% | Sets the top position in % of the height of the parent element |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
技術的な詳細
デフォルト値: | 自動 |
---|---|
戻り値: | 配置された要素の最上位を表す文字列 |
CSSバージョン | CSS2 |
その他の例
例
負の値の使用-<div>要素の一番上の位置を設定します。
document.getElementById("myDiv").style.top = "-100px";
例
<div>要素の一番上の位置を返します。
alert(document.getElementById("myDiv").style.top);
関連ページ
CSSチュートリアル:CSSポジショニング
CSSリファレンス:トッププロパティ
❮スタイルオブジェクト