テーブルをメインに使用しているコンテンツをレスポンシブ対応する必要があるので、色々調べてみて実装してみました。
目次
参考にしたサイトと実装デモ
色々見てみた中で、自分なりに一番しっくりしたレスポンシブ対応の参考サイトはこちら。
これを基に実装したテーブルのデモはこちらになります。
各テーブルの説明は下記になります。
縦並びの見出しテーブル
参考サイトとは関係ありませんが、一応縦並びの見出しテーブルのレスポンシブ対応も実装してみました。
会社概要風のテーブル
会社概要とかでよく使われている感じの一般的なテーブルです。
通常
レスポンシブ対応
見出しを上段、コンテンツを下段という形にしています。
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<table class="col-head-type1"> <tr> <th>見出し1</th> <td>コンテンツ</td> </tr> <tr> <th>見出し2</th> <td>コンテンツコンテンツコンテンツ</td> </tr> <tr> <th>見出し3</th> <td>コンテンツコンテンツ</td> </tr> <tr> <th>見出し4</th> <td>コンテンツコンテンツコンテンツ</td> </tr> <tr> <th>見出し5</th> <td>コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツ</td> </tr> </table> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
.col-head-type1 { border-collapse: collapse; width: 100%; } .col-head-type1 th, .col-head-type1 td { border: 1px solid #dbe1e8; padding: 8px; } .col-head-type1 th { background: #f9fafc; text-align: left; width: 120px; } @media only screen and (max-width: 800px) { .col-head-type1 tr, .col-head-type1 th, .col-head-type1 td { display: block; width: auto; } .col-head-type1 tr:first-child { border-top: 1px solid #dbe1e8; } .col-head-type1 th, .col-head-type1 td { border-top: none; } } |
ポイントは19-23行目でtr、th、tdのdisplayをblockに指定しているくらいです。
フォーム風のテーブル
下図のようなタイプのレスポンシブ対応も欲しかったので実装してみました。
通常
レスポンシブ対応
最初のと同じ感じですが、見出しとコンテンツの間を詰めています。
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<table class="col-head-type2"> <tr> <th>見出し1</th> <td><input type="text" value=""></td> </tr> <tr> <th>見出し2</th> <td><input type="text" value=""></td> </tr> <tr> <th>見出し3</th> <td> <input type="checkbox" name="ct1-check1" value="1"><label>コンテンツ</label> <input type="checkbox" name="ct1-check2" value="1"><label>コンテンツ</label> </td> </tr> <tr> <th>見出し4</th> <td> <input type="radio" name="ct1-radio" value="1"><label>コンテンツ</label> <input type="radio" name="ct1-radio" value="2"><label>コンテンツ</label> </td> </tr> <tr> <th>見出し5</th> <td><textarea></textarea></td> </tr> </table> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
.col-head-type2 { border-collapse: collapse; width: 100%; } .col-head-type2 tr { border-bottom: 1px solid #dbe1e8; } .col-head-type2 tr:first-child { border-top: 1px solid #dbe1e8; } .col-head-type2 th, .col-head-type2 td { padding: 8px 0; } .col-head-type2 th { text-align: left; width: 120px; } .col-head-type2 input[type='text'], .col-head-type2 textarea { width: 50%; } .col-head-type2 textarea { height: 100px; } @media only screen and (max-width: 800px) { .col-head-type2 tr, .col-head-type2 th, .col-head-type2 td { display: block; width: auto; } .col-head-type2 th { padding-bottom: 0; } .col-head-type2 input[type='text'], .col-head-type2 textarea { width: 100%; } } |
横並びの見出しテーブル
参考サイトを基に、自分の環境に合わせて実装したテーブルです。
3パターンありますがCSSは共通なので、ここでCSSを記載しておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
.row-head { border-collapse: collapse; vertical-align: middle; width: 100%; } .row-head th, .row-head td { border: 1px solid #dbe1e8; padding: 8px; } .row-head th { background: #f9fafc; } .row-head label { line-height: 34px; margin: 0; } @media only screen and (max-width: 800px) { .row-head thead { display: none; } .row-head tr, .row-head td { display: block; width: auto; } .row-head tr { border: 1px solid #dbe1e8; border-radius: 5px; box-shadow: 3px 3px rgba(0, 0, 0, .1); margin-bottom: 20px; padding: 8px 8px 0; } .row-head td { border: none; border-bottom: 1px solid #dbe1e8; display: flex; justify-content: space-between; text-align: right; } .row-head td:last-child { border-bottom: none; } .row-head td::before { content: attr(aria-label); display: inline-block; font-weight: bold; float: left; text-align: left; padding-right: 20px; white-space: nowrap; } .row-head td.bt-area::before { display: none; } .row-head td.bt-area a { background: #007aff; border-radius: 5px; color: #fff; display: inline-block; flex: 1; height: 40px; margin: 0 2px; overflow: hidden; width: auto; } .row-head td.bt-area a::before { align-items: center; content: attr(aria-label); display: flex; font-size: 14px; height: 100%; justify-content: center; line-height: 40px; width: 100%; } .row-head.header-check tr { padding-left: 40px; position: relative; } .row-head.header-check td:first-child { align-items: center; background: #fff; border-right: 1px solid #dbe1e8; border-bottom: none; bottom: 0; display: flex; justify-content: center; left: 0; padding: 0; position: absolute; text-align: center; top: 0; width: 40px; } .row-head.header-check td:first-child::before { display: none; } } |
61-84行目は2つ目のテーブル用、さらにその下の86-107行目は3つ目のテーブル用のCSSで、それ以外は共通という感じになっています。
テキスト情報のみのテーブル
手始めにまんまな感じで実装してみました。
通常
レスポンシブ対応
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<table class="row-head"> <thead> <tr> <th width="60" class="center">#</th> <th width="80">見出し1</th> <th width="150">見出し2</th> <th width="80">見出し3</th> <th width="150">見出し4</th> <th>見出し5</th> </tr> </thead> <tbody> <tr> <td aria-label="#">0001</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツ</td> <td aria-label="見出し5">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテン</td> </tr> <tr> <td aria-label="#">0002</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツ</td> <td aria-label="見出し5">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテン</td> </tr> <tr> <td aria-label="#">0003</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツ</td> <td aria-label="見出し5">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテン</td> </tr> </tbody> </table> |
主なポイントは、見出し部分を<thead>で囲み、レスポンシブ対応時にdisplay: none;している感じです。
また、<td>要素にaria-label属性を付けて、レスポンシブ対応時にCSSで値を読み込んで見出しとして表示しています。
管理画面風に操作ボタンがあるタイプ
右端に操作ボタンがあるタイプのテーブルのレスポンシブ対応が必要だったのでカスタマイズして実装してみました。
通常
レスポンシブ対応
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<table class="row-head"> <thead> <tr> <th width="60" class="center">#</th> <th width="80">見出し1</th> <th width="150">見出し2</th> <th width="80">見出し3</th> <th>見出し4</th> <th width="80">操作</th> </tr> </thead> <tbody> <tr> <td aria-label="#">0001</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツ</td> <td align="center" class="bt-area"> <a href="#" class="mt-icon edit-icon" aria-label="編集"></a> <a href="#" class="mt-icon delete-icon" aria-label="削除"></a> </td> </tr> <tr> <td aria-label="#">0002</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツ</td> <td align="center" class="bt-area"> <a href="#" class="mt-icon edit-icon" aria-label="編集"></a> <a href="#" class="mt-icon delete-icon" aria-label="削除"></a> </td> </tr> <tr> <td aria-label="#">0003</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツ</td> <td align="center" class="bt-area"> <a href="#" class="mt-icon edit-icon" aria-label="編集"></a> <a href="#" class="mt-icon delete-icon" aria-label="削除"></a> </td> </tr> </tbody> </table> |
ボタン欄となる<td>要素にclass=”bt-area”を付けてやる事でボタンとしてレイアウトするように対応しています。
一括操作出来る風のテーブル
左端にチェックボックスがあるパターンのテーブルです。このタイプもそれらしいレスポンシブ対応になるようにカスタマイズしてみました。
通常
レスポンシブ対応
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<table class="row-head header-check"> <thead> <tr> <th width="40"></th> <th width="60">#</th> <th width="80">見出し1</th> <th width="150">見出し2</th> <th width="80">見出し3</th> <th>見出し4</th> <th width="80">操作</th> </tr> </thead> <tbody> <tr> <td align="center"><input type="checkbox"><label></label></td> <td aria-label="#">0001</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツ</td> <td align="center" class="bt-area"> <a href="#" class="icon-button mt-icon edit-icon" aria-label="編集"></a> <a href="#" class="icon-button mt-icon more-icon" aria-label="詳細"></a> <a href="#" class="icon-button mt-icon delete-icon" aria-label="削除"></a> </td> </tr> <tr> <td align="center"><input type="checkbox"><label></label></td> <td aria-label="#">0002</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツ</td> <td align="center" class="bt-area"> <a href="#" class="icon-button mt-icon edit-icon" aria-label="編集"></a> <a href="#" class="icon-button mt-icon more-icon" aria-label="詳細"></a> <a href="#" class="icon-button mt-icon delete-icon" aria-label="削除"></a> </td> </tr> <tr> <td align="center"><input type="checkbox"><label></label></td> <td aria-label="#">0003</td> <td aria-label="見出し1">コンテンツ</td> <td aria-label="見出し2">コンテンツコンテンツ</td> <td aria-label="見出し3">コンテンツ</td> <td aria-label="見出し4">コンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツコンテンツ</td> <td align="center" class="bt-area"> <a href="#" class="icon-button mt-icon edit-icon" aria-label="編集"></a> <a href="#" class="icon-button mt-icon more-icon" aria-label="詳細"></a> <a href="#" class="icon-button mt-icon delete-icon" aria-label="削除"></a> </td> </tr> </tbody> </table> |
<table>にheader-checkクラスを付けています。このクラスで最初の<td>要素を左寄せ・縦長に変化させています。