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

              
                <table>
  <tr>
    <th></th>
    <th>Lugnuts</th>
    <th>Rutabegas</th>
    <th>Dumptrucks</th>
    <th>Saturns</th>
  </tr>
  <tr>
    <th>
      <p>Alabama</p>
    </th>
    <td>
      16,000,000
    </td>
    <td>
      8,000,000
    </td>
    <td>
      4,000,000
    </td>
    <td>
      90,000,000
    </div>
  </tr>
  <tr>
    <th>
      <p>Arkansas</p>
    </th>
    <td>
      1,000,000
    </td>
    <td>
      100,000,000
    </td>
    <td>
      88,000,000
    </td>
    <td>
      9,000,000,000
    </div>
  </tr>
  <tr>
    <th>
      <p>Montana</p>
    </th>
    <td>
      44,000,000
    </td>
    <td>
      91,000,000
    </td>
    <td>
      43,000,000
    </td>
    <td>
      10,000,000
    </div>
  </tr>
  <tr>
    <th>
      <p>New Jersey</p>
    </th>
    <td>
      44,000,000
    </td>
    <td>
      16,000,000
    </td>
    <td>
      33,000,000
    </td>
    <td>
      80,000,000
    </div>
  </tr>
</table>
              
            
!

CSS

              
                $max_columns: 10;

* {
  box-sizing: border-box;
}

table {
  width: 100%;
}

tr {
  width: 100%;
  display: block;
  clear: both;
}

tr > * {
  float: left;
  height: 50px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  position: relative;
  border: solid black 1px;
  border-collapse: collapse;
  p {
    width: 100%;
    display: flex;
    justify-content: center;
    margin: 0;
    &.value {
      display: none;
    }
    &.item {
      display: none;
    }
  }
  @for $i from 1 to $max_columns {
    @include exactly($i) {
      $column-count: $i;
      width: calc(100% / #{$column-count});
    }
  }
}

th {
  font-weight: bold;
  cursor: auto;
}


@media screen and (max-width: 720px) {
  table {
    display: flex;
    flex-wrap: wrap;
  }
  tr:first-child{
    display: none;
  }
  tr:not(:first-child) {
    > * {      
      @for $i from 1 to $max_columns {
        @include exactly($i) {
          $column-count: $i;
          width: calc(100% / calc( 1 + calc(#{$column-count} / 2)));
          @if ($i%2!=0) {
            width: calc(100% / calc( 1 + calc(calc(#{$column-count} - 1) / 2)));
          }
        }
      }
      &:nth-child(1) {
        height: 100px;
      }
      p {
        &.item, &.value {
          display: flex;
          justify-content: center;
        }
      }
    }
    td {
      &:hover {
        cursor: pointer;
        background: rgb(115,115,115);
        color: white;
      }
      &.selected {
        background: rgb(175, 175, 175);
        color: white;
        cursor: auto;
        &:hover {
          background: rgb(175, 175, 175);
          cursor: auto;
        }
      }      
    }
  }
}
              
            
!

JS

              
                $(document).ready(function(){
  // We want this to work for every table on the page
  $('table').each(function(){
    const table = $(this);
    // Get all of the table headings
    const topCells = table.find('tr:first-child th');
    // Get the bottom rows
    const bottomRows = table.find('tr:not(:first-child)');
    // We need to insert a label in each cell of a column, so we start from the heading
    topCells.each(function(index){
      // We don't care about the top-left cell—it doesn't label the cells beneath it
      if (index != 0) {
        const headerCell = $(this)
        const headerIndex = index;
        // Get the item name from the cell
        const itemName = headerCell.text();
        // Set the cell's data-item to the item name (useful later)
        $(this).data('item', itemName);
        // Now we need to label the appropriate cell on each line
        bottomRows.each(function(index){
          // Find the right cell (the index is one lower because of the <th> at the start)
          const cell = $($(this).find('td')[headerIndex - 1]);
          // Set that cell's data-item to the same item name
          cell.data('item', itemName);
          // Create an HTML element with the item name
          const itemEntry = `<p class="item">${itemName}</p>`
          // Append it to the cell (we hide it with CSS)
          cell.append(itemEntry);       
        });
      }
    })
    // Find the heading of each row
    const rowHeads = bottomRows.find('th');
    rowHeads.each(function(){
      // Add an empty <p> that we'll populate later
      $(this).append('<p class="value"></p>');
    });
  })
  // Event handler for clicking on cells
  $('td').click(function(){
    // Unselect everything
    $('td').removeClass('selected');
    // Select this cell
    $(this).addClass('selected');
    // Find the item this cell represents
    const itemName = $(this).data('item');
    // Find the cell's parent table
    const table = $(this).closest('table');
    // Find that table's bottom rows
    const bottomRows = table.find('tr:not(:first-child)');
    bottomRows.each(function(){
      const row = $(this);
      // Find all the cells in the row
      const itemCells = row.find('td');
      itemCells.each(function(){
        // Find the cell in question's item
        const thisItem = $(this).data('item');
        // If it's the same as the item we clicked
        if (thisItem == itemName) {
          // Select it
          $(this).addClass('selected');
          // Set the row's value label to that amount
          const thisValue = $(this).text();
          row.find('.value').text(thisValue);
        }
      });
    });                                          
  });
});
              
            
!
999px

Console