<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Creating visually appealing responsive tables</title>
<!--
# Creating visually appealing responsive tables
- Ensure titles and contents wrap as little as possible.
- Display content width-wise, avoiding extra space around tables.
- Center tables on the screen.
- Ensure readability even on narrow screens.
# 見た目の良いレスポンシブなテーブルを作成
- タイトルも内容もできる限り折り返されることがないように表示
- テーブルの幅に余白ができないようにコンテンツの幅で表示
- テーブルを画面の中央に表示
- 幅の狭い画面でも見やすく表示
Example classes:
<table class="table-sc full-width center horizontal zebra gray hover">
...
</table>
-->
<style>
/* Center the table */
/* テーブルを中央寄せにする */
* {
box-sizing: border-box;
}
.nowrap {
/*
white-space: nowrap;
*/
display: inline-table;
}
html {
background-color: #f7f7f7;
width: 100%;
height: 100%;
}
body {
background-color: #efcccc;
max-width: 96%;
width: 1140px;
height: 100%;
margin: 0 auto;
}
page {
display: block;
background-color: #e7ee00;
width: 1040px;
max-width: 96%;
height: 100%;
margin: 0 auto;
}
main {
display: block;
background-color: #eee;
max-width: 96%;
width: 960px;
height: 100%;
margin: 0 auto;
}
.center {
display: block;
margin: 10px auto 30px auto;
}
.table-sc {
display: table;
}
.table-sc tbody {
display: grid;
grid-template-columns: max-content 1fr;
width: fit-content;
}
/* If you want the table to occupy the full width of its parent */
/* テーブルの幅を親要素の幅一杯にしたい場合 */
.table-sc.full-width tbody {
grid-template-columns: minmax(20px, auto) 1fr;
width: auto;
}
.table-sc tr {
display: contents;
}
.table-sc th,
.table-sc td {
padding: 8px;
}
.table-sc th {
text-align: left;
font-weight: normal;
}
.table-sc.align-center td {
text-align: center !important;
}
.table-sc.border td {
border: 1px solid #555 !important;
padding: 1% 2%;
}
.table-sc.clear {
grid-template-columns: minmax(4px, auto) 1fr;
border: none;
}
.table-sc.clear td {
border: none !important;
}
.table-sc.clear td:first-child {
padding-left: 0;
}
.table-sc.clear td:last-child {
padding-right: 0;
}
.table-sc.zebra tr:nth-child(odd) th,
.table-sc.zebra tr:nth-child(odd) td {
background: #fcfcfc !important;
}
.table-sc.zebra tr:nth-child(even) th,
.table-sc.zebra tr:nth-child(even) td {
background: #eee !important;
}
.table-sc.hover tr:hover th,
.table-sc.hover tr:hover td {
background: #ffffdc !important;
}
.table-sc.gray tbody {
text-shadow: 1px 1px 0px #fff;
background: #eaebec;
margin: 20px 0 20px 4px;
border: #ccc 1px solid;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
-moz-box-shadow: 0 1px 2px #d1d1d1;
-webkit-box-shadow: 0 1px 2px #d1d1d1;
box-shadow: 0 1px 2px #d1d1d1;
}
.table-sc.gray th {
border-top: 1px solid #fafafa;
border-right: none;
border-bottom: 1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #ededed;
background: -webkit-gradient(linear, left top, left bottom, from(#ededed), to(#ebebeb));
background: -moz-linear-gradient(top, #ededed, #ebebeb);
}
.table-sc.gray th {
-moz-border-radius-topleft: 3px;
-webkit-border-top-left-radius: 3px;
border-top-left-radius: 3px;
}
.table-sc.gray td {
-moz-border-radius-topright: 3px;
-webkit-border-top-right-radius: 3px;
border-top-right-radius: 3px;
}
.table-sc.gray td:nth-child(2n-1) {
border-left: none;
}
.table-sc.gray th,
.table-sc.gray td {
border-top: none;
border-right: none;
border-bottom: 1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #fafafa;
background: -webkit-gradient(linear, left top, left bottom, from(#fbfbfb), to(#fafafa));
background: -moz-linear-gradient(top, #fbfbfb, #fafafa);
}
.table-sc.gray tr:nth-child(even) th,
.table-sc.gray tr:nth-child(even) td {
background: #f2f2f2;
background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#f2f2f2));
background: -moz-linear-gradient(top, #f8f8f8, #f2f2f2);
}
.table-sc.gray tr:first-child th {
-moz-border-radius-topleft: 3px;
-webkit-border-top-left-radius: 3px;
border-top-left-radius: 3px;
}
.table-sc.gray tr:last-child th {
-moz-border-radius-bottomleft: 3px;
-webkit-border-bottom-left-radius: 3px;
border-bottom-left-radius: 3px;
}
.table-sc.gray tr:first-child td {
-moz-border-radius-topright: 3px;
-webkit-border-top-right-radius: 3px;
border-top-right-radius: 3px;
}
.table-sc.gray tr:last-child td {
-moz-border-radius-bottomright: 3px;
-webkit-border-bottom-right-radius: 3px;
border-bottom-right-radius: 3px;
}
.table-sc caption {
display: block;
font-weight: bold;
margin: 10px auto;
padding: 4px;
}
.table-sc.horizontal caption {
border-bottom: 3px solid #ccc !important;
}
.table-sc.horizontal tbody {
border-top: none !important;
border-right: none !important;
border-bottom: none !important;
border-left: none !important;
}
.table-sc.horizontal th,
.table-sc.horizontal td {
border-top: none !important;
border-right: none !important;
border-bottom: 1px solid #ccc !important;
border-left: none !important;
}
.table-sc input {
max-width: 96%;
}
@media screen and (max-width: 640px) {
.table-sc {
display: block;
}
.table-sc tbody,
.table-sc.full-width tbody {
grid-template-columns: auto;
display: inline-block;
overflow: hidden;
display: block;
width: 100%;
}
.table-sc tr {
display: flex;
flex-direction: column;
}
.table-sc th {
font-size: .9em;
display: flex;
flex-wrap: wrap;
width: 98%;
}
.table-sc td {
padding-left: 2em;
margin-bottom: 16px;
border-bottom: none !important;
display: flex;
flex-wrap: wrap;
}
}
</style>
</head>
<body>
<page>
<main>
<article>
<table class="table-sc horizontal center hover">
<caption>Creating visually appealing responsive tables</caption>
<tbody>
<tr>
<th>Title 1</th>
<td class="td">Contents 1</td>
</tr>
<tr>
<th>Long long long long long long title 2</th>
<td class="td"><input type="text" /><span class="nowrap">Contents contents contents 2</span>
</td>
</tr>
<tr>
<th>Title 3</th>
<td class="td">Contents 3</td>
</tr>
<tr>
<th>日本語のタイトル</th>
<td class="td">日本語の内容、日本語の内容、日本語の内容。</td>
</tr>
</tbody>
</table>
</article>
</main>
</page>
<p>Return: <a href="https://memo.silverpigeon.jp/creating-visually-appealing-responsive-tables/">https://memo.silverpigeon.jp/creating-visually-appealing-responsive-tables/</a></p>
</body>
</html>