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

              
                <h1>Responsive Tables: Collapse to tabs & accordions</h1>
<ul>
  <li>Variant ways of collapsing layout tables on small screens</li>
  <li>This includes the basic table styles from <a href="https://codepen.io/davidelrizzo/pen/yYzpKK?editors=100" target="_blank">this Pen</a>.</li>
</ul>

<!------------------------------------------>


<h2>Tab collapse table</h2>
<ul>
  <li>Each column collapses to tabs on small screens</li>
  <li>Note: The Tabs themselvs are not responsive and will break on a small enough screen with long enough tab wording</li>
</ul> 

<div class="Rtable Rtable--3cols Rtable--collapse js-RtableTabs">
  
  <div class="Tablist u-hiddenLarge" role="tablist"> 
    <button class="Tab" role="tab" aria-selected="true">Ned</button>
    <button class="Tab" role="tab" aria-selected="false">Jon</button>
    <button class="Tab" role="tab" aria-selected="false">Arya</button>
  </div>

  <div style="order:0;" class="Rtable-cell Rtable-cell--head"><h3>Eddard Stark</h3></div>
  <div style="order:1;" class="Rtable-cell">Has a sword named Ice</div>
  <div style="order:2;" class="Rtable-cell">No direwolf</div>
  <div style="order:3;" class="Rtable-cell Rtable-cell--foot"><strong>Lord of Winterfell</strong></div>

  <div style="order:0;" class="Rtable-cell Rtable-cell--head"><h3>Jon Snow</h3></div>
  <div style="order:1;" class="Rtable-cell">Has a sword named Longclaw</div>
  <div style="order:2;" class="Rtable-cell">Direwolf: Ghost</div>
  <div style="order:3;" class="Rtable-cell Rtable-cell--foot"><strong>Knows nothing</strong></div>

  <div style="order:0;" class="Rtable-cell Rtable-cell--head"><h3>Arya Stark</h3></div>
  <div style="order:1;" class="Rtable-cell">Has a sword named Needle</div>
  <div style="order:2;" class="Rtable-cell">Direwolf: Nymeria</div>
  <div style="order:3;" class="Rtable-cell Rtable-cell--foot"><strong>No one</strong></div>

</div>


<!------------------------------------------>


<h2>Accordion collapse table</h2>
<ul>
  <li>Each column collapses to accordions on small screens</li>
</ul> 

<div class="Rtable Rtable--4cols Rtable--collapse js-RtableAccordions">
  
  <button class="Accordion u-hiddenLarge" role="tab" aria-selected="true">Ned</button>
  <div class="Rtable-cell Rtable-cell--head"><h3>Eddard Stark</h3></div>
  <div class="Rtable-cell">Has a sword named Ice</div>
  <div class="Rtable-cell">No direwolf</div>
  <div class="Rtable-cell Rtable-cell--foot"><strong>Lord of Winterfell</strong></div>
  
  <button class="Accordion u-hiddenLarge" role="tab" aria-selected="false">Jon</button>
  <div class="Rtable-cell Rtable-cell--head"><h3>Jon Snow</h3></div>
  <div class="Rtable-cell">Has a sword named Longclaw</div>
  <div class="Rtable-cell">Direwolf: Ghost</div>
  <div class="Rtable-cell Rtable-cell--foot"><strong>Knows nothing</strong></div>

  <button class="Accordion u-hiddenLarge" role="tab" aria-selected="false">Arya</button>
  <div class="Rtable-cell Rtable-cell--head"><h3>Arya Stark</h3></div>
  <div class="Rtable-cell">Has a sword named Needle</div>
  <div class="Rtable-cell">Direwolf: Nymeria</div>
  <div class="Rtable-cell Rtable-cell--foot"><strong>No one</strong></div>

</div>


<!------------------------------------------>


<small>- end of examples -</small>
              
            
!

CSS

              
                /* Variables
==================================== */
@tabColour: darkcyan;
@accordionColour: darkcyan;


/* Tab Styling
==================================== */
.Tablist {
  display: flex;
  flex-direction: row;
  margin-left: -@bw;
}
.Tab {
  padding: @pad*0.6;
  margin: 0 @bw @bw 0;
  text-align: center;
  background-color: @tabColour;
  border: solid @bw @tabColour;
  border-bottom-width: 0;
  text-decoration: none;
  transition: background-color 0.1s;
  cursor: pointer;
  &:hover, &:focus {
    background-color: mix(white,@tabColour,10%);
    border-color: mix(white,@tabColour,10%);
    outline: none;
  }
  &[aria-selected="false"]:active {
    margin-top: @pad*0.2;
    padding-bottom: @pad*0.4;
  }
  &[aria-selected="true"] {
    background: mix(white,@tabColour,90%);
    cursor: default;
  }
}



/* Accordion Styling
==================================== */
.Accordion {
  @iconSize: 20px;
  position: relative; top: -@bw; left: -@bw; //compensate for border offset
  width: 100%;
  width: ~"calc(100% + @{bw})";
  margin: 0 0 @pad*0.5 0;
  padding: @pad*0.6 @pad*0.6 @pad*0.6 (10px + @iconSize);
  border-radius: @pad*0.3;
  text-align: left;
  border: solid @bw mix(black,@accordionColour,15%);
  background-color: @accordionColour;
  text-decoration: none;
  transition: background-color 0.1s;
  cursor: pointer;
  &:hover, &:focus {
    outline: none;
    filter: contrast(150%);
  }
  &[aria-selected="true"] {
    margin-bottom: 0;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    border-bottom-width: 0;
    background: mix(white,@accordionColour,70%);
  }
  &:before {
    content: "+";
    position: absolute;
    top: 50%;
    left: 5px;
    width: @iconSize;
    height: @iconSize;
    margin-top: -(@iconSize/2);
    vertical-align: middle;
    text-align: center;
    font-size: @iconSize;
    line-height: @iconSize;
    background-size: @iconSize @iconSize;
  }
  &[aria-selected="true"]:before {
    content: "-";
  }
}

              
            
!

JS

              
                (function ($) {
  "use strict";
  $.fn.responsiveTable = function() { 

    var toggleColumns = function($table) {
      var selectedControls = [];
      $table.find(".Accordion, .Tab").each( function() {
        selectedControls.push( $(this).attr("aria-selected") );
      });
      var cellCount = 0, colCount = 0;
      var setNum = $table.find(".Rtable-cell").length / Math.max( $table.find(".Tab").length, $table.find(".Accordion").length );
      $table.find(".Rtable-cell").each( function() {
        $(this).addClass("u-hiddenSmall");
        if( selectedControls[colCount] === "true" ) $(this).removeClass("u-hiddenSmall");
        cellCount++;
        if( cellCount % setNum === 0 ) colCount++; 
      });
    };
    $(this).each(function(){ toggleColumns($(this)); });

    $(this).find(".Tab").click( function() {
      $(this).attr("aria-selected","true").siblings().attr("aria-selected","false");
      toggleColumns( $(this).parents(".Rtable") );
    });

    $(this).find(".Accordion").click( function() {
      $(this).attr("aria-selected", $(this).attr("aria-selected") !== "true" );
      toggleColumns( $(this).parents(".Rtable") );
    });

  };
}(jQuery));


$(".js-RtableTabs, .js-RtableAccordions").responsiveTable();
              
            
!
999px

Console