top
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
* Some parts of this feature may have varying levels of support.
top は CSS のプロパティで、位置指定要素の垂直位置を設定します。このインセットプロパティは、位置指定されていない要素には効果がありません。
試してみましょう
top: 0;
top: 4em;
top: 10%;
top: 20px;
<section id="default-example">
<div class="example-container">
<div id="example-element">これは絶対位置指定されています。</div>
<p>
まるで地球の表面から水が引き始めたかのように、通りは泥だらけで、ホルボーン通りを象のようなトカゲのようによちよち歩く、体長 12 メートルほどのメガロサウルスに出会っても不思議ではないでしょう。
</p>
</div>
</section>
.example-container {
border: 0.75em solid;
padding: 0.75em;
text-align: left;
position: relative;
width: 100%;
min-height: 200px;
}
#example-element {
background-color: #264653;
border: 4px solid #ffb500;
color: white;
position: absolute;
width: 140px;
height: 60px;
}
top の効果は、要素がどの様に配置されているか(つまり、 position プロパティの値)によって変わります。
positionがabsoluteまたはfixedに設定されている場合、topプロパティは、要素の上端の外側のマージンと、その包含ブロックの上端の内側の境界線との間の距離を指定します。または、値内でanchor()関数が使用されている場合、位置指定要素の場合、値内でanchor()関数が使用されているときは、指定された<anchor-side>の辺を基準とします。topプロパティは、top、bottom、start、end、self-start、self-end、center、および<percentage>値と互換性があります。positionがrelativeに設定されている場合、topプロパティは要素の上辺が通常位置から下方へ移動する量を指定します。positionがstickyに設定されている場合、topプロパティは粘着制約矩形の計算に使用されます。positionがstaticに設定されている場合、topプロパティは効果がありません。
top と bottom の両方が指定されている場合は、 3 つの異なる場合があります。
positionがabsoluteまたはfixedに設定されており、heightが指定されていない(autoまたは100%である)場合、topおよびbottomの両方が尊重されます。positionがrelativeに設定されているか、heightが指定されている場合、topプロパティが優先され、bottomプロパティは無視されます。positionがstickyに設定されている場合、topとbottomの両方の値が考慮されます。つまり、粘着位置指定要素は、要素の位置ボックスがそのブロック内に収まっている限り、これら 2 つのプロパティの値に基づいて、そのブロック内で上下に移動できる可能性があります。
構文
css
/* <length> 値 */
top: 3px;
top: 2.4em;
top: anchor(bottom);
top: anchor-size(--my-anchor self-block, 10%);
/* 包含ブロックの高さに対する <percentage> */
top: 10%;
/* キーワード値 */
top: auto;
/* グローバル値 */
top: inherit;
top: initial;
top: revert;
top: revert-layer;
top: unset;
値
<length>-
負、null、または正の
<length>で、以下のものを表します。- 絶対位置指定要素の場合は、包含ブロックの上辺までのを表します。
- アンカー位置指定要素の場合は、
anchor()関数は、関連付けられたアンカー要素の上端または下端の位置を基準とした相対的な<length>値に解決されます(詳細はインセットプロパティでanchor()関数値を使用を参照)。また、anchor-size()関数は、関連するアンカー要素の幅または高さに相対的な<length>値に解決します(アンカーサイズに基づく要素の位置の設定 を参照)。 - 相対位置指定要素の場合は、通常の位置からの下方向への移動量を表します。
<percentage>-
包含ブロックの高さに対する
<percentage>です。 auto-
以下のように指定します。
公式定義
形式文法
top =
auto |
<length-percentage> |
<anchor()> |
<anchor-size()>
<length-percentage> =
<length> |
<percentage>
<anchor()> =
anchor( <anchor-name>? &&
<anchor-side> , <length-percentage>? )
<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )
<anchor-name> =
<dashed-ident>
<anchor-side> =
inside |
outside |
top |
left |
right |
bottom |
start |
end |
self-start |
self-end |
<percentage> |
center
<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline
例
>位置指定要素を上から 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> |
ブラウザーの互換性
関連情報
bottom,left,rightinset一括指定inset-block-start,inset-block-end,inset-inline-start,inset-inline-endinset-blockおよびinset-inline一括指定position- CSS 位置指定レイアウトモジュール