Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                <h2 style="text-align:center;">table HTML avec entête fixe - responsive - JS (+choix du nombre de lignes affichées)</h2>
<div class="conteneur">

  <div class="table-wrap table-thead-fixed" data-nbreligne="10">
    <table>
      <thead>
        <tr>
          <th>Date</th>
          <th>Time</th>
          <th title="Timezone">TZ</th>
          <th>Position</th>
          <th title="Time To Waypoint">TTW</th>
          <th title="Distance To Waypoint">DTW</th>
          <th title="Distance To Go">DTG</th>
          <th title="True Wind Direction">TWD</th>
          <th title="True Wind Speed">TWS</th>
          <th title="True Wind Angle">TWA</th>
          <th title="Bearing To Waypoint - Heading">BTW</th>
          <th>Sail</th>
          <th title="Speed Through Water - Boat speed">STW</th>
          <th title="Average True Wind Angle">ATWA</th>
          <th title="Average Bearing To Waypoint">ABTW</th>
        </tr>
      </thead>
      <tbody id="pointsTable" align="center">
        <tr>
          <td>2018-05-14</td>
          <td>16:30</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°35'37"W</td>
          <td>T+ 0:00</td>
          <td>0.0nm</td>
          <td>158.4nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>235°</td>
          <td>Spi</td>
          <td>8.84 kts</td>
          <td>-</td>
          <td>-</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
      </tbody>
    </table>
  </div>

</div>

<br/><br/>

<div class="conteneur">

  <div class="table-wrap table-thead-fixed" data-nbreligne="5">
    <table>
      <thead>
        <tr>
          <th>Date</th>
          <th>Time</th>
          <th title="Timezone">TZ</th>
          <th>Position</th>
          <th title="Time To Waypoint">TTW</th>
          <th title="Distance To Waypoint">DTW</th>
          <th title="Distance To Go">DTG</th>
          <th title="True Wind Direction">TWD</th>
          <th title="True Wind Speed">TWS</th>
          <th title="True Wind Angle">TWA</th>
          <th title="Bearing To Waypoint - Heading">BTW</th>
          <th>Sail</th>
          <th title="Speed Through Water - Boat speed">STW</th>
          <th title="Average True Wind Angle">ATWA</th>
          <th title="Average Bearing To Waypoint">ABTW</th>
        </tr>
      </thead>
      <tbody id="pointsTable" align="center">
        <tr>
          <td>2018-05-14</td>
          <td>16:30</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°35'37"W</td>
          <td>T+ 0:00</td>
          <td>0.0nm</td>
          <td>158.4nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>235°</td>
          <td>Spi</td>
          <td>8.84 kts</td>
          <td>-</td>
          <td>-</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
        <tr>
          <td>2018-05-14</td>
          <td>16:40</td>
          <td>CEST</td>
          <td>19°26'15"N - 60°37'30"W</td>
          <td>T+ 0:10</td>
          <td>1.5nm</td>
          <td>156.9nm</td>
          <td>83°</td>
          <td>16.0 kt</td>
          <td>152°</td>
          <td>234°</td>
          <td>Spi</td>
          <td>8.82 kts</td>
          <td>152°</td>
          <td>235°</td>
        </tr>
      </tbody>
    </table>
  </div>

</div>
              
            
!

CSS

              
                /* ---------------------- */
/* table avec entete fixe */
/* ---------------------- */
.table-thead-fixed {
  position: relative;
  overflow: hidden;
}
.theader-fixed {
  z-index: 1;
  position: absolute;
  overflow: hidden;
  background:#fff;
}
.tbody-scroll {
  position: relative;
  overflow: auto;
  margin-bottom: 5px;
}
/* ---------------------- */
/* DECO */
.conteneur {
  width: 100%;
  max-width: 900px; /* pour les TESTS */
  margin:0 auto;
}
table {
  text-align: center;
  width: 100%;
  border-collapse:collapse;
  font-size: 85%;
  font-family:Arial,sans-serif;
}
table th {
  /*position: sticky;*/
  top: 0;
  padding:5px 1px;
  background: #ccc;
  border: 1px solid #666;
}
table td {
  border: 1px solid #ccc;
  padding:8px 3px;
  white-space: nowrap;
}
/* ---------------------- */

              
            
