【CSS】tableをレスポンシブ対応する、2列を1列に切り替える
- 2020.10.27
- CSS


tableを1列にレスポンシブしたいんですけどー

CSSだけで簡単にできますよー
2列を1列に切り替える
▽只今の設定。
768px以上(iPadとかのタブレット、PC)は2列。
767px以下(スマホとか)は1列。
See the Pen
【CSS】tableをレスポンシブ対応する、2列を1列に切り替える by 125naroom (@125naroom)
on CodePen.
HTMLとCSSはこちら
HTML
<table> <tr> <th>名前</th> <td>テキストテキストテキストテキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキストテキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト<br>テキストテキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト<br>テキストテキストテキスト</td> </tr> </table>
CSS
table { width: 100%; border-collapse: collapse; border-spacing: 0; font-size: 14px; } table th { width: 22%; background: #fff27d; border: 1px solid #ccc; box-sizing: border-box; padding: 15px; vertical-align: middle; font-weight: bold; text-align: center; } table td { background: #fff; border: 1px solid #ccc; box-sizing: border-box; padding: 15px; vertical-align: top; text-align: left; } @media all and (max-width: 767px) { table th, table td { display: block; width: 100%; border-bottom: none; } table tr:last-child td:last-child { border-bottom: 1px solid #ccc; } }
3列を1列に切り替える
▽只今の設定。
なんと。
2列を1列に切り替えると同じCSSで問題なしです。
See the Pen
【CSS】tableをレスポンシブ対応する、3列を1列に切り替える by 125naroom (@125naroom)
on CodePen.
HTMLとCSSはこちら
HTML
<table> <tr> <th>名前</th> <td>テキストテキストテキストテキストテキスト</td> <td>テキスト<br>テキストテキスト<br>テキストテキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト<br>テキストテキストテキスト</td> <td>テキストテキストテキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> <td>テキストテキストテキスト<br>テキストテキスト<br>テキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> <td>テキストテキスト<br>テキスト<br>テキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト<br>テキストテキストテキスト</td> <td>テキストテキストテキストテキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト<br>テキストテキストテキスト</td> <td>テキストテキストテキスト<br>テキストテキスト<br>テキスト</td> </tr> </table>
CSS
table { width: 100%; border-collapse: collapse; border-spacing: 0; font-size: 14px; } table th { width: 22%; background: #fff27d; border: 1px solid #ccc; box-sizing: border-box; padding: 15px; vertical-align: middle; font-weight: bold; text-align: center; } table td { background: #fff; border: 1px solid #ccc; box-sizing: border-box; padding: 15px; vertical-align: top; text-align: left; } @media all and (max-width: 767px) { table th, table td { display: block; width: 100%; border-bottom: none; } table tr:last-child td:last-child { border-bottom: 1px solid #ccc; } }
だったら、4列を1列に切り替える
▽只今の設定。
なんと。
なんと。
こちらも、2列を1列に切り替えると同じCSSで問題なしです。
See the Pen
【CSS】tableをレスポンシブ対応する、4列を1列に切り替える by 125naroom (@125naroom)
on CodePen.
HTMLとCSSはこちら
HTML
<table> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> </tr> <tr> <th>名前</th> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> <td>テキスト<br>テキストテキスト</td> </tr> </table>
CSS
table { width: 100%; border-collapse: collapse; border-spacing: 0; font-size: 14px; } table th { width: 22%; background: #fff27d; border: 1px solid #ccc; box-sizing: border-box; padding: 15px; vertical-align: middle; font-weight: bold; text-align: center; } table td { background: #fff; border: 1px solid #ccc; box-sizing: border-box; padding: 15px; vertical-align: top; text-align: left; } @media all and (max-width: 767px) { table th, table td { display: block; width: 100%; border-bottom: none; } table tr:last-child td:last-child { border-bottom: 1px solid #ccc; } }
さいごに

わー、できましたー

テーブルレスポンシブには最強のスタイル(CSS)だったりしますー
おすすめ
かんれん
- web / 2019.4.16
- 【CSS】ハンバーガーメニュー実装サンプル集(クリック時のエフェクトも集めました)
- web / 2019.10.31
- 【CSS】レスポンシブ対応の三角形を作る
- web / 2019.1.17
- 【CSS】プラスとマイナスをつくる
- web / 2019.7.19
- 【CSS】olのリストで①、②、③(丸数字)を表示させる
Googleさんのおすすめ
人気記事
- web / 2019.4.16
- 【CSS】ハンバーガーメニュー実装サンプル集(クリック時のエフェクトも集めました)
- web / 2019.7.22
- 【jQuery】アコーディオン実装サンプル10選
- web / 2019.9.27
- 【jQuery】スライダープラグイン「slick」実装サンプル集
- web / 2019.7.24
- 【jQuery】ページ内リンクでスムーズスクロール(スクロール位置の調整も簡単にできる)
- web / 2019.7.19
- 【CSS】olのリストで①、②、③(丸数字)を表示させる
- web / 2019.10.2
- 【jQuery】モーダルプラグイン『Remodal』の実装サンプル集
- web / 2019.1.22
- 【CSS】三角アイコンと矢印アイコンをつくる
Googleさんのおすすめ
デザインの記事
- 2021.01.12New
- カラーストーリーアドミオ
- 国内、海外のウェブデザイン、パソコン・タブレット・スマートフォンでも見やすいレスポンシブ対応の「あ、いいな」と思うウェブデザインを集めています。
- デザインのこと – Web design gallery
- 2021.01.12New
- カラーストーリーアドミオ
- 国内、海外のウェブデザイン、パソコン・タブレット・スマートフォンでも見やすいレスポンシブ対応の「あ、いいな」と思うウェブデザインを集めています。
- デザインのこと – Web design gallery
- 2021.01.07
- 玄関ドアの松飾
- 窓の家にもちょっとおめかし 玄関ドアに松飾り 赤、白、緑 お正月っていつまでなの? …
- いつかのこと – 無印良品の家と暮らす
- 2021.01.06
- 2021
- 5、4、3、2、1、0 ぴょん 子どもの頃、新年が始まる瞬間は必ずジャンプしていた いつか…
- いつかのこと – 無印良品の家と暮らす