Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="l-wrapper">
  <div class="l-contents">
    <h1>Table formatting: horizontal scrolling on mobile <br>Vanilla JS</h1>
    <table>
      <thead>
        <th>モバイルで<br>横スクロールにする</th>
        <th>テキスト</th>
        <th>テキストテキスト</th>
        <th>テキスト</th>
        <th>テキストテキスト</th>
        <th>テキスト</th>
        <th>テキスト</th>
        <th>テキスト</th>
        <th>テキスト</th>
      </thead>
      <tbody>
        <tr>
          <th>テキスト</th>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
        </tr>
        <tr>
          <th>テキスト</th>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
        </tr>
        <tr>
          <th>テキスト</th>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
          <td>テキスト</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
              
            
!

CSS

              
                /* テストページ用style */
body {
  margin: 0;
}
h1 {
  text-align: center;
  margin: 10vh 0;
}
.l-wrapper {
  overflow: hidden;
}
.l-contents {
  max-width: 1080px;
  padding-left: 40px;
  padding-right: 40px;
  margin-left: auto;
  margin-right: auto;
}
table {
  border-collapse: collapse;
}
th,
td {
  border: 1px solid #ccc;
  padding: 15px;
}
th {
  background-color: #f0f0f0;
}
              
            
!

JS

              
                //表組み スクロール切り替え
const tables = document.querySelectorAll('table');
if(tables.length > 0) {
	//スクロール処理を発火させるブレイクポイント
	const breakPoint = 767;
	//tableごとの処理
	tables.forEach((table) => {
		//tableに除外class "is-no-scroll" が付与されている場合は処理しない
		if(!table.classList.contains('is-no-scroll')) {
			//スクロール用div要素作成
			const inner = document.createElement('div');
			inner.classList.add('table-inner');
			const wrap = document.createElement('div');
			wrap.classList.add('table-wrap');

			//スクロール用div要素挿入
			table.parentNode.insertBefore(inner, table);
			inner.appendChild(table);
			inner.parentNode.insertBefore(wrap, inner);
			wrap.appendChild(inner);

			//スクロール発火フラグ
			let scrollFlg = false;
			//table widthの設定値を取得
			table.baseWidth = table.style.width;

			//スクロール関数
			const tableScroll = () => {
				//スクロールが発火していたら設定をリセット
				if(scrollFlg) {
					inner.removeAttribute('style');
					wrap.removeAttribute('style');
					table.style.width = table.baseWidth;
					scrollFlg = false;
				}
				//ブレイクポイント以下の場合のみ処理
				const winWidth = window.innerWidth;
				if(winWidth > breakPoint) {
					return;
				}

				//tableのベース幅を取得
				let tableWidth = table.offsetWidth;
				const wrapWidth = wrap.offsetWidth;

				//tableの幅がwrapの幅以上の場合は本来の幅をチェック
				if(wrapWidth <= tableWidth) {
					//tableに100%以外のwidth指定が入っている場合
					if(table.baseWidth !== '' && table.baseWidth !== '100%') {
						tableWidth = parseFloat(table.baseWidth) + 1;

					} else {
						table.baseWidth = table.style.width;
						table.style.width = 'auto';
						inner.style.width = '9999px';
						tableWidth = table.offsetWidth;
						inner.style.width = '';
						table.style.width = table.baseWidth;
						console.log(tableWidth,table.baseWidth);
					}
				}

				//tableがwrapの幅を超えている場合スクロール発火
				if(wrapWidth < tableWidth) {
					//wrapリセット
					wrap.removeAttribute('style');
					wrap.style.overflowX = 'hidden';

					//wrapの左右の余白を取得
					const wrapLeftMargin = wrap.getBoundingClientRect().left;
					const wrapRightMargin = winWidth - parseFloat(wrap.getBoundingClientRect().right);

					//wrapを画面幅いっぱいに広げる
					wrap.style.overflowX = 'scroll';
					wrap.style.marginLeft = '-' + wrapLeftMargin + 'px';
					wrap.style.marginRight = '-' + wrapRightMargin + 'px';

					//innerにtable本来の幅 + 左右余白を設定
					inner.style.boxSizing = 'content-box';
					inner.style.paddingLeft = wrapLeftMargin + 'px';
					inner.style.paddingRight = wrapRightMargin + 'px';
					inner.style.width = (tableWidth + 1) + 'px'; //少数点分の繰り上げ

					//スクロール発火フラグセット
					scrollFlg = true;
				}
			}

			//スクロール関数呼び出し
			tableScroll();

			//リサイズイベント
			let lastInnerWidth = window.innerWidth;
			window.addEventListener('resize', function() {
				//画面の横サイズが変わった時のみ発火
				if (lastInnerWidth !== window.innerWidth) {
					//スクロール関数呼び出し
					tableScroll();
					//横幅を保存
					lastInnerWidth = window.innerWidth;
				}
			});
		}
	});
}
              
            
!
999px

Console