0
2

More than 1 year has passed since last update.

HTMLの主なタグと用途(3) 「テーブル」

Last updated at Posted at 2021-08-14

1. はじめに

HTMLの学習内容は広大なので、本記事では
「テーブル」
を記載する。

2. テーブルの概要

テーブルとは?

表を作成するタグで、身近なもので例えるとExcelで作成する表のこと。

タグは<table>で表記。

横一行の記載法として<table>〜</table>内に<tr>〜</tr>で記載。

<tr>〜</tr>タグ中に<th>〜</th><td>〜</td>でセル(列)を定義。

3. テーブル作成の例

【サンプル】

index.html
<table border="1">
 <tr>
  <td>山田</td>
  <td>太郎</td>
 </tr>
  <td>田中</td>
  <td>花子</td>
 </tr>
</table>

table

テーブル全体のエリア(テーブルの親要素)

tr

"table row"の略

テーブルの一行を定義。行は横方向。

td

"table data"の略。

テーブルのデータが入るセルを定義。

【テーブルタグのイメージ図】
テーブル_イメージ図.png
【表示例】
テーブル.png

4.ヘッダーのあるテーブル

thタグで表記する。 ・適用された文字は太字になる。

【サンプル】

index.html
<table border="1">
 <tr>
  <th></th>
  <th></th>
 </tr>
 <tr>
  <td>山田</td>
  <td>太郎</td>
 </tr>
 <tr>
  <td>田中</td>
  <td>花子</td>
 </tr>
</table>

【表示例】
ヘッダーのあるテーブル.png

5.thead・tbody・tfoot

thead:テーブルのヘッダー行を定義。 ・tbody:テーブルのボディ部分(本体部分)を定義。 ・tfoot:テーブルのフッター部分を定義。

【サンプル】

index.html
<table border="1">
 <thead>
  <tr>
   <th>ヘッダー1</th>
   <th>ヘッダー2</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>ボディー1</td>
   <td>ボディー2</td>
  </tr>
 </tbody>
 <tfoot>
   <tr>
    <td>フッター1</td>
    <td>フッター2</td>
   </tr>
 </tfoot>
</table>

【表示例】
thead_tbody_tfoot.png
【thead・tbody・tfootの使用目的】

・文章構造を正確に表現できる。 ・CSSでスタイルを当てやすい。

6.キャプション

キャプションとは?

テーブルのタイトルのこと。

【サンプル】

index.html
<table border="1">
 <caption>キャプション</caption>
 <tr>
  <td>データ1</td>
  <td>データ2</td>
  <td>データ3</td>
 </tr>
</table>

【表示例】
caption.png
【注意点】
caption要素は、table要素の子要素として配置しなくてはならない。

7.セルの結合

〜横方向に結合(行)〜
colspan属性を使用
【サンプル】

index.html
<table border="1">
 <caption>行方向(横方向)に結合</caption>
 <tr>
  <th colspan="3">セルを結合</th>
 </tr>
 <tr>
  <td>コンテンツ1</td>
  <td>コンテンツ2</td>
  <td>コンテンツ3</td>
 </tr>
</table>

※colspan属性の"3"という数字は、横方向に結合させるセルの個数
【表示例】
セルを結合(横方向).png


〜縦方向に結合(列)〜
rowspan属性を使用
【サンプル】

index.html
<table border="1">
 <caption> 列方向(縦方向)に結合</caption>
 <tr>
  <td rowspan="3">セルを結合</td>
  <td>コンテンツ1</td>
 </tr>
 <tr>
  <td>コンテンツ2</td>
 </tr>
 <tr>
  <td>コンテンツ3</td>
 </tr>
</table>

※rowspan属性の"3"という数字は、縦方向に結合させるセルの個数
【表示例】
セルを結合(縦方向).png

8. おわりに

次項:HTMLの主なタグと用途(4) 「フォーム」に続く。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2