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 id="wrap">
  
  <h1>Responsive <span>Table</span> Layout Sample</h1>
  <p class="description">レスポンシブデザインのテーブルレイアウト例</p>
  
  <ul class="btnList">
    <li><button class="addThead">Thead追加</button></li>
    <li><button class="removeThead">Thead削除</button></li>
    <li><button class="increaseText">テキスト増やす</button></li>
    <li><button class="cutBackText">テキスト減らす</button></li>
  </ul>
  
  <div class="tableBlock pattern01">
  <table>
    <caption>レスポンシブテーブル パターン1:単純に幅可変</caption>
    <tbody>
    <tr>
      <th>見出し1</th>
      <td>テキスト1-1</td>
      <td>テキスト1-2</td>
    </tr>
    <tr>
      <th>見出し2</th>
      <td>テキスト2-1</td>
      <td>テキスト2-2</td>
    </tr>
    <tr>
      <th>見出し3</th>
      <td>テキスト3-1</td>
      <td>テキスト3-2</td>
    </tr>
    </tbody>
  </table>
  <!-- /.tableBlock pattern01 --></div>
  
  <div class="tableBlock pattern02">
  <table>
    <caption>レスポンシブテーブル パターン2:フォントサイズ縮小</caption>
    <tbody>
    <tr>
      <th>見出し1</th>
      <td>テキスト1-1</td>
      <td>テキスト1-2</td>
    </tr>
    <tr>
      <th>見出し2</th>
      <td>テキスト2-1</td>
      <td>テキスト2-2</td>
    </tr>
    <tr>
      <th>見出し3</th>
      <td>テキスト3-1</td>
      <td>テキスト3-2</td>
    </tr>
    </tbody>
  </table>
  <!-- /.tableBlock pattern02 --></div>
  
  <div class="tableBlock pattern03">
  <table>
    <caption>レスポンシブテーブル パターン3:縦並び</caption>
    <tbody>
    <tr>
      <th>見出し1</th>
      <td>テキスト1-1</td>
      <td>テキスト1-2</td>
    </tr>
    <tr>
      <th>見出し2</th>
      <td>テキスト2-1</td>
      <td>テキスト2-2</td>
    </tr>
    <tr>
      <th>見出し3</th>
      <td>テキスト3-1</td>
      <td>テキスト3-2</td>
    </tr>
    </tbody>
  </table>
  <!-- /.tableBlock pattern03 --></div>

  <div class="tableBlock pattern04">
  <table>
    <caption>レスポンシブテーブル パターン4:横スクロール</caption>
    <tbody>
    <tr>
      <th>見出し1</th>
      <td>テキスト1-1</td>
      <td>テキスト1-2</td>
    </tr>
    <tr>
      <th>見出し2</th>
      <td>テキスト2-1</td>
      <td>テキスト2-2</td>
    </tr>
    <tr>
      <th>見出し3</th>
      <td>テキスト3-1</td>
      <td>テキスト3-2</td>
    </tr>
    </tbody>
  </table>
  <!-- /.tableBlock pattern04 --></div> 
  
  <div class="tableBlock pattern05">
  <table>
    <caption>レスポンシブテーブル パターン5:リスト化</caption>
    <tbody>
    <tr>
      <th>見出し1</th>
      <td>テキスト1-1</td>
      <td>テキスト1-2</td>
    </tr>
    <tr>
      <th>見出し2</th>
      <td>テキスト2-1</td>
      <td>テキスト2-2</td>
    </tr>
    <tr>
      <th>見出し3</th>
      <td>テキスト3-1</td>
      <td>テキスト3-2</td>
    </tr>
    </tbody>
  </table>
  <!-- /.tableBlock pattern05 --></div> 

  <div class="tableBlock pattern06">
  <table>
    <caption>レスポンシブテーブル パターン6:thead縦横切替</caption>
    <tbody>
    <tr>
      <th>見出し1</th>
      <td>テキスト1-1</td>
      <td>テキスト1-2</td>
    </tr>
    <tr>
      <th>見出し2</th>
      <td>テキスト2-1</td>
      <td>テキスト2-2</td>
    </tr>
    <tr>
      <th>見出し3</th>
      <td>テキスト3-1</td>
      <td>テキスト3-2</td>
    </tr>
    </tbody>
  </table>
  <!-- /.tableBlock pattern06 --></div> 
  
<!-- /#wrap --></div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Righteous);

#wrap {
  margin:2em auto;
  font-family: Arial, Roboto, “Droid Sans”, “游ゴシック”, YuGothic, “ヒラギノ角ゴ ProN W3”, “Hiragino Kaku Gothic ProN”, “メイリオ”, Meiryo, sans-serif;
  max-width:960px;
}
h1, h2 {
  text-align:center;
  font-family: 'Righteous', cursive;
}
h1 {
  font-size: 2.5em;
}
h1 span {
  color:#ffc045;
}
p.description {
  margin-top: 1em;
  letter-spacing:2px;
  text-align:center;
}
.btnList {
  margin-top: 2em;
  text-align:center;
}
.btnList li {
  display:inline-block;
}

