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

              
                
  <figure>
    <figcaption id="caption">
      <h1>Countries with most population</h1>
    </figcaption>
    <div class="table-wrapper" role="group" aria-labelledby="caption">
      <table>
        <tbody>
          <tr>
            <th>Rank</th>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
            <td>6</td>
            <td>7</td>
          </tr>
          <tr>
            <th>Country</th>
            <td>China</td>
            <td>United States</td>
            <td>Indonesia</td>
            <td>Pakistan</td>
            <td>Brazil</td>
            <td>Nigeria</td>
            <td>Bangladesh</td>
          </tr>
          <tr>
            <th>Population</th>
            <td>1,439,323,776</td>
            <td>331,002,651</td>
            <td>273,523,615</td>
            <td>220,892,340</td>
            <td>212,559,417</td>
            <td>206,139,589</td>
            <td>164,689,383</td>
          </tr>

          <tr>
            <th>World-share %</th>
            <td>18.47</td>
            <td>4.25</td>
            <td>3.51</td>
            <td>2.83</td>
            <td>2.73</td>
            <td>2.64</td>
            <td>2.11</td>
          </tr>
        </tbody>
      </table>
    </div>
  </figure>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

:root {
  --color-primary: #2e4a40;
  --color-secondary: #e6efa3;
  --color-tertiary: rgba(0, 0, 0, 0.2);

  --font-body: "Switzer", sans-serif;
  --font-headings: "Gambarino", sans-serif;
}

body {
  color: var(--color-primary);
  font-family: var(--font-body);
  font-size: 22px;
  line-height: 1.5;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  padding: 2rem;
  background-color: var(--color-secondary);
}

figure {
  width: 100%;
  max-width: 800px;
}

table {
  text-align: left;
  border-collapse: collapse;
  margin-bottom: 1rem;
  table-layout: fixed;
  white-space: nowrap;
  border-radius: 3rem;
  width: min(auto, 100%);
}

table tbody {
    border:  2px solid var(--color-tertiary);
}

tbody td {
  vertical-align: top;
}

th {
  font-family: var(--font-headings);
  font-weight: bold;
}

tr > * {
  padding: 0.75rem 1rem;
}

tr > *:nth-child(even) {
  background-color: var(--color-tertiary);
}

tr:first-child > td {
  font-family: var(--font-headings);
  font-weight: bold;
}

thead {
  border-bottom: 1px solid var(--color-primary);
  font-size: var(--step-1);
}

tr > * {
  border-bottom: 2px solid var(--color-tertiary);
    border-top: 2px solid var(--color-tertiary);
}

.table-wrapper {
  overflow: auto;
}

.force-mobile {
  background-color: var(--color-secondary);
  min-height: calc(100vh - 6rem);
  width: 100%;
  max-width: 720px; margin: 0 auto;
}

::-webkit-scrollbar {
  height: 1rem;
  z-index: 100;
  position: relative;
}

::-webkit-scrollbar-thumb {
  background: var(--color-primary);
}

::-webkit-scrollbar-track {
  background: var(--color-tertiary);
}

caption {
  text-align: left;
  margin-bottom: 1rem;
}

h1 {
  margin-top: 0;
  margin-bottom: 1rem;
  font-family: var(--font-headings);
  font-size: 1.5rem;
  display: block;
}
              
            
!

JS

              
                // Thanks to Adrian Roselli for this really useful snippet https://adrianroselli.com/2017/11/a-responsive-accessible-table.html
// https://adrianroselli.com/2018/02/tables-css-display-properties-and-aria.html
// https://adrianroselli.com/2018/05/functions-to-add-aria-to-tables-and-lists.html
function AddTableARIA() {
  try {
    var allTables = document.querySelectorAll("table");
    for (var i = 0; i < allTables.length; i++) {
      allTables[i].setAttribute("role", "table");
    }
    var allRowGroups = document.querySelectorAll("thead, tbody, tfoot");
    for (var i = 0; i < allRowGroups.length; i++) {
      allRowGroups[i].setAttribute("role", "rowgroup");
    }
    var allRows = document.querySelectorAll("tr");
    for (var i = 0; i < allRows.length; i++) {
      allRows[i].setAttribute("role", "row");
    }
    var allCells = document.querySelectorAll("td");
    for (var i = 0; i < allCells.length; i++) {
      allCells[i].setAttribute("role", "cell");
    }
    var allHeaders = document.querySelectorAll("th");
    for (var i = 0; i < allHeaders.length; i++) {
      allHeaders[i].setAttribute("role", "columnheader");
    }
    // this accounts for scoped row headers
    var allRowHeaders = document.querySelectorAll("th[scope=row]");
    for (var i = 0; i < allRowHeaders.length; i++) {
      allRowHeaders[i].setAttribute("role", "rowheader");
    }
    // caption role not needed as it is not a real role and
    // browsers do not dump their own role with display block
  } catch (e) {
    console.log("AddTableARIA(): " + e);
  }
}

AddTableARIA();

const MAX_COLS = 5;
const MIN_COLS = 2;

function cropTable(visibleCols) {
  const table = document.querySelector("figure");
  const { width: tableWidth } = table.getBoundingClientRect();
  const cols = table.querySelectorAll("th, td");
  const newWidth = tableWidth / visibleCols;

  // Resize columns to fit table
  cols.forEach(function (col) {
    // Always make sure that col is cropped by at least 15%
    col.style.minWidth = newWidth + (newWidth * 0.15) + "px";
  });

  // Return if we are about to fall below min column count
  if (visibleCols <= MIN_COLS) {
    return;
  }

  // Measure a sample table column to check if resizing was successful
  const { width: colWidth } = cols[0].getBoundingClientRect();

  // Check if we should crop to 1 column less (calculate new column width)
  if (colWidth * visibleCols > tableWidth + newWidth * 0.95) {
    cropTable(visibleCols - 1);
  }
}

window.addEventListener("resize", function () {
  cropTable(MAX_COLS);
});

cropTable(MAX_COLS);

              
            
!
999px

Console