Company | Contact | Country |
---|---|---|
Alfreds Futterkiste | Maria Anders | Germany |
Centro comercial Moctezuma | Francisco Chang | Mexico |
Ernst Handel | Roland Mendel | Austria |
Island Trading | Helen Bennett | UK |
Laughing Bacchus Winecellars | Yoshi Tannamuri | Canada |
Magazzini Alimentari Riuniti | Giovanni Rovelli | Italy |
HTML テーブルは、<table> タグで定義します。
各テーブルの行は、<tr> タグで定義します。テーブルの見出しは、 <th> タグで定義します。デフォルトで、テーブルの見出しは太字で中央揃えになります。 テーブルのデータ/セルは、<td> タグで定義します。
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself »
注: <td> 要素は、テーブルのデータ・コンテナです。
この要素には、あらゆる種類の HTML 要素(テキスト、画像、リスト、別なテーブルなど)を含めることができます。
テーブルにボーダーを指定しない限り、ボーダー(罫線)は表示されません。
ボーダーは、CSS の border プロパティを使用して設定します:
<table border="1" style="width:100%">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
テーブルとテーブルセルの両方にボーダーを定義することを忘れないでください。
1 本のボーダーに折畳んだボーダーにしたい場合は、CSS border-collapse を追加します:
セル・パディングは、セル内のコンテンツとボーダー間のスペースを指定します。
パディングを指定しなければ、テーブルのセルはパディングなしで表示されます。
パディングを設定するには、CSS padding プロパティを使用します:
デフォルトでは、テーブルの見出は太字で中央揃えで表示されます。
テーブルの見出しを左揃えにしたい場合は、CSS text-align プロパティを使用します:
ボーダー・スペーシングはセル間のスペースを指定します。
テーブルにボーダー・スペーシングを設定するには、CSS border-spacing プロパティを使用します:
ボーダーを折畳んだ HTML テーブルでは、border-spacing の効果はありません。
1つ以上の列を結合したセルを作るには、colspan 属性を使用します:
<table style="width:100%">
<tr>
<th>Name</th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>55577854</td>
<td>55577855</td>
</tr>
</table>
Try it Yourself ❯
1つ以上の行を結合したセルを作るには、rowspan 属性を使用します:
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">Telephone:</th>
<td>55577854</td>
</tr>
<tr>
<td>55577855</td>
</tr>
</table>
テーブルに表題を追加するには、<caption> タグを使用します:
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
Try it Yourself ❯
<caption> タグは、<table> タグの直後に挿入しなければなりません。
特別なテーブルに特別なスタイルを定義するためには、テーブルに id 属性を追加します:
<table id="t01">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
table#t01 {
width: 100%;
background-color: #f1f1c1;
}
Try it Yourself ❯
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color: #fff;
}
table#t01 th {
color: white;
background-color: black;
}
Try it Yourself ❯
Exercise 1 ❯ Exercise 2 ❯ Exercise 3 ❯ Exercise 4 ❯ Exercise 5 ❯ Exercise 6 ❯
タグ | 説明 |
---|---|
<table> | テーブルを定義する |
<th> | テーブルの見出しセルを定義する |
<tr> | テーブルの行を定義する |
<td> | テーブルのセルを定義する |
<caption> | テーブルの表題を定義する |
<colgroup> | フォーマットのためテーブルの1つ以上の列グループを指定する |
<col> | <colgroup> 要素内の各列の column プロパティを指定する |
<thead> | テーブルの見出し内容のグループ |
<tbody> | テーブルの本文内容のグループ |
<tfoot> | テーブルのフッター内容のグループ |