Bootstrap3でテーブルを角丸にする方法

  • 公開日:2014/3/12
この記事は最終更新日から9年以上が経過しています。

Bootstrapでは、以下のようにtableタグのクラスに.table-borderedを追加するとテーブルに線を引くことができます。
この記述はBootstrap2でも3でも共通です。

<table class=&quot;table table-bordered&quot;>
  ...
</table>

だけど、表示されたテーブルをみると、Bootstrap2では角丸ですが、Bootstrap3では角丸ではありません。
Bootstrap3でも角丸にしたい場合は、bootstrap.min.cssよりも後に読み込んでいる任意のcssファイルに以下を記述すると上手くいきました。

.table-bordered {
    border: 1px solid #dddddd;
    border-collapse: separate;
    *border-collapse: collapse;
    border-left: 0;
    border-radius: 4px;
}
.table-bordered>thead>tr>th,
.table-bordered>tbody>tr>th,
.table-bordered>tfoot>tr>th,
.table-bordered>thead>tr>td,
.table-bordered>tbody>tr>td,
.table-bordered>tfoot>tr>td {
    border-right: none;
    border-bottom: none;
}
.table-bordered th,
.table-bordered td {
    border-left: 1px solid #dddddd;
}
.table-bordered thead:first-child tr:first-child th,
.table-bordered tbody:first-child tr:first-child th,
.table-bordered tbody:first-child tr:first-child td {
    border-top: 0;
}
.table-bordered thead:first-child tr:first-child > th:first-child,
.table-bordered tbody:first-child tr:first-child > td:first-child,
.table-bordered tbody:first-child tr:first-child > th:first-child {
    border-top-left-radius: 4px;
}
.table-bordered thead:first-child tr:first-child > th:last-child,
.table-bordered tbody:first-child tr:first-child > td:last-child,
.table-bordered tbody:first-child tr:first-child > th:last-child {
    border-top-right-radius: 4px;
}
.table-bordered thead:last-child tr:last-child > th:first-child,
.table-bordered tbody:last-child tr:last-child > td:first-child,
.table-bordered tbody:last-child tr:last-child > th:first-child,
.table-bordered tfoot:last-child tr:last-child > td:first-child,
.table-bordered tfoot:last-child tr:last-child > th:first-child {
    border-bottom-left-radius: 4px;
}
.table-bordered thead:last-child tr:last-child > th:last-child,
.table-bordered tbody:last-child tr:last-child > td:last-child,
.table-bordered tbody:last-child tr:last-child > th:last-child,
.table-bordered tfoot:last-child tr:last-child > td:last-child,
.table-bordered tfoot:last-child tr:last-child > th:last-child {
    border-bottom-right-radius: 4px;
}
.table-bordered tfoot + tbody:last-child tr:last-child td:first-child {
    border-bottom-left-radius: 0;
}
.table-bordered tfoot + tbody:last-child tr:last-child td:last-child {
    border-bottom-right-radius: 0;
}

2014.04.05 追記
clicktxさんにご指摘いただき、cssを追記しなくても角丸にできる方法がわかりました。
ありがとうございます!勉強になりました!
その方法は・・・
panelクラスを利用します。

<div class=&quot;panel panel-default&quot;>
  <table class=&quot;table table-bordered&quot;>
    ...
  </table>
</div>

公式サイトも参考にしてください。
Panels : Components – Bootstrap