top
試してみましょう
top の効果は、要素がどの様に配置されているか (つまり、 position プロパティの値) によって変わります。
positionがabsoluteまたはfixedに設定されている場合、topプロパティは要素の上辺と包含ブロックの上辺との間の距離を指定します。positionがrelativeに設定されている場合、topプロパティは要素の上辺が通常位置から下方へ移動する量を指定します。positionがstickyに設定されている場合、topプロパティは粘着制約矩形の計算に使用されます。positionがstaticに設定されている場合、topプロパティは効果がありません。
top と bottom の両方が指定されており、 position が absolute または fixed に設定されており、かつ height が未指定 (auto または 100% のどちらか) の場合は、 top と bottom の距離が尊重されます。それ以外の場合、 height が何らかの形で制約されていた場合、または position が relative に設定されていた場合は、 top プロパティが優先されて bottom プロパティは無視されます。
構文
css
/* <length> 値 */
top: 3px;
top: 2.4em;
/* 包含ブロックの高さに対する <percentage> */
top: 10%;
/* キーワード値 */
top: auto;
/* グローバル値 */
top: inherit;
top: initial;
top: revert;
top: unset;
値
<length>-
負、null、または正の
<length>で、以下のものを表します。- 絶対位置指定要素の場合は、包含ブロックの上辺までの距離。
- 相対位置指定要素の場合は、通常の位置からの下方向への移動量。
<percentage>-
包含ブロックの高さに対する
<percentage>です。 auto-
以下のように指定します。
inherit-
値が親要素 (包含ブロックとは限りません) の計算値と同じであることを示すキーワードです。そして、この計算値は
<length>,<percentage>, またはautoキーワードと同様に扱われます。
公式定義
形式文法
top =
auto |
<length-percentage>
<length-percentage> =
<length> |
<percentage>
例
位置指定要素を上から 10% に設定
css
body {
background: beige;
}
div {
position: absolute;
top: 10%;
right: 40%;
bottom: 20%;
left: 15%;
background: gold;
border: 1px solid blue;
}
html
<div>この内容の寸法は、辺の位置によって決まります。</div>
仕様書
| Specification |
|---|
| CSS Positioned Layout Module Level 3 # insets |
ブラウザーの互換性
BCD tables only load in the browser
関連情報
inset: 関連するすべてのプロパティであるtop,bottom,left,rightの一括指定- 対応する論理的プロパティ:
inset-block-start,inset-block-end,inset-inline-start,inset-inline-endおよび一括指定のinset-blockとinset-inline position