caption-side

Baseline 廣泛可用 *

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

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

caption-side CSS 屬性將表格 <caption> 元素的內容放置在指定的一側。該值相對於表格的 writing-mode

試一試

caption-side: top;
caption-side: bottom;
<section class="default-example" id="default-example">
  <table class="transition-all" id="example-element">
    <caption>
      Famous animals
    </caption>
    <tr>
      <th>Name</th>
      <th>Location</th>
    </tr>
    <tr>
      <td>Giraffe</td>
      <td>Africa</td>
    </tr>
    <tr>
      <td>Penguin</td>
      <td>Antarctica</td>
    </tr>
    <tr>
      <td>Sloth</td>
      <td>South America</td>
    </tr>
    <tr>
      <td>Tiger</td>
      <td>Asia</td>
    </tr>
  </table>
</section>
table {
  font-size: 1.2rem;
  text-align: left;
  color: black;
}

th,
td {
  padding: 0.2rem 1rem;
}

caption {
  background: #ffcc33;
  padding: 0.5rem 1rem;
}

tr {
  background: #eeeeee;
}

tr:nth-child(even) {
  background: #cccccc;
}

語法

css
/* Directional values */
caption-side: top;
caption-side: bottom;

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

caption-side 屬性被指定為下面列出的一個關鍵詞值。

top

標題框應位於表格的塊起始側。

bottom

標題框應位於表格的塊結束側。

注意:CSS 邏輯屬性和值模組定義了兩個邏輯值,inline-startinline-end,分別用於將標題框定位在表格的內聯起始邊緣和內聯結束邊緣。這些值在任何瀏覽器中都不受支援。

正式定義

初始值top
應用於table-caption 元素
繼承性
計算值同指定值
動畫型別離散

正式語法

caption-side = 
top |
bottom

示例

設定上下標題

HTML

html
<table class="top">
  <caption>
    Caption ABOVE the table
  </caption>
  <tr>
    <td>Some data</td>
    <td>Some more data</td>
  </tr>
</table>

<br />

<table class="bottom">
  <caption>
    Caption BELOW the table
  </caption>
  <tr>
    <td>Some data</td>
    <td>Some more data</td>
  </tr>
</table>

CSS

css
.top caption {
  caption-side: top;
}

.bottom caption {
  caption-side: bottom;
}

table {
  border: 1px solid red;
}

td {
  border: 1px solid blue;
}

結果

規範

規範
層疊樣式表級別 2
# propdef-caption-side
CSS 邏輯屬性和值第 1 級
# caption-side

瀏覽器相容性

另見