bottom

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

bottom CSS 属性用于设置定位元素的垂直位置。这个嵌入属性对非定位元素没有影响。

试一试

bottom: 0;
bottom: 4em;
bottom: 10%;
bottom: 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;
}

bottom 的效果取决于元素的定位方式(即 position 属性的值)。

  • position 设置为 absolutefixed 时,bottom 属性指定了元素底部外边距的外边缘与包含块底部内边距的外边缘之间的距离。如果是在锚点定位元素中,并且在值中使用了 anchor() 函数,那么它是相对于指定 <anchor-side> 边缘的位置。bottom 属性与 topbottomstartendself-startself-endcenter<percentage> 值是兼容的。
  • position 设置为 relative 时,bottom 属性指定了元素底部边缘相对于其正常位置向上移动的距离。
  • position 设置为 sticky 时,bottom 属性用于计算粘性约束矩形。
  • position 设置为 static 时,bottom 属性没有效果

当同时指定了 topbottom,并且 position 设置为 absolutefixed并且 height 未指定(即 auto100%)时,topbottom 的距离都将被遵守。在所有其他情况下,如果 height 以任何方式受到约束,或者 position 设置为 relative,则 top 属性优先,bottom 属性将被忽略。

语法

css
/* <length> values */
bottom: 3px;
bottom: 2.4em;
bottom: calc(anchor(--my-anchor 50%) + 5px);
bottom: anchor-size(width);

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

/* Keyword value */
bottom: auto;

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

<length>

负值、零值或正值 <length>

<percentage>

包含块高度的<percentage>

auto

指定

  • 对于绝对定位元素,元素的位置基于 top 属性,而 height: auto 被视为基于内容的高度;如果 top 也为 auto,则元素将垂直定位在其作为静态元素时应有的位置。
  • 对于相对定位元素,元素与其正常位置的距离基于 top 属性;如果 top 也为 auto,则元素根本不会垂直移动。

正式定义

初始值auto
应用于定位元素
继承性
百分比参考包含块的高度。
计算值如果指定为长度,则为相应的绝对长度;如果指定为百分比,则为指定值;否则为 auto
动画类型一个长度百分比或 calc();

正式语法

bottom = 
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

示例

绝对定位和固定定位

此示例演示了当 positionabsolutefixed 时,bottom 属性行为上的差异。

HTML

html
<p>
  This<br />is<br />some<br />tall,<br />tall,<br />tall,<br />tall,<br />tall<br />content.
</p>
<div class="fixed"><p>Fixed</p></div>
<div class="absolute"><p>Absolute</p></div>

CSS

css
p {
  font-size: 30px;
  line-height: 2em;
}

div {
  width: 48%;
  text-align: center;
  background: rgb(55 55 55 / 20%);
  border: 1px solid blue;
}

.absolute {
  position: absolute;
  bottom: 0;
  left: 0;
}

.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
}

结果

规范

规范
CSS 定位布局模块第 3 级
# 内嵌

浏览器兼容性

另见