<table>(表格元素)

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

HTML <table> 元件代表表格數據 ── 換句話說,就是透過二維資料表來呈現資訊。

嘗試一下

<table>
  <caption>
    Front-end web developer course 2021
  </caption>
  <thead>
    <tr>
      <th scope="col">Person</th>
      <th scope="col">Most interest in</th>
      <th scope="col">Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Chris</th>
      <td>HTML tables</td>
      <td>22</td>
    </tr>
    <tr>
      <th scope="row">Dennis</th>
      <td>Web accessibility</td>
      <td>45</td>
    </tr>
    <tr>
      <th scope="row">Sarah</th>
      <td>JavaScript frameworks</td>
      <td>29</td>
    </tr>
    <tr>
      <th scope="row">Karen</th>
      <td>Web performance</td>
      <td>36</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th scope="row" colspan="2">Average age</th>
      <td>33</td>
    </tr>
  </tfoot>
</table>
table {
  border-collapse: collapse;
  border: 2px solid rgb(140 140 140);
  font-family: sans-serif;
  font-size: 0.8rem;
  letter-spacing: 1px;
}

caption {
  caption-side: bottom;
  padding: 10px;
  font-weight: bold;
}

thead,
tfoot {
  background-color: rgb(228 240 245);
}

th,
td {
  border: 1px solid rgb(160 160 160);
  padding: 8px 10px;
}

td:last-of-type {
  text-align: center;
}

tbody > tr:nth-of-type(even) {
  background-color: rgb(237 238 242);
}

tfoot th {
  text-align: right;
}

tfoot td {
  font-weight: bold;
}
內容類型 流內容
允許內容
按照以下順序:
  1. 一個可選的<caption>元素,
  2. 零個或更多的<colgroup>元素,
  3. 一個可選的<thead>元素,
  4. 一個在以下元素之前或之後的可選 <tfoot> 元素:
    • 零個或更多的<tbody>元素,
    • 或者,一個或更多的<tr>元素
標籤省略 不允許,開始和結束標籤都是必須的。
允許父元素 Any element that accepts flow content
允許 ARIA 規則 Any
DOM 介面 HTMLTableElement

屬性

這個元件包含了 全域屬性(global attributes)

棄用屬性

align 已棄用

這個枚舉屬性會指示表格中的文字要如何對齊。可用值如下:left:意思是表格應該顯示在文件的左方。

  • center:意思是表格應該顯示在文件的中間。
  • right:意思是表格應該顯示在文件的右方。在 CSS 要得出類似效果,應該設定 margin-leftmargin-right;如果要置中,則要把 margin 屬性設定為 0 auto
bgcolor 已棄用

定義表格的背景與內容顏色。它使用六位十六進制 RGB code,前缀需要加上 '#' 。也可以用預先定義的顏色字串可用。在 CSS 要得出類似效果,應該使用 background-color 屬性。

border 已棄用

這個屬性以像素為單位,定義了圍繞於表格框架的大小。如果設為 0,代表 frame 屬性為空。在 CSS 要得出類似效果,應該使用 border 屬性。

cellpadding 已棄用

這個屬性定義了元件與邊界線之間的空白,以及要不要顯示。如果輸入像素,像素長度的空白會套用到四個邊;如果輸入百分比,內容將居中、整體的垂直空間(上與下)會使用這個百分比表示。橫向空間(左與右)也適用這個規則。在 CSS 要得出類似效果,應該對 <table> 屬性使用 border-collapse;並對 <td> 使用