!

JS

              
                "use strict";
// ----------
// table HTML avec entête fixe - responsive - JS (+choix du nombre de lignes affichées)
function table_thead_fixed() 
{
	// on peut avoir plusieurs tables
	let tableDivWraps = document.querySelectorAll(".table-thead-fixed");
	tableDivWraps.forEach(function(tableDivWrap) 
	{
		// ----------
		// table avec entete fixe
		let tableRef = tableDivWrap.children[0]; // table
		// ----------
		// on entoure la table d un div
		let tableBodyScroll = document.createElement("div");
		tableBodyScroll.className += "tbody-scroll"; // "tbody-scroll"
		tableDivWrap.insertBefore(tableBodyScroll, tableRef);
		tableBodyScroll.appendChild(tableRef);
		// ----------
		// entete fixe : copie (clone) de la table
		let tableHeadFixed = document.createElement("div"); // "theader-fixed"
		tableHeadFixed.className += "theader-fixed";
		tableHeadFixed.appendChild(tableRef.cloneNode(true));
		tableDivWrap.prepend(tableHeadFixed);
		// ----------
		// SPECIAL : cas particulier
    // on change l id "pointsTable" -> "pointsTable2" (id sur le <tbody>)
//		document.querySelector('.theader-fixed #pointsTable').id = 'pointsTable2';
		// ----------
	});
	// ----------
	// ajustement hauteur de scroll propre
	table_thead_fixed_resize();
}
// --------------------
// Redimensionnement de la fenêtre (horizontal)
function table_thead_fixed_resize() 
{
	// on peut avoir plusieurs tables
	let tableDivWraps = document.querySelectorAll(".table-thead-fixed");
	tableDivWraps.forEach(function(tableDivWrap) 
	{
		let nbLigne = tableDivWrap.dataset.nbreligne !== undefined ? tableDivWrap.dataset.nbreligne : 12; // défaut 12 (A ADAPTER)
		// ----------
		let tableHeadFixed = tableDivWrap.children[0];
		let tableBodyScroll = tableDivWrap.children[1];
		let tableRef = tableBodyScroll.children[0];
		// ----------
		// calcul hauteur scroll-barre-X
		let hScrollBar = tableBodyScroll.offsetHeight - tableBodyScroll.clientHeight;
		// ----------
		// nombre de lignes à montrer
		let nbLigneToShow = Math.min(nbLigne, tableRef.rows.length) + 1;
		let rect1 = tableRef.rows[0].getBoundingClientRect();
		let rect2 = tableRef.rows[nbLigneToShow].getBoundingClientRect();
		tableBodyScroll.style.height = rect2.top - rect1.top + hScrollBar + "px";
		// ----------
		// calcul pour réajustement
		let hEntete = tableRef.tHead.offsetHeight;
		// ----------
		// calcul largeur scroll-barre-Y
		let wScrollBar = tableBodyScroll.offsetWidth - tableBodyScroll.clientWidth;
		// ----------
		// réajustement du header fixe
		tableHeadFixed.style.height = hEntete + "px";
		tableHeadFixed.style.width = "calc(100% - " + wScrollBar + "px)";
		// ----------
		// synchronisation des scrolls
		tableBodyScroll.onscroll = function() {
		tableHeadFixed.style.marginLeft = -this.scrollLeft + "px";
		};
		// ----------
	});
}
 
// Activation :
window.onload = function(){
  table_thead_fixed();
}
// Redimensionnement de la fenêtre
window.onresize = function(){
  table_thead_fixed_resize();
}
              
            
!
999px

Console