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 級
# 內嵌

瀏覽器相容性

另見