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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <main>
  <h1>Responsive Table with Semantics Retained by ARIA</h1>
  <p>
    I suggest only pressing the following button after you have made the viewport narrow. This way you can experience the table without the CSS generated content that makes up for the hidden column headers.
  </p>
  <p>
  <button onclick="ResponsiveCellHeaders('Books');this.setAttribute('disabled','true');">Add fake cell headers at narrow width</button>
  </p>
  <div role="region" aria-labelledby="Cap1" tabindex="0">
    <table id="Books" role="table">
      <caption id="Cap1">Books I May or May Not Have Read</caption>
      <tr role="row">
        <th role="columnheader">Author</th>
        <th role="columnheader">Title</th>
        <th role="columnheader">Year</th>
        <th role="columnheader">ISBN-13</th>
        <th role="columnheader">ISBN-10</th>
      </tr>
      <tr role="row">
        <td role="cell">Miguel De Cervantes</td>
        <td role="cell">The Ingenious Gentleman Don Quixote of La Mancha</td>
        <td role="cell">1605</td>
        <td role="cell">9783125798502</td>
        <td role="cell">3125798507</td>
      </tr>
      <tr role="row">
        <td role="cell">Mary Shelley</td>
        <td role="cell">Frankenstein; or, The Modern Prometheus</td>
        <td role="cell">1818</td>
        <td role="cell">9781530278442</td>
        <td role="cell">1530278449</td>
      </tr>
      <tr role="row">
        <td role="cell">Herman Melville</td>
        <td role="cell">Moby-Dick; or, The Whale</td>
        <td role="cell">1851</td>
        <td role="cell">9781530697908</td>
        <td role="cell">1530697905</td>
      </tr>
      <tr role="row">
        <td role="cell">Emma Dorothy Eliza Nevitte Southworth</td>
        <td role="cell">The Hidden Hand</td>
        <td role="cell">1888</td>
        <td role="cell">9780813512969</td>
        <td role="cell">0813512964</td>
      </tr>
      <tr role="row">
        <td role="cell">F. Scott Fitzgerald</td>
        <td role="cell">The Great Gatsby</td>
        <td role="cell">1925</td>
        <td role="cell">9780743273565</td>
        <td role="cell">0743273567</td>
      </tr>
      <tr role="row">
        <td role="cell">George Orwell</td>
        <td role="cell">Nineteen Eighty-Four</td>
        <td role="cell">1948</td>
        <td role="cell">9780451524935</td>
        <td role="cell">0451524934</td>
      </tr>
    </table>
  </div>
  
  <p>
    Note that this is an <em>accessible</em> (keyboard and screen reader) responsive (width and print) table. It retains table semantics for screen readers with thanks to the liberal application of ARIA roles. You can <a href="http://adrianroselli.com/2018/02/tables-css-display-properties-and-aria.html">read more at my site</a>.
  </p>
</main>
            
          
!

CSS

            
              @import url("https://fonts.googleapis.com/css?family=Lato:400,400i,700");

body {
  background-color: #6d695c;
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAACVBMVEUAAAAAAAAAAACDY+nAAAAAA3RSTlMmDQBzGIDBAAAAG0lEQVR42uXIIQEAAADCMHj/0NdkQMws0HEeAqvwAUGJthrXAAAAAElFTkSuQmCC);
  font-size: 100%;
  color: #333;
  font-family: Lato, Arial, sans-serif;
  padding: 0;
  margin: 0;
}

main {
  display: block;
  box-sizing: border-box;
  width: auto;
  padding: 1em 2vw;
  margin: 1em 2vw;
  color: #000;
  background-color: rgba(204, 204, 204, 0.7);
  border: 0.07em solid rgba(0, 0, 0, 0.5);
  border-radius: 0.5em;
}

table {
  margin: 1em 0;
  border-collapse: collapse;
/*   width: 100%; */
}

caption {
  text-align: left;
  font-style: italic;
  padding: 0.25em 0.5em 0.5em 0.5em;
}

