【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 / 2018.8.29
- 【CSS】px(ピクセル)とem(エム)
- web / 2023.2.22
- 【CSS】Webフォントのちらつきをなくす方法
Googleさんのおすすめ
人気記事
- web / 2019.4.16
- 【CSS】ハンバーガーメニュー実装サンプル集(クリック時のエフェクトも集めました)
- web / 2022.2.1
- 【jQuery】アコーディオン実装サンプル10選
- web / 2019.9.27
- 【jQuery】スライダープラグイン「slick」実装サンプル集
Googleさんのおすすめ
デザインの記事
- 2023.12.08New
- 信州アップルパイ研究所_Q
- 国内、海外のウェブデザイン、パソコン・タブレット・スマートフォンでも見やすいレスポンシブ対応の「あ、いいな」と思うウェブデザインを集めています。
- デザインのこと – Web design gallery
- 2022.06.15
- ちょっと思い出しただけ
- (C)2022『ちょっと思い出しただけ』製作委員会 もくじ 『ちょっと思い出しただけ』 あ…
- いつかのこと – 無印良品の家と暮らす
- 2023.11.30
- 【jQuery】スクロールして、指定した場所から出てきて、指定した場所で消える
- 指定した場所で出したいのですよー そして指定した場所で消したいのですねー もくじ 実…
- 125naroom / デザインするところ(会社)です。