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 级
# 内嵌

浏览器兼容性

另见