テーブルの基本
HTMLではtableタグを使って表を作成することができます。
tableタグの基本構造
tableタグは、いくつかのタグで構成されています。
- table:テーブルの大枠
- tr:行のまとまり
- th:タイトルのセル
- td:通常のセル
table
tableタグは、テーブルの一番外側のタグです。
<table>
</table>
tr
trタグは行を表示するタグで、tableの中に入れます。
<table>
<tr></tr>
</table>
td / tdタグ
th、tdタグはセルを表示するタグで、trタグの中に入れます。thは表のタイトルになります。
<table>
<tr>
<th>タイトル</th>
<td>コンテンツ</td>
</tr>
</table>
テーブルを作成してみる
ファイル構成
./table.html
テーブルの概要
商品名と価格を、tableタグで表示してみましょう。
| 商品名 | 価格 |
|---|---|
| コーヒー | ¥120 |
| 紅茶 | ¥150 |
| ほうじ茶 | ¥130 |
行の追加
tableタグの入力
まず、tableタグを入力します。
<table>
</table>
trタグの入力
trタグを入力します。
table.html
<table>
<tr>
</tr>
</table>
タイトルの追加
thタグの入力
trタグの中に、「商品名」の thタグを入力します。
table.html
<table>
<tr>
<th>商品名</th>
</tr>
</table>
同様に「価格」の thタグも入力します。
<table>
<tr>
<th>商品名</th>
<th>価格</th>
</tr>
</table>
親子要素のセレクタ
Emmetを使うとテーブルを効率よく入力できます。*>*は、親子要素のセレクタを表します。
親要素>子要素
tr>td のEmmet
tr>td の親子要素はつぎのとおりです。
<tr>
<td></td>
</tr>
「価格」の行の下で、Emmetで入力します。

trタグとtdタグが親子要素で入力できました。
<table>
<tr>
<th>商品名</th>
<th>価格</th>
</tr>
<tr>
<td></td>
</tr>
</table>
tdタグに文字列を入力します。
<table>
<tr>
<th>商品名</th>
<th>価格</th>
</tr>
<tr>
<td>コーヒー</td>
</tr>
</table>
テーブルの完成形
テーブルを完成させてみましょう。
<table>
<tr>
<th>商品名</th>
<th>価格</th>
</tr>
<tr>
<td>コーヒー</td>
<td>¥120</td>
</tr>
<tr>
<td>紅茶</td>
<td>¥150</td>
</tr>
<tr>
<td>ほうじ茶</td>
<td>¥130</td>
</tr>
</table>
tableタグの確認
ブラウザでテーブル表示されるか確認しましょう。thタグはデフォルトで太字になっています。
thead / tbody
ヘッダー部分をtheadタグ、データ部分をtbodyタグでまとめることができます。
<table>
<thead>
<tr>
<th>商品名</th>
<th>価格</th>
</tr>
</thead>
<tbody>
<tr>
<td>コーヒー</td>
<td>¥120</td>
</tr>
<tr>
<td>紅茶</td>
<td>¥150</td>
</tr>
</tbody>
</table>
セルの結合
colspan
colspanはセルの列を結合します。
<td colspan="結合する数"></td>
colspanの例
<table>
<tr>
<th colspan="2">関東</th>
</tr>
<tr>
<td>東京</td>
<td>とうきょう</td>
</tr>
<tr>
<td>神奈川</td>
<td>かながわ</td>
</tr>
</table>
rowspan
rowspan はセルの行を結合します。
<td colspan="結合する数"></td>
colspanの例
<table>
<tr>
<th rowspan="4">関東</th>
</tr>
<tr>
<td>東京</td>
</tr>
<tr>
<td>神奈川</td>
</tr>
<tr>
<td>千葉</td>
</tr>
</table>
tableレイアウト(アンチパターン)
テーブルレイアウトとは
「ヘッダー」「メニュー」「メイン」などを、tableタグで構成することをテーブルレイアウトといい、HTMLの悪い例です。
| ヘッダー | |
| サイドバー | コンテンツ |
| フッター | |
テーブルレイアウト(悪い例)
一見、綺麗にレイアウトできているように見えますが、HTMLソースが見にくく、複雑になってしまう悪い例です。
table.html
<table border="0" cellspacing="0" cellpadding="0" width="760">
<tr>
<td colspan="2" style="background: red; height=50px">
ヘッダー
</td>
</tr>
<tr>
<td style="background: green; width: 200px;">
サイドバー
</td>
<td style="background: white; height: 300px">
コンテンツ
</td>
</tr>
<tr>
<td colspan="2" style="background: darkgray;">
フッター
</td>
</tr>
</table>
アンチパターン
このように動作はするが、よくないコーディングや設計のことをアンチパターンといいます。
コース内レッスン
- 1
Lesson 1
HTML開発の準備
レッスンを見る - 2
Lesson 2
HTMLとは
レッスンを見る - 3
Lesson 3
HTMLの基本構造
レッスンを見る - 4
Lesson 4
HTMLタグの基本
レッスンを見る - 5
Lesson 5
プロフィールページ作成
レッスンを見る - 6
Lesson 6
画像表示
レッスンを見る - 7
Lesson 7
リスト表示
レッスンを見る - 8
Lesson 8
ページリンク
レッスンを見る - 9
Lesson 9
テーブル
レッスンを見る - 10
Lesson 10
レイアウト
レッスンを見る - 11
Lesson 11
CSSとは
レッスンを見る - 12
Lesson 12
CSSの基本
レッスンを見る - 13
Lesson 13
フォント
レッスンを見る - 14
Lesson 14
ブロック要素とインライン要素
レッスンを見る - 15
Lesson 15
idとclassセレクタ
レッスンを見る - 16
Lesson 16
ボックス
レッスンを見る - 17
Lesson 17
テキストのスタイル
レッスンを見る - 18
Lesson 18
擬似要素と擬似クラス
レッスンを見る - 19
Lesson 19
バックグラウンドイメージ
レッスンを見る - 20
Lesson 20
コンテナー
レッスンを見る - 21
Lesson 21
Float
レッスンを見る - 22
Lesson 22
Flexbox
レッスンを見る - 23
Lesson 23
要素の位置指定
レッスンを見る - 24
Lesson 24
Overflow
レッスンを見る - 25
Lesson 25
HTMLフォーム
レッスンを見る - 26
Lesson 26
GETリクエスト
レッスンを見る - 27
Lesson 27
POSTリクエスト
レッスンを見る