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.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console