topプロパティ
プロパティ top は、
親要素の上端からの要素の上端の位置を指定します。
プロパティの値は、
ピクセル、インチ、ポイント、パーセントなどの
一般的な寸法単位で記述できます。
指定する値は負の数でも可能で、
この場合、要素は互いに重なります。
座標の基準は position プロパティによって決まり、
通常は relative
(相対位置)または absolute
(絶対位置)の値が使用されます。
構文
セレクタ {
top: サイズ または auto;
}
例
子要素にホバーしたとき、
その上端の位置を
25px に設定してみましょう:
<div id="elem">
<p></p>
</div>
#elem {
width: 400px;
height: 300px;
border: 1px solid black;
}
p {
position: relative;
width: 100px;
height: 100px;
border: 1px solid red;
margin-top: 50px;
margin-left: 50px;
}
p:hover {
top: 25px;
width: 100px;
height: 100px;
border: 1px solid red;
}
:
例
次に、ホバー時に子要素の上端が
-50px の位置に配置されるようにしましょう:
<div id="elem">
<p></p>
</div>
#elem {
width: 400px;
height: 300px;
border: 1px solid black;
}
p {
position: relative;
margin-top: 50px;
width: 100px;
height: 100px;
border: 1px solid red;
margin-left: 50px;
}
p:hover {
top: -50px;
width: 100px;
height: 100px;
border: 1px solid red;
}
:
関連項目
-
要素の下端の位置を指定するプロパティ
bottom -
要素の左端の位置を指定するプロパティ
left -
要素の右端の位置を指定するプロパティ
right -
最初の要素の背後に配置を指定する疑似要素
::backdrop -
テーブルのキャプションの位置を指定するプロパティ
caption-side