<tr>タグについて
<tr>とは?
<tr>
タグは、HTMLの表(<table>
)の中で1行(table row)を定義するためのタグです。
行の中には<th>
(見出しセル)または<td>
(データセル)を並べて使用します。
基本構文
<table>
<tr>
<th>見出し1</th>
<th>見出し2</th>
</tr>
<tr>
<td>データ1</td>
<td>データ2</td>
</tr>
</table>
主な使い方
<tr>
は1行分のセル(<th>
や<td>
)をまとめる
- 複数の
<tr>
を使って表の行を増やす
- ヘッダーとデータ行で
<th>
と<td>
を使い分ける
使用例
<table border="1">
<tr>
<th>名前</th>
<th>年齢</th>
</tr>
<tr>
<td>田中</td>
<td>28</td>
</tr>
</table>
スタイルの適用例(CSS)
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #e0f0ff;
}
ネスト・入れ子の注意点
<tr>
は<table>
タグの中で使用する
- セルの数(
<th>
や<td>
)は他の行とそろえるのが一般的
<tr>
は必ず</tr>
で閉じる
まとめ
<tr>
は表の1行を定義するタグ
- 中に
<th>
または<td>
を含める
- ヘッダー行・データ行ともに使われる
- 表全体は
<table>
で囲む必要がある