LoginSignup
0
0

More than 3 years have passed since last update.

テーブルタグ~HTML~

Last updated at Posted at 2021-04-25

テーブルタグ

① tableタグ
1つのテーブル全体をまとめるタグ

②trタグ
table rowの略です。テーブルの横の列を作ります。

③thタグ
見出しタグセルを作るタグを作るタグです。

④tdタグ
通常セルを作るタグです。

コード

index.html
<table>
    <caption>登場人物</caption>
    <tr>
      <th class="col-1">&nbsp;</th>
      <th class="col-2">年齢</th>
      <th class="col-3">性別</th>
      <th class="col-4">性格</th>
    </tr>
    <tr>
      <th>太郎</th>
      <td>22</td>
      <td></td>
      <td>優しい</td>
    </tr>
    <tr>
      <th>花子</th>
      <td>18</td>
      <td></td>
      <td>お人好し</td>
    </tr>
    <tr>
      <th>一郎</th>
      <td>24</td>
      <td></td>
      <td>ロールキャベツ系男子</td>
    </tr>
  </table>

①:nbsp
セルが空の場合に入れます。

trタグが横列になります。trタグに挟まれる形で、太郎君、22,男、優しいが1行になります。

HTMLでの実装は以上になりますが、このままでは表のようにボーダーが無いのでCSSで整えていきます。

CSS

style.css
 table,th,td {
   border: 3px solid gray;
   border-spacing: 0;
   border-collapse: collapse;
   margin: auto;
 }

 .col-1,.col-2,.col-3 {
   width: 20%;
 }

 th{
   background-color: pink;
 }

デフォルトでは、ボーダーがつかないためcssで指定します。

①border-spacingでは、ボーダー同士の隙間をなくします。
②border-collapaseでは、ボーダー同士を重ねます。

③セル同士の幅設定
それぞれのthタグに幅を設定することでより見やすくなります。

完成

スクリーンショット 2021-04-26 7.57.02.png

0
0
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
0