Bootstrap

Bootstrapテーブルの使い方|見やすい表を簡単に作成

Bootstrap テーブル コンポーネント

Bootstrapテーブルの使い方
見やすい表を簡単に作成

Bootstrapのテーブルクラスを使えば、ストライプ、ホバー、ボーダーなどのスタイルをクラスひとつで適用できます。
レスポンシブ対応のテーブルも簡単に作れます。

こんな人向けの記事です

  • HTMLテーブルの見た目をすぐに改善したい
  • Bootstrapのテーブルクラスを知りたい
  • スマホでもテーブルを見やすく表示したい

Step 1基本のテーブル

table クラスを付けるだけで、パディングとボーダーが整ったテーブルになります。

#名前メール
1田中太郎tanaka@example.com
2鈴木花子suzuki@example.com
3佐藤一郎sato@example.com
HTML
<table class="table">
  <thead>
    <tr>
      <th>#</th>
      <th>名前</th>
      <th>メール</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>田中太郎</td>
      <td>tanaka@example.com</td>
    </tr>
    <tr>
      <td>2</td>
      <td>鈴木花子</td>
      <td>suzuki@example.com</td>
    </tr>
    <tr>
      <td>3</td>
      <td>佐藤一郎</td>
      <td>sato@example.com</td>
    </tr>
  </tbody>
</table>

Step 2ストライプとホバー

クラス効果
table-striped奇数行にストライプ(縞模様)
table-hover行にホバーすると背景色が変わる
table-bordered全セルにボーダーを追加
table-borderlessボーダーを全て除去
table-smパディングを半分にしてコンパクトに
HTML
<!-- ストライプ + ホバー + ボーダー -->
<table class="table table-striped table-hover table-bordered">
  ...
</table>

<!-- コンパクトなテーブル -->
<table class="table table-sm">
  ...
</table>
複数のクラスを組み合わせ可能
table-stripedtable-hovertable-bordered は自由に組み合わせて使えます。

Step 3テーブルの色バリエーション

HTML
<!-- テーブル全体の色 -->
<table class="table table-dark">...</table>
<table class="table table-dark table-striped">...</table>

<!-- ヘッダーだけ暗く -->
<table class="table">
  <thead class="table-dark">...</thead>
  <tbody>...</tbody>
</table>

<!-- ヘッダーを薄いグレーに -->
<thead class="table-light">...</thead>

Step 4レスポンシブテーブル

テーブルを table-responsive で囲むと、横幅が画面に収まらない時に横スクロールが出ます。

HTML
<!-- 常にレスポンシブ -->
<div class="table-responsive">
  <table class="table">...</table>
</div>

<!-- 特定の画面幅以下でのみレスポンシブ -->
<div class="table-responsive-md">
  <table class="table">...</table>
</div>
table-responsive は table の親要素に付ける
table-responsive<table> 自体ではなく、<div> で囲んでその <div> に付けます。

Step 5行や列の色付け

HTML
<!-- 特定の行に色を付ける -->
<tr class="table-success">...</tr>
<tr class="table-danger">...</tr>
<tr class="table-warning">...</tr>

<!-- 特定のセルに色を付ける -->
<td class="table-primary">重要</td>

まとめ

  • table クラスで基本スタイルを適用
  • table-striped / table-hover / table-bordered で見た目を調整
  • table-dark でダークテーマ
  • table-responsive で横スクロール対応(親divに付ける)
  • table-success 等で行やセルを個別に色付け