top

Baseline 廣泛可用 *

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

* 此特性的某些部分可能存在不同級別的支援。

top CSS 屬性設定定位元素的垂直位置。此內嵌屬性對非定位元素無效。

試一試

top: 0;
top: 4em;
top: 10%;
top: 20px;
<section id="default-example">
  <div class="example-container">
    <div id="example-element">I am absolutely positioned.</div>
    <p>
      As much mud in the streets as if the waters had but newly retired from the
      face of the earth, and it would not be wonderful to meet a Megalosaurus,
      forty feet long or so, waddling like an elephantine lizard up Holborn
      Hill.
    </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 設定為 absolutefixed 時,top 屬性指定元素頂部外邊距邊緣與其包含塊頂部內邊框邊緣之間的距離,或者,在錨點定位元素中使用 anchor() 函式時,相對於指定的 <anchor-side> 邊緣。top 屬性與 topbottomstartendself-startself-endcenter<percentage>相容
  • position 設定為 relative 時,top 屬性指定元素的頂部邊緣在其正常位置下方移動的距離。
  • position 設定為 sticky 時,top 屬性用於計算粘性約束矩形。
  • position 設定為 static 時,top 屬性無效

當同時指定 topbottom 值時,有三種不同情況

  • 如果 position 設定為 absolutefixedheight 未指定(auto100%),則 topbottom 值都會被尊重。
  • 如果 position 設定為 relativeheight 受到限制,則 top 屬性優先,bottom 屬性被忽略。
  • 如果 position 設定為 sticky,則 topbottom 值都會被考慮。這意味著粘性元素可以在其包含塊內上下移動,只要元素的定位框保持在其包含塊內。

語法

css
/* <length> values */
top: 3px;
top: 2.4em;
top: anchor(bottom);
top: anchor-size(--my-anchor self-block, 10%);

/* <percentage>s of the height of the containing block */
top: 10%;

/* Keyword value */
top: auto;

/* Global values */
top: inherit;
top: initial;
top: revert;
top: revert-layer;
top: unset;

<length>

負值、零值或正值 <length>

<percentage>

包含塊高度的 <percentage>

auto

指定

  • 對於絕對定位元素,元素的位置基於 bottom 屬性,而 height: auto 被視為基於內容的高度;或者如果 bottom 也為 auto,則元素定位在其作為靜態元素時應垂直定位的位置。
  • 對於相對定位元素,元素與其正常位置的距離基於 bottom 屬性;或者如果 bottom 也為 auto,則元素根本不垂直移動。

正式定義

初始值auto
應用於定位元素
繼承性
百分比參照包含塊的高度
計算值如果指定為長度,則為相應的絕對長度;如果指定為百分比,則為指定值;否則為 auto
動畫型別一個長度百分比或 calc();

正式語法

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>The size of this content is determined by the position of its edges.</div>

規範

規範
CSS 定位佈局模組第 3 級
# 內嵌

瀏覽器相容性

另見