th,
td {
  padding: 0.25em 0.5em 0.25em 1em;
  vertical-align: text-top;
  text-align: left;
  text-indent: -0.5em;
}

th {
  vertical-align: bottom;
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-weight: bold;
}

td::before {
  display: none;
}

tr:nth-child(even) {
  background-color: rgba(255, 255, 255, 0.25);
}

tr:nth-child(odd) {
  background-color: rgba(255, 255, 255, 0.5);
}

td:nth-of-type(2) {
  font-style: italic;
}

th:nth-of-type(3),
td:nth-of-type(3) {
  text-align: right;
}

div {
  overflow: auto;
}

@media screen and (max-width: 37em), print and (max-width: 5in) {
  table,
  tr,
  td {
    display: block;
  }
  tr {
    padding: 0.7em 2vw;
  }
  th,
  tr:first-of-type {
    display: none;
  }
  td::before {
    display: inline;
    font-weight: bold;
  }
  td {
    display: grid;
    grid-template-columns: 4em auto;
    grid-gap: 1em 0.5em;
  }
  caption {
    font-style: normal;
    background-color: rgba(0, 0, 0, 0.35);
    color: #fff;
    font-weight: bold;
  }
  td:nth-of-type(3) {
    text-align: left;
  }
  td:nth-of-type(4), td:nth-of-type(5) {
    text-align: right;
    width: 12em;
  }
  td:nth-of-type(4)::before, td:nth-of-type(5)::before {
    text-align: left;
  }
  td:nth-of-type(2)::before {
    font-style: normal;
  }
}

@media print {
  body {
    font-size: 6pt;
    color: #000;
    background-color: #fff;
    background-image: none;
  }
  body,
  main {
    margin: 0;
    padding: 0;
    background-color: #fff;
    border: none;
  }
  table {
    page-break-inside: avoid;
  }
  div {
    overflow: visible;
  }
  th {
    color: #000;
    background-color: #fff;
    border-bottom: 1pt solid #000;
  }
  tr {
    border-top: 1pt solid #000;
  }
}

@media print and (max-width: 5in) {
  caption {
    color: #000;
    background-color: #fff;
    border-bottom: 1pt solid #000;
  }
  table {
    page-break-inside: auto;
  }
  tr {
    page-break-inside: avoid;
  }
}

button {
  font-size: 90%;
  font-family: Lato, Arial, sans-serif;
  color: #fff;
  background-color: #00c;
  border: .1em solid #00f;
  border-radius: .5em;
  font-weight: bold;
  padding: .5em;
  text-align: center;
  box-shadow: .25em .25em .5em rgba(0, 0, 0, .5);
}

button[disabled] {
  background-color: #ccc;
  border-color: #ccc;
  color: #666;
}

button:not([disabled]):focus,
button:not([disabled]):hover {
  color: #00f;
  background-color: #fff;
  outline: 0;
}

button:active {
  box-shadow: 0 0 .5em rgba(0, 0, 0, .5);
}

            
          
!

JS

            
              function ResponsiveCellHeaders(elmID) {
  try {
    var THarray = [];
    var table = document.getElementById(elmID);
    var ths = table.getElementsByTagName("th");
    for (var i = 0; i < ths.length; i++) {
      var headingText = ths[i].innerHTML;
      THarray.push(headingText);
    }
    var styleElm = document.createElement("style"),
      styleSheet;
    document.head.appendChild(styleElm);
    styleSheet = styleElm.sheet;
    for (var i = 0; i < THarray.length; i++) {
      styleSheet.insertRule(
        "#" +
          elmID +
          " td:nth-child(" +
          (i + 1) +
          ')::before {content:"' +
          THarray[i] +
          ': ";}',
        styleSheet.cssRules.length
      );
    }
  } catch (e) {
    console.log("ResponsiveCellHeaders(): " + e);
  }
}
// ResponsiveCellHeaders("Books");

            
          
!
999px

Console