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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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>
  </div>
  <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%;
  //display: flex;
  flex-wrap: wrap;
  border: solid black 1px;
}

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

tr > * {
  float: left;
  width: calc(100% / var(--column-count));
  height: 50px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  position: relative;
  border: solid black 1px;
  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;
      @if ($i%2!=0) {
        --odd-adjuster: 1;
      }
    }
  }
}

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


@media screen and (max-width: 720px) {
  table {
    display: flex;
  }
  tr:first-child{
    display: none;
  }
  tr:not(:first-child) {
    > * {
      width: calc(100% / calc( 1 + calc(calc(var(--column-count) - var(--odd-adjuster, 0)) / 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