ブログやWebサイトに必要不可欠なテーブル!
データをまとめる時などに使いますがデータ量が多いとまぁ入りきらないですよね。
パソコンで見たときは入っていてもスマホで見るとぐちゃぁと崩れてることもしばしば。
そんな時の対処法は「横スクロールを可能にして見えないところは隠す」がメジャーかと思います。
今回はそれの発展バージョンです。デモはこちら!
特徴をまとめると以下の通りです。
スクロールヒントは指のやつです。スクロールできるかどうか一発でわかりますね!
注意点としてはInternetExplorerでは見出し固定がされません。InternetExplorerのシェアは10%強ですがPCユーザーが多いサイト以外はそんな気にする必要ないと思います。
コピペで簡単にできるようになってるのでテーブルに困ってる方はぜひやってみてください。
この記事はWordPressユーザー向けに書いています。
見出し固定、かつスクロールヒントを表示させるテーブル
手順は以下の通りです。
手順
STEP1
CSSコピペ
style.cssにコードをコピペ
STEP2
function.phpコピペ
スクロールヒント使いたい人限定
STEP3
HTMLコピペ
投稿画面でHTMLコピペ
STEP1:CSSをコピペ
下記CSSをコピペしていきます。
メモ
「外観→テーマエディター→style.css(子テーマ)」。もしくは、「外観→カスタマイズ→追加CSS」に貼り付けてください。
CSSが反映されない場合はスーパーリロード(Ctrl+F5orShift+F5)してください。
右上に出てくるマークを押すとコピペしやすいです
/************************************
** 横長テーブル
************************************/
.p-sticky-table{
white-space: nowrap;
line-height:1.6;
}
.p-sticky-table table{
border:none;
border-left:1px solid #dedede;
border-right:1px solid #dedede;
border-bottom: 1px solid #dedede;
border-collapse: collapse;
word-break: break-all;
table-layout: fixed;
display:block;
overflow:scroll;
}
.p-sticky-table th{
background:#f0f9ff !important; /* 見出し背景色 */
color: #666;/* 見出し文字色 */
font-weight: 500 !important;/* 見出し文字太さ */
font-size:13px !important; /* 見出し文字サイズ */
}
.p-sticky-table td{
font-size: 13px !important; /* 文字サイズ */
color:#555; /* 文字色 */
}
.p-sticky-table thead th:first-child,.p-sticky-table tbody th:first-child{
position: -webkit-sticky;
position: sticky;
left: 0;
z-index: 1;
border:none;
}
.p-sticky-table tr{
border-top:1px solid #dedede;
border-bottom:none !important;
}
.p-sticky-table tr:last-child{
border-bottom:1px solid #dedede;
}
.p-sticky-table th, .p-sticky-table td {
vertical-align:middle !important;
border: none !important;
background:#fff;
text-align:center;
padding:12px !important;
}
.p-sticky-table img{
margin:0;
padding:0;
max-width:70px !important; /* 画像最大サイズ */
}
/* スマホ */
@media screen and (max-width: 560px) {
.p-sticky-table th{
font-size:11px !important; /* スマホ見出し文字サイズ */
}
.p-sticky-table td{
font-size: 11px !important; /* スマホ文字サイズ */
}
.p-sticky-table thead th:first-child,.p-sticky-table tbody th:first-child{
white-space:normal;
min-width:80px; /* スマホ見出し横幅 */
}
.p-sticky-table th, .p-sticky-table td {
padding:8px !important;
}
.p-sticky-table img{
max-width:50px !important; /* スマホ画像最大サイズ */
}
}
- /************************************
- ** 横長テーブル
- ************************************/
- .p-sticky-table{
- white-space: nowrap;
- line-height:1.6;
- }
- .p-sticky-table table{
- border:none;
- border-left:1px solid #dedede;
- border-right:1px solid #dedede;
- border-bottom: 1px solid #dedede;
- border-collapse: collapse;
- word-break: break-all;
- table-layout: fixed;
- display:block;
- overflow:scroll;
- }
- .p-sticky-table th{
- background:#f0f9ff !important; /* 見出し背景色 */
- color: #666;/* 見出し文字色 */
- font-weight: 500 !important;/* 見出し文字太さ */
- font-size:13px !important; /* 見出し文字サイズ */
- }
- .p-sticky-table td{
- font-size: 13px !important; /* 文字サイズ */
- color:#555; /* 文字色 */
- }
- .p-sticky-table thead th:first-child,.p-sticky-table tbody th:first-child{
- position: -webkit-sticky;
- position: sticky;
- left: 0;
- z-index: 1;
- border:none;
- }
- .p-sticky-table tr{
- border-top:1px solid #dedede;
- border-bottom:none !important;
- }
- .p-sticky-table tr:last-child{
- border-bottom:1px solid #dedede;
- }
- .p-sticky-table th, .p-sticky-table td {
- vertical-align:middle !important;
- border: none !important;
- background:#fff;
- text-align:center;
- padding:12px !important;
- }
- .p-sticky-table img{
- margin:0;
- padding:0;
- max-width:70px !important; /* 画像最大サイズ */
- }
- /* スマホ */
- @media screen and (max-width: 560px) {
- .p-sticky-table th{
- font-size:11px !important; /* スマホ見出し文字サイズ */
- }
- .p-sticky-table td{
- font-size: 11px !important; /* スマホ文字サイズ */
- }
- .p-sticky-table thead th:first-child,.p-sticky-table tbody th:first-child{
- white-space:normal;
- min-width:80px; /* スマホ見出し横幅 */
- }
- .p-sticky-table th, .p-sticky-table td {
- padding:8px !important;
- }
- .p-sticky-table img{
- max-width:50px !important; /* スマホ画像最大サイズ */
- }
- }
/************************************
** 横長テーブル
************************************/
.p-sticky-table{
white-space: nowrap;
line-height:1.6;
}
.p-sticky-table table{
border:none;
border-left:1px solid #dedede;
border-right:1px solid #dedede;
border-bottom: 1px solid #dedede;
border-collapse: collapse;
word-break: break-all;
table-layout: fixed;
display:block;
overflow:scroll;
}
.p-sticky-table th{
background:#f0f9ff !important; /* 見出し背景色 */
color: #666;/* 見出し文字色 */
font-weight: 500 !important;/* 見出し文字太さ */
font-size:13px !important; /* 見出し文字サイズ */
}
.p-sticky-table td{
font-size: 13px !important; /* 文字サイズ */
color:#555; /* 文字色 */
}
.p-sticky-table thead th:first-child,.p-sticky-table tbody th:first-child{
position: -webkit-sticky;
position: sticky;
left: 0;
z-index: 1;
border:none;
}
.p-sticky-table tr{
border-top:1px solid #dedede;
border-bottom:none !important;
}
.p-sticky-table tr:last-child{
border-bottom:1px solid #dedede;
}
.p-sticky-table th, .p-sticky-table td {
vertical-align:middle !important;
border: none !important;
background:#fff;
text-align:center;
padding:12px !important;
}
.p-sticky-table img{
margin:0;
padding:0;
max-width:70px !important; /* 画像最大サイズ */
}
/* スマホ */
@media screen and (max-width: 560px) {
.p-sticky-table th{
font-size:11px !important; /* スマホ見出し文字サイズ */
}
.p-sticky-table td{
font-size: 11px !important; /* スマホ文字サイズ */
}
.p-sticky-table thead th:first-child,.p-sticky-table tbody th:first-child{
white-space:normal;
min-width:80px; /* スマホ見出し横幅 */
}
.p-sticky-table th, .p-sticky-table td {
padding:8px !important;
}
.p-sticky-table img{
max-width:50px !important; /* スマホ画像最大サイズ */
}
}
色や大きさを変更したい場合はコメントに書いてある通りに数値やカラーコードを変更してください。(!importantは残してね)
STEP2:function.phpにコードコピペ
スクロールヒントを表示させたい人限定です。アップルップルのライブラリを使って表示させます。
下記コードを子テーマのfunction.phpにコピペしてください。
必ず手元に編集前のfunction.phpを残しておくこと!
アップ後サイトに繋がらなくなったらすぐに戻してね!
右上に出てくるマークを押すとコピペしやすいです
function p_table_func() {
echo <<< EOM
<link rel="stylesheet" href="https://unpkg.com/scroll-hint@1.1.10/css/scroll-hint.css">
<script src="https://unpkg.com/scroll-hint@1.1.10/js/scroll-hint.js"></script>
<script>
window.addEventListener('DOMContentLoaded', function(){
new ScrollHint('.js-scrollable', {
remainingTime: 3000,
i18n: {
scrollable: 'スクロールできます'
}
});
});
</script>
EOM;
}
add_action( 'wp_footer', 'p_table_func' );
- function p_table_func() {
- echo <<< EOM
- <link rel="stylesheet" href="https://unpkg.com/scroll-hint@1.1.10/css/scroll-hint.css">
- <script src="https://unpkg.com/scroll-hint@1.1.10/js/scroll-hint.js"></script>
- <script>
- window.addEventListener('DOMContentLoaded', function(){
- new ScrollHint('.js-scrollable', {
- remainingTime: 3000,
- i18n: {
- scrollable: 'スクロールできます'
- }
- });
- });
- </script>
- EOM;
- }
- add_action( 'wp_footer', 'p_table_func' );
function p_table_func() {
echo <<< EOM
<link rel="stylesheet" href="https://unpkg.com/scroll-hint@1.1.10/css/scroll-hint.css">
<script src="https://unpkg.com/scroll-hint@1.1.10/js/scroll-hint.js"></script>
<script>
window.addEventListener('DOMContentLoaded', function(){
new ScrollHint('.js-scrollable', {
remainingTime: 3000,
i18n: {
scrollable: 'スクロールできます'
}
});
});
</script>
EOM;
}
add_action( 'wp_footer', 'p_table_func' );
「remainingTime: 3000」は自動で非表示になる時間です。スクロールするまで常時表示させておきたい人は消してください。デモはわかりやすいように消してあります。
「スクロールできます」は表示される文章です。好きに変更して大丈夫です!
ちなみに指の背景色を白に変えるには「remainingTime: 3000,」の下に
scrollHintIconAppendClass: 'scroll-hint-icon-white',
- scrollHintIconAppendClass: 'scroll-hint-icon-white',
scrollHintIconAppendClass: 'scroll-hint-icon-white',
を入れればOK!
STEP3:HTMLをコピペ、入力
ここまでで準備は整ったのであとは投稿画面でHTMLを入力するのみです。
AddQuicktagなどに登録しておくといつでも使えます。
下記コードを投稿画面にコピペし、コンテンツを入力します。
<div class="p-sticky-table">
<table class="js-scrollable">
<thead>
<tr>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
</tr>
</thead>
<tbody>
<tr>
<th>1行目:見出し</th>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
</tr>
<tr>
<th>2行目:見出し</th>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
</tr>
<tr>
<th>3行目:見出し</th>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
</tr>
</tbody>
</table>
</div>
- <div class="p-sticky-table">
- <table class="js-scrollable">
- <thead>
- <tr>
- <th>上部見出し</th>
- <th>上部見出し</th>
- <th>上部見出し</th>
- <th>上部見出し</th>
- <th>上部見出し</th>
- <th>上部見出し</th>
- <th>上部見出し</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <th>1行目:見出し</th>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- </tr>
- <tr>
- <th>2行目:見出し</th>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- </tr>
- <tr>
- <th>3行目:見出し</th>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- </tr>
- </tbody>
- </table>
- </div>
<div class="p-sticky-table">
<table class="js-scrollable">
<thead>
<tr>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
<th>上部見出し</th>
</tr>
</thead>
<tbody>
<tr>
<th>1行目:見出し</th>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
</tr>
<tr>
<th>2行目:見出し</th>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
</tr>
<tr>
<th>3行目:見出し</th>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
</tr>
</tbody>
</table>
</div>
行を増やす:<tr> ~ </tr>をコピー
行を減らす:<tr> ~ </tr>を削除
列を増やす:それぞれ1行ずつ追加
列を減らす:それぞれ1行ずつ削除
上部見出しが必要ない場合:
<div class="p-sticky-table">
<table class="js-scrollable">
<tbody>
<tr>
<th>1行目:見出し</th>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
</tr>
<tr>
<th>2行目:見出し</th>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
</tr>
<tr>
<th>3行目:見出し</th>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
</tr>
</tbody>
</table>
</div>
- <div class="p-sticky-table">
- <table class="js-scrollable">
- <tbody>
- <tr>
- <th>1行目:見出し</th>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- <td>1行目:ここにテキストor画像</td>
- </tr>
- <tr>
- <th>2行目:見出し</th>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- <td>2行目:ここにテキストor画像</td>
- </tr>
- <tr>
- <th>3行目:見出し</th>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- <td>3行目:ここにテキストor画像</td>
- </tr>
- </tbody>
- </table>
- </div>
<div class="p-sticky-table">
<table class="js-scrollable">
<tbody>
<tr>
<th>1行目:見出し</th>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
<td>1行目:ここにテキストor画像</td>
</tr>
<tr>
<th>2行目:見出し</th>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
<td>2行目:ここにテキストor画像</td>
</tr>
<tr>
<th>3行目:見出し</th>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
<td>3行目:ここにテキストor画像</td>
</tr>
</tbody>
</table>
</div>
HTML入力のポイント
コンテンツの文章が長くなる場合は改行を入れて調整してください。改行を入れないと横長になります。
導入済みのブログ、サイトを紹介させてください
「この記事を見て導入したよ!」という方でSNSやブログでこの記事を紹介していただいた場合(ココ大事!)、こちらでブログやサイトを紹介させていただきます!
その際テーブルを使っている記事を教えてください。長いタイトルは少し省略させていただきます!
増えすぎたら締め切ります。ご連絡お待ちしております!
他にもコピペカスタマイズやってます
他にもコピペでできるカスタマイズをやっているので良かったらご覧ください。
あわせて読むコピペでできるブログカスタマイズまとめ

コピペでできるブログカスタマイズまとめ【HTML&CSS】
ブロガー、アフィリエイターに役立つブログやサイトのカスタマイズをまとめていきます。HTML、CSSがわからなくてもコピペ可!WordPress(Cocoon、JIN、SANGO、Affingerなど)、はてなブログなど一部機能を除けば誰でも利用可能です。
コメント