今回はBootstrapのテーブルの使い方を覚えてみましょう。
Bootstrap4のバージョン4.3をベースに書いています。
基本的なテーブル
要素にtableクラスを使用します。 #見出し1見出し2見出し31内容1内容2内容32内容1内容2内容3 コード<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">見出し1</th>
<th scope="col">見出し2</th>
<th scope="col">見出し3</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<th scope="row">2</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
</tbody>
</table>
テーブルの背景に色をつける
<table class="table table-light">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">見出し1</th>
<th scope="col">見出し2</th>
<th scope="col">見出し3</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<th scope="row">2</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
</tbody>
</table>
交互に色変える
交互に色を変えるには
#見出し1見出し2見出し31内容1内容2内容32内容1内容2内容33内容1内容2内容3 コード
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">見出し1</th>
<th scope="col">見出し2</th>
<th scope="col">見出し3</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<th scope="row">2</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<th scope="row">3</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<th scope="row">4</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
</tbody>
</table>
テーブル全体に罫線を付ける
#見出し1見出し2見出し31内容1内容2内容32内容1内容2内容3 コード
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">見出し1</th>
<th scope="col">見出し2</th>
<th scope="col">見出し3</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<th scope="row">2</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
</tbody>
</table>
上記とは逆に
これは背景色が暗くても使用可能です。
#見出し1見出し2見出し31内容1内容2内容32内容1内容2内容3 コード
<table class="table table-dark table-borderless">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">見出し1</th>
<th scope="col">見出し2</th>
<th scope="col">見出し3</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<th scope="row">2</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
</tbody>
</table>
行をマウスオーバー時にホバーで見やすく
<table class="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">見出し1</th>
<th scope="col">見出し2</th>
<th scope="col">見出し3</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<th scope="row">2</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
</tbody>
</table>
テーブルを小さくコンパクトに
コンパクトに表示させます。
#見出し1見出し2見出し31内容1内容2内容32内容1内容2内容3 コード
<table class="table table-sm">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">見出し1</th>
<th scope="col">見出し2</th>
<th scope="col">見出し3</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<th scope="row">2</th>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
</tbody>
</table>
背景色について
背景色については先ほど書きましたがbg-○○のクラスでも指定することができます。
table-○○で指定した場合とbg-○○で指定した場合の違いは下記のようになります。
テキストの色など何も追記していない状態なので比べて見て下さい。
背景色のクラスを各行に指定table-primarytable-secondarytable-successtable-dangertable-warningtable-infotable-lighttable-darkbg-primarybg-secondarybg-successbg-dangerbg-warningbg-infobg-lightbg-dark
レスポンシブでのテーブル表示
<div class="table-responsive-ブレークポントを指定">
<table class="table">
...
</table>
</div>
各ブレークポイントの指定は下記のようになります。
table-responsive-sm
table-responsive-md
table-responsive-lg
table-responsive-xl
table-responsive-smセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセル
table-responsive-mdセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセル
table-responsive-lgセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセル
table-responsive-xlセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセル
table-responsiveで全てのブレークポイントをまたいで
テーブルを水平にスクロールすることができます。
table-responsiveセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセル
