HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<h1>Scroll Middle Section Sideways</h1>
<div class="table-scroll-wrap">
<div id="table-scroll" class="table-scroll">
<table id="main-table" class="main-table">
<thead>
<tr>
<th class="col col1" scope="col">Header 1</th>
<th class="col col2" scope="col">Header 2</th>
<th class="col col3" scope="col">Header 3 </th>
<th scope="col">Header 4 </th>
<th scope="col">Header 5</th>
<th scope="col">Header 6</th>
<th scope="col">Header 7</th>
<th scope="col">Header 8</th>
<th scope="col">Header 9</th>
<th scope="col">Header 10</th>
<th scope="col">Header 11</th>
<th scope="col">Header 12</th>
<th scope="col">Header 13</th>
<th scope="col">Header 14</th>
<th scope="col">Header 15</th>
<th class="col collast" scope="col">Header 16</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col col1">1 First top Column</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
<tr>
<td class="col col1">1 Last FirstColumn</td>
<td class="col col2">2 Cell content </td>
<td class="col col3">3 <a href="#">Cell content longer</a></td>
<td>4 Cell content with more content and more content </td>
<td>5 Cell content</td>
<td>6 Cell content</td>
<td>7 Cell content</td>
<td>8 Cell content</td>
<td>9 Cell content with more content and more </td>
<td>10 Cell content</td>
<td>11 Cell content</td>
<td>12 Cell content with more content </td>
<td>13 Cell content</td>
<td>14 Cell content</td>
<td>15 Cell content</td>
<td class="col collast">16 Cell content</td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="col col1">Footer 1</td>
<td class="col col2">Footer 2</td>
<td class="col col3">Footer 3</td>
<td>Footer 4</td>
<td>Footer 5</td>
<td>Footer 6</td>
<td>Footer 7</td>
<td>Footer 8</td>
<td>Footer 9</td>
<td>Footer 10</td>
<td>Footer 11</td>
<td>Footer 12</td>
<td>Footer 13</td>
<td>Footer 14</td>
<td>Footer 15</td>
<td class="col collast">Footer 16</td>
</tr>
</tfoot>
</table>
</div>
</div>
:root {
--cell-width: 200px; /* js will change this to match 10% of table-width. */
}
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
h1 {
margin: auto 0 0;
text-align: center;
font-size: 1.5rem;
}
.table-scroll-wrap {
position: relative;
margin: 0 auto auto;
width: 85%;
max-width: 2000px;
height: 70vh; /* for vertical scrolling if required*/
}
.table-scroll {
position: relative;
width: 100%;
z-index: 1;
margin: 0;
overflow: auto;
height: 100%; /* for vertical scrolling if required*/
}
.table-scroll table {
width: 100%;
margin: 0;
table-layout: fixed;
border-collapse: separate;
border-spacing: 0;
word-wrap: break-word;
}
.table-wrap {
position: relative;
}
.table-scroll th,
.table-scroll td {
padding: 5px 10px;
border: 1px solid #000;
background: #fff;
vertical-align: top;
width: 200px;
width: var(--cell-width);
}
.table-scroll thead th {
background: #333;
color: #fff;
position: -webkit-sticky;
position: sticky;
top: 0;
}
/* safari and ios need the tfoot itself to be position:sticky also */
.table-scroll tfoot,
.table-scroll tfoot th,
.table-scroll tfoot td {
position: -webkit-sticky; /* this is needed for ios*/
position: sticky;
bottom: 0;
background: #666;
color: #fff;
z-index: 4;
}
a:focus {
background: red;
} /* testing links*/
.col {
position: -webkit-sticky;
position: sticky;
left: 0;
z-index: 2;
}
tbody .col {
background: #ccc;
}
.col2 {
left: var(--cell-width);
}
.col3 {
left: calc(var(--cell-width) * 2);
}
.collast {
left: auto;
right: 0;
}
thead .col,
.table-scroll tfoot .col {
z-index: 6;
}
@media screen and (max-width: 1050px) {
.table-scroll-wrap {
width: 100%;
}
}
/* for small screen I would remove sticky side columns and just let table scroll sideways.*/
/* something roughly like this */
@media screen and (max-width: 900px) {
.table-scroll table {
table-layout: auto;
}
.table-scroll tbody .col {
position: static;
}
.table-scroll thead .col,
.table-scroll tfoot .col {
left: auto;
right: auto;
}
.table-scroll tbody td.col {
background: #fff;
}
.table-scroll th,
.table-scroll td {
min-width: 150px;
}
.table-scroll-wrap:after,
.table-scroll-wrap:before{
content: "";
position: absolute;
right: 0;
top: 0;
bottom: 0;
background: linear-gradient(to left, #666, transparent);
z-index: 999;
width: 45px;
pointer-events: none;
}
.table-scroll-wrap:before{
right:auto;
left:0;
background: linear-gradient(to right, #666, transparent)
}
}
(function (d) {
const theTableWrap = d.querySelector("#table-scroll");
//the CSS custom variable for the cell width
const cssWidthProperty = "--cell-width";
var tableWidth = theTableWrap.offsetWidth;
function setWidth(tableWidth) {
var cssWidthValue = Math.round(tableWidth / 10) + "px"; // assumes a 10% width
document.documentElement.style.setProperty(cssWidthProperty, cssWidthValue);
}
window.addEventListener("resize", throttle(getColumnSizes, 100));
function getColumnSizes(e) {
tableWidth = theTableWrap.offsetWidth;
setWidth(tableWidth);
}
setWidth(tableWidth);
function throttle(callback, limit) {
//http://sampsonblog.com/749/simple-throttle-function
var wait = false; // Initially, we're not waiting
return function () {
// We return a throttled function
if (!wait) {
// If we're not waiting
callback.call(); // Execute users function
wait = true; // Prevent future invocations
setTimeout(function () {
// After a period of time
wait = false; // And allow future invocations
}, limit);
}
};
}
})(document);
Also see: Tab Triggers