Home アーカイブ 【初心者の方向け】 Bootstrap4の使い方 テーブル 【初心者の方向け】 Bootstrap4の使い方 テーブル2019/03/21archivebootstrap4 目次 →基本的なテーブル→テーブルの背景に色をつける→交互に色変える→テーブル全体に罫線を付ける→テーブルを小さくコンパクトに→背景色について→レスポンシブでのテーブル表示今回は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-○○を追記することによりテーブルに背景色を指定する事ができます。にはthead-○○で色を指定します。下のテーブルにはにtable-lightをにthread-darkにしています。 #見出し1見出し2見出し31内容1内容2内容32内容1内容2内容3 コード<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> 交互に色変える交互に色を変えるには要素にtable-stripedを追加します。 #見出し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> テーブル全体に罫線を付ける要素にtable-borderedでテーブルとセルのすべての辺の境界線を追加します。#見出し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> 上記とは逆に要素にtable-borderlessで境界線無しになります。 これは背景色が暗くても使用可能です。 #見出し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-hoverを追加するとホバーが有効になります。 #見出し1見出し2見出し31内容1内容2内容32内容1内容2内容3 コード<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> テーブルを小さくコンパクトに要素にtable-smを追加することで、セル内に設定されているpaddingを半分にしコンパクトに表示させます。 #見出し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 レスポンシブでのテーブル表示要素にtable-responsiveのクラスで囲うことにより、ブレークポイントに応じて水平にスクロールされるテーブルを表示させることができます。<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セルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセルセル Share:B! RU DESIGNの無料Webツール集 画像処理、テキスト整形、AIツールなど、制作や作業を効率化する便利ツールをブラウザで無料公開中! ツールを見る → Contact & Request お問い合わせ・お仕事のご相談 Webサイト制作、システム開発、サイト保守・改善などのご依頼や技術的なご相談を承っております。専用フォームよりお気軽にご連絡ください(2営業日以内にご返信いたします)。 フォームより24時間受付 初回ご相談・お見積り無料 全国オンライン対応 フォームから問い合わせる 制作の流れ・料金について AUTHORRU DESIGNWebサイト制作・システム開発・保守運用を手がけています。一般からナイト系まで幅広く対応し、制作で培った実践的なノウハウや技術知見、自作Webツールも公開中。制作のご相談もお気軽にどうぞ。 前の記事 【初心者の方向け】 Bootstrap4の使い方 ボーダー 次の記事 【初心者の方向け】 Bootstrap4の使い方 ボタン作成 関連記事 一覧を見る →2020/05/14Bootstrap4.5が公開されました2019/03/28【初心者の方向け】 Bootstrap4の使い方 フォーム2019/03/28【初心者の方向け】 Bootstrap4の使い方 スピナー2019/03/27【初心者の方向け】 Bootstrap4の使い方 カルーセル2019/03/27【初心者の方向け】 Bootstrap4の使い方 ドロップダウン コラム・雑記 一覧を見る →2026/10/25Xはインプレッション収益から変わるのか? X Moneyと「いいね」送金構想2026/10/23PHPの重大な脆弱性とは?高市首相の注意喚起とテレビの「家の鍵」解説2026/10/08MacBookを80%充電で使う。シンプルなバッテリー管理アプリ「Battery」2026/10/06不正アクセスによる情報流出はなぜ起こる?最近の事例から仕組みと対策を考える2026/10/05阿部寛のホームページ、何がすごい?古いのに今も使える理由を見てみた