レスポンシブでの表組(table)表示どうしてる?
テーブルを使って表組を制作しているときに、

スマホ版ではみでちゃうけど、どうしよう
と思ったことありませんか? この記事ではレスポンシブで使用できる基本的なパターンを3つご紹介します。
テーブルを使って表組を制作しているときに、

と思ったことありませんか? この記事ではレスポンシブで使用できる基本的なパターンを3つご紹介します。
この記事は「2021年01月02日」に書かれた記事です
紹介するテーブルについて
| 1 | 1-1 | 1-2 |
|---|---|---|
| 2 | 2-1 | 2-2 |
| 1 | 1-1 | 1-2 |
|---|---|---|
| 2 | 2-1 | 2-2 |
| 1 | 1-1 | 1-2 |
|---|---|---|
| 2 | 2-1 | 2-2 |
<table> <tr> <th>1</th> <td>1-1<td> <td>1-2<td> </tr> <tr> <th>1</th> <td>1-1<td> <td>1-2<td> </tr> </table>
@media screen and (max-width: 768px) {
th,td {
width: 100%;
display:block;
}
}

と思うことありまりませんか?そんな時は横と縦を反転させてしまいましょう
| a | b | c | d | e | |
|---|---|---|---|---|---|
| 1 | 1a | 1b | 1c | 1d | 1e |
| 2 | 2a | 2b | 2c | 2d | 2e |
| 1 | 2 | |
|---|---|---|
| a | 1a | 2a |
| b | 1b | 2b |
| c | 1c | 2c |
| d | 1d | 2d |
| e | 1e | 2e |
| a | b | c | d | e | |
|---|---|---|---|---|---|
| 1 | 1a | 1b | 1c | 1d | 1e |
| 2 | 2a | 2b | 2c | 2d | 2e |
<table> <tbody> <tr> <th> </th> <th>a</th> <th>b</th> <th>c</th> <th>d</th> <th>e</th> </tr> <tr> <th>1</th> <td>1a</td> <td>1b</td> <td>1c</td> <td>1d</td> <td>1e</td> </tr> <tr> <th>2</th> <td>2a</td> <td>2b</td> <td>2c</td> <td>2d</td> <td>2e</td> </tr> </tbody> </table>
CSS
@media screen and (max-width: 768px) {
tr{
float:left;
width: 33%;
}
th,td {
-webkit-box-sizing: border-box;
box-sizing: border-box;
display:block;
height: 30px;
}
th:not(:first-child) th:first-child, td {
border-left: 1px solid #EEE;
}
tr:not(:first-child) th:first-child, td {
border-left: 1px solid #EEE;
}
}
上記の問題点は高さを決め打ちにしないといけないため、文章が複数行になる場合などの高さ調整が必要になることです。
スクロールを表示させたいテーブルを<div class=”table-scroll”></div>内に表示させます。
※下記はスマートフォン版でスクロールを表示していますが、メディアクエリをはずせば、パソコン版でもスクロールが表示されるようになります。
| 項目1 | 項目2 | 項目3 | 項目4 | 項目5 | 項目6 | 項目7 | 項目8 | 項目9 | 項目10 |
|---|---|---|---|---|---|---|---|---|---|
| 項目1の内容 | 項目2の内容 | 項目3の内容 | 項目4の内容 | 項目5の内容 | 項目6の内容 | 項目7の内容 | 項目8の内容 | 項目9の内容 | 項目10の内容 |
HTML
<div class="scroll">
<table>
<tbody>
<tr>
<th>項目1</th>
<th>項目2</th>
<th>項目3</th>
<th>項目4</th>
<th>項目5</th>
<th>項目6</th>
<th>項目7</th>
<th>項目8</th>
<th>項目9</th>
<th>項目10</th>
</tr>
<tr>
<td>項目1の内容</td>
<td>項目2の内容</td>
<td>項目3の内容</td>
<td>項目4の内容</td>
<td>項目5の内容</td>
<td>項目6の内容</td>
<td>項目7の内容</td>
<td>項目8の内容</td>
<td>項目9の内容</td>
<td>項目10の内容</td>
</tr>
</tbody>
</table>
</div>
CSS
@media screen and (max-width: 768px) {
.scroll {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
}
参考例はシンプルなので何とかなりますが、

という場合、頭を抱えてしまうこともあります。
また、項目と内容のバランスで、スクロースしているうちにこの列はなんの項目だったっけ? とわからなくなってしまうこともあります。そんな時は、項目を固定して表示することもできます。
| 項目1 | 項目1の内容 | 項目1の内容(2) | 項目1の内容(3) | 項目1の内容(4) | 項目1の内容(5) | 項目1の内容(6) | 項目1の内容(7) | 項目1の内容(8) | 項目1の内容(9) |
|---|---|---|---|---|---|---|---|---|---|
| 項目2 | 項目2の内容 | 項目2の内容(2) | 項目2の内容(3) | 項目2の内容(4) | 項目2の内容(5) | 項目2の内容(6) | 項目2の内容(7) | 項目2の内容(8) | 項目2の内容(9) |
HTML
<div class="sccroll">
<table class="sapm-table05">
<tbody>
<tr>
<th>項目1</th>
<td>項目1の内容</td>
<td>項目1の内容(2)</td>
<td>項目1の内容(3)</td>
<td>項目1の内容(4)</td>
<td>項目1の内容(5)</td>
<td>項目1の内容(6)</td>
<td>項目1の内容(7)</td>
<td>項目1の内容(8)</td>
<td>項目1の内容(9)</td>
</tr>
<tr>
<th>項目2</th>
<td>項目2の内容</td>
<td>項目2の内容(2)</td>
<td>項目2の内容(3)</td>
<td>項目2の内容(4)</td>
<td>項目2の内容(5)</td>
<td>項目2の内容(6)</td>
<td>項目2の内容(7)</td>
<td>項目2の内容(8)</td>
<td>項目2の内容(9)</td>
</tr>
</tbody>
</table>
</div>
CSS
.scroll {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
table {
width: 1500px;
border-top: 1px solid #CCC;
border-left:1px solid #CCC;
}
th {
width: 150px;
text-align: center;
background-color :#EEE;
border-right: 1px solid #CCC;
border-bottom: 1px solid #CCC;
position: sticky;
left: 0;
}
th::before{
content: "";
position: absolute;
top: -1px;
left: -1px;
width: 100%;
height: 100%;
border: 1px solid #ccc;
}
td {
width: 150px;
background-color :#FFF;
border-right: 1px solid #CCC;
border-bottom: 1px solid #CCC;
}
thにposition: sticky;を付与してleft:0; で固定しています。
次にわかりやすいテーブルを作ってみましょう「4-10 CSSでテーブル(表)を分かりやすくしよう(近日公開予定)」
HOME > ホームページ制作 > CSSでホームページを1UP > レスポンシブでの表組(table)表示どうしてる?