HTML <tr> 標簽

定義和用法

<tr> 標簽定義 HTML 表格中的行。

<tr> 元素包含一個或多個 <th><td> 元素。

另請參閱:

HTML 教程:HTML 表格

HTML DOM 參考手冊:TableRow 對象

CSS 教程:設置表格樣式

實例

例子 1

一個簡單的三行 HTML 表格;一個標題行和兩個數據行:

<table>
  <tr>
    <th>月份</th>
    <th>儲蓄</th>
  </tr>
  <tr>
    <td>一月</td>
    <td>¥3400</td>
  </tr>
  <tr>
    <td>二月</td>
    <td>¥4500</td>
  </tr>
</table>

親自試一試

例子 2

如何對齊 <tr> 中的內容(使用 CSS):

<table style="width:100%">
  <tr>
    <th>月份</th>
    <th>儲蓄</th>
  </tr>
  <tr style="text-align:right">
    <td>一月</td>
    <td>¥3400</td>
  </tr>
</table>

親自試一試

例子 3

如何將背景顏色添加到表格行(使用 CSS):

<table>
  <tr style="background-color:#FF0000">
    <th>月份</th>
    <th>儲蓄</th>
  </tr>
  <tr>
    <td>一月</td>
    <td>¥3400</td>
  </tr>
 </table>

親自試一試

例子 4

如何垂直對齊 <tr> 中的內容(使用 CSS):

<table style="height:200px">
  <tr  style="vertical-align:top">
    <th>月份</th>
    <th>儲蓄</th>
  </tr>
  <tr style="vertical-align:bottom">
    <td>一月</td>
    <td>¥3400</td>
  </tr>
</table>

親自試一試

例子 5

如何創建表格標題:

<table>
  <tr>
    <th>姓名</th>
    <th>電郵</th>
    <th>電話</th>
  </tr>
  <tr>
    <td>Bill Gates</td>
    <td>bill.gates@example.com</td>
    <td>138-1234-5678</td>
  </tr>
</table>

親自試一試

例子 6

如何創建帶有標題的表格:

<table>
  <caption>月份ly savings</caption>
  <tr>
    <th>月份</th>
    <th>儲蓄</th>
  </tr>
  <tr>
    <td>一月</td>
    <td>¥3400</td>
  </tr>
  <tr>
    <td>二月</td>
    <td>¥4500</td>
  </tr>
</table>

親自試一試

例子 7

如何定義跨越多行或多列的表格單元格:

<table>
  <tr>
    <th>姓名</th>
    <th>電郵</th>
    <th colspan="2">電話</th>
  </tr>
  <tr>
    <td>Bill Gates</td>
    <td>bill.gates@example.com</td>
    <td>138-1234-5678</td>
    <td>186-2345-6789</td>
  </tr>
</table>

親自試一試

全局屬性

<tr> 標簽還支持 HTML 中的全局屬性

事件屬性

<tr> 標簽還支持 HTML 中的事件屬性

默認的 CSS 設置

大多數瀏覽器將使用以下默認值顯示 <tr> 元素:

tr {
  display: table-row;
  vertical-align: inherit;
  border-color: inherit;
}

瀏覽器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持