/* 基本スタイル */
.tableBlock {
  margin-top:2em;
}
table {
  width:100%;
  table-layout:fixed;
  -webkit-box-sizing:border-box;
  box-sizing:border-box;
  border:1px solid #cfcfcf;
}
table caption {
  padding:1em;
  line-height: 1.5;
  text-align: left;
  background:#ffc045;
}
table tr th {
  background:#f2f2f2;
}
table tr th,
table tr td {
  padding:1em;
  line-height: 1.5;
  text-align:left;
  border:1px solid #cfcfcf;
}

/* パターン4:テーブルを横スクロールに */
.tableBlock.pattern04 {
  overflow-x:auto;
}
.tableBlock.pattern04 table {
  width:960px;
}
/* スクロールバー全体 */
.tableBlock.pattern04::-webkit-scrollbar{
    height: 0.5em;
    background-color: #F1F1F1;
}
/* スクロールバーのつまみ(可動部分) */
.tableBlock.pattern04::-webkit-scrollbar-thumb{
    background-color: #BCBCBC;
    border-radius: 12px;
    border-radius: 1.2rem;
}
.tableBlock.pattern04::-webkit-scrollbar-thumb:hover{
    background-color: #A9A9AA;
}

/* Tablet */
@media screen and (max-width:768px){
  
  /* パターン2:フォントサイズ縮小 */
  .tableBlock.pattern02 table {
    font-size:75%;
  }
  
  /* パターン3:テーブルセルを縦並びに */
  .tableBlock.pattern03 table,
  .tableBlock.pattern03 table caption,
  .tableBlock.pattern03 table thead,
  .tableBlock.pattern03 table tbody,
  .tableBlock.pattern03 table tr,
  .tableBlock.pattern03 table tr th,
  .tableBlock.pattern03 table tr td {
    display:block;
  }
  .tableBlock.pattern03 table tr th,
  .tableBlock.pattern03 table tr td {
    margin:-1px; /* borderが二重になるのを防ぐ */
  }
  
  /* パターン5:リスト化 */
  .tableBlock.pattern05 table,
  .tableBlock.pattern05 table caption,
  .tableBlock.pattern05 table thead,
  .tableBlock.pattern05 table tbody,  
  .tableBlock.pattern05 table tr,
  .tableBlock.pattern05 table tr th {
    display:block;
  }
  .tableBlock.pattern05 table tr th {
    margin:-1px; /* borderが二重になるのを防ぐ */
  }
  .tableBlock.pattern05 table tr td {
    display:list-item;
    list-style:disc inside;
    border:none;
  }
  .tableBlock.pattern05 table tr td + td {
    padding-top:0;
  }
  
  /* パターン6:縦横切替 */
  .tableBlock.pattern06 table,
  .tableBlock.pattern06 table caption,
  .tableBlock.pattern06 table thead,
  .tableBlock.pattern06 table tbody,
  .tableBlock.pattern06 table tr,
  .tableBlock.pattern06 table tr th,
  .tableBlock.pattern06 table tr td {
    display:block;
  }
  .tableBlock.pattern06 table tr th,
  .tableBlock.pattern06 table tr td {
    margin:-1px; /* borderが二重になるのを防ぐ */
  }
  .tableBlock.pattern06 table thead {
	  display:none;
  }
  .tableBlock.pattern06 table tbody tr td[data-th]:before {
    content: attr(data-th) " : ";
    font-weight: bold;
  	display: inline-block;
    margin-right:0.25em;
  }
  
/* Tablet */ }

/* SmartPhone */
@media screen and (max-width:480px){

  /* パターン2:フォントサイズ縮小 */
  .tableBlock.pattern02 table {
    font-size:50%;
  }

/* Smartphone */ }


              
            
!

JS

              
                $(function(){

  // thead追加
  $('.btnList .addThead').click(function(){
    tableReset();
    $('table caption').after('<thead><tr><th></th><th>見出し●●</th><th>見出し××</th></tr></thead>');
    tableData() // パターン6:縦横切替
  });

  // thead削除
  $('.btnList .removeThead').click(function(){
    tableReset();
  });
  
  // テーブルリセット
  function tableReset(){
    $('table thead').remove();
    $('table td').each(function(){
      $(this).removeAttr('data-th');
    });
  }
  
  // デフォルトのテキストを配列に格納
  var defTextArr = new Array();
  $('table td').each(function(i){
      defTextArr[i] = $(this).text();
  });
  
  // テキスト増やす
  var count = 1;
  $('.btnList .increaseText').click(function(){
    count++;
    $('table td').each(function(e){
      $(this).text( $(this).text() + $(this).text()  );
    });
  });

  // テキスト減らす
  $('.btnList .cutBackText').click(function(){
    for(t=0; t<defTextArr.length; t++) {
      var txt = defTextArr[t];
      if(count >= 1) {
        $('table td').each(function(){
           $(this).text( $(this).text().replace(txt, '' ) );
        });
      }
    }
  });
  
	// パターン6:縦横切替のテーブルにdata属性を追加
	function tableData(){
		var index ='';
		var headTxt ='';
		$('.tableBlock.pattern06 table').each(function() {
			$(this).find('thead tr th').each(function() {
	
				// theadの位置やテキストを取得
				index = $(this).index()-1;
				headTxt = $(this).text();
						
				// tbodyのセルにdata属性を追加
				$(this).parents('table').find('tbody tr').each(function() {
					$(this).find('td').eq(index).attr('data-th',headTxt);
				});
			});
		});
	}
  tableData();

});
              
            
!
999px

Console