【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 / 2020.1.15
- 【CSS】簡単に固定できるposition: sticky;の実装サンプル集
- web / 2023.2.23
- 【CSS】『shape-outside』でテキストの回り込みを円形にしてみる
- web / 2020.1.8
- 【CSS】Flexboxの使い方と実装サンプル集
Googleさんのおすすめ
人気記事
- web / 2019.4.16
- 【CSS】ハンバーガーメニュー実装サンプル集(クリック時のエフェクトも集めました)
- web / 2022.2.1
- 【jQuery】アコーディオン実装サンプル10選
- web / 2019.9.27
- 【jQuery】スライダープラグイン「slick」実装サンプル集
Googleさんのおすすめ
デザインの記事
- 2023.06.05New
- NEWTOWN/グラフィック・Webデザイン
- 国内、海外のウェブデザイン、パソコン・タブレット・スマートフォンでも見やすいレスポンシブ対応の「あ、いいな」と思うウェブデザインを集めています。
- デザインのこと – Web design gallery
- 2022.06.15
- ちょっと思い出しただけ
- (C)2022『ちょっと思い出しただけ』製作委員会 もくじ 『ちょっと思い出しただけ』 あ…
- いつかのこと – 無印良品の家と暮らす
- 2023.04.25
- 【CSS】見出しデザインをまとめてみる(左右に線を引くとか)
- 見出しのデザインをまとめてくださーい わ、わかりましたー もくじ 左右に線を引く 左…
- 125naroom / デザインするところ(会社)です。