cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <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>
            
          
!
            
              $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;
        }
      }      
    }
  }
}
            
          
!
            
              $(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
Loading ..................

Console