Pen Settings

HTML

CSS

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

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

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.

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

              
                <div class="container">
  <div class="row">
    <div class="col-xs-12">
      <h2>Display Flex & HTML5 Table with sticky headers</h2>
    </div>
    <div class="col-xs-6">
      <div class="panel panel-danger">
        <div class="panel-heading">Regular OLD Table</div>
        <div class="panel-body panel-regular-table">
          <table class="table table-striped table-hover">
            <thead>
              <tr>
                <th>one</th>
                <th>two</th>
                <th>three</th>
                <th>four</th>
                <th>five</th>
              </tr>
            </thead>
            <tbody class="scroll">
              <tr>
                <td>r1 content 1</td>
                <td>r1 content 2</td>
                <td>r1 content 3</td>
                <td>r1 content 4</td>
                <td>r1 content 5</td>
              </tr>
              <tr>
                <td>r2 content 1</td>
                <td>r2 content 2</td>
                <td>r2 content 3</td>
                <td>r2 content 4</td>
                <td>r2 content 5</td>
              </tr>
              <tr>
                <td>r3 content 1</td>
                <td>r3 content 2</td>
                <td>r3 content 3</td>
                <td>r3 content 4</td>
                <td>r3 content 5</td>
              </tr>
              <tr>
                <td>r4 content 1</td>
                <td>r4 content 2</td>
                <td>r4 content 3</td>
                <td>r4 content 4</td>
                <td>r4 content 5</td>
              </tr>
              <tr>
                <td>r5 content 1</td>
                <td>r5 content 2</td>
                <td>r5 content 3</td>
                <td>r5 content 4</td>
                <td>r5 content 5</td>
              </tr>
              <tr>
                <td>r6 content 1</td>
                <td>r6 content 2</td>
                <td>r6 content 3</td>
                <td>r6 content 4</td>
                <td>r6 content 5</td>
              </tr>
              <tr>
                <td>r7 content 1</td>
                <td>r7 content 2</td>
                <td>r7 content 3</td>
                <td>r7 content 4</td>
                <td>r7 content 5</td>
              </tr>
              <tr>
                <td>r8 content 1</td>
                <td>r8 content 2</td>
                <td>r8 content 3</td>
                <td>r8 content 4</td>
                <td>r8 content 5</td>
              </tr>
              <tr>
                <td>r9 content 1</td>
                <td>r9 content 2</td>
                <td>r9 content 3</td>
                <td>r9 content 4</td>
                <td>r9 content 5</td>
              </tr>
              <tr>
                <td>r10 content 1</td>
                <td>r10 content 2</td>
                <td>r10 content 3</td>
                <td>r10 content 4</td>
                <td>r10 content 5</td>
              </tr>
              <tr>
                <td>r11 content 1</td>
                <td>r11 content 2</td>
                <td>r11 content 3</td>
                <td>r11 content 4</td>
                <td>r11 content 5</td>
              </tr>
              <tr>
                <td>r12 content 1</td>
                <td>r12 content 2</td>
                <td>r12 content 3</td>
                <td>r12 content 4</td>
                <td>r12 content 5</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
    <div class="col-xs-6">
      <div class="panel panel-success">
        <div class="panel-heading">Flex Table with Sticky Headers</div>
        <div class="panel-body panel-flex-table">
          <table class="flex-table table table-striped table-hover">
            <thead>
              <tr>
                <th>one</th>
                <th>two</th>
                <th>three</th>
                <th>four</th>
                <th>five</th>
              </tr>
            </thead>
            <tbody class="scroll">
              <tr>
                <td>r1 content 1</td>
                <td>r1 content 2</td>
                <td>r1 content 3</td>
                <td>r1 content 4</td>
                <td>r1 content 5</td>
              </tr>
              <tr>
                <td>r2 content 1</td>
                <td>r2 content 2</td>
                <td>r2 content 3</td>
                <td>r2 content 4</td>
                <td>r2 content 5</td>
              </tr>
              <tr>
                <td>r3 content 1</td>
                <td>r3 content 2</td>
                <td>r3 content 3</td>
                <td>r3 content 4</td>
                <td>r3 content 5</td>
              </tr>
              <tr>
                <td>r4 content 1</td>
                <td>r4 content 2</td>
                <td>r4 content 3</td>
                <td>r4 content 4</td>
                <td>r4 content 5</td>
              </tr>
              <tr>
                <td>r5 content 1</td>
                <td>r5 content 2</td>
                <td>r5 content 3</td>
                <td>r5 content 4</td>
                <td>r5 content 5</td>
              </tr>
              <tr>
                <td>r6 content 1</td>
                <td>r6 content 2</td>
                <td>r6 content 3</td>
                <td>r6 content 4</td>
                <td>r6 content 5</td>
              </tr>
              <tr>
                <td>r7 content 1</td>
                <td>r7 content 2</td>
                <td>r7 content 3</td>
                <td>r7 content 4</td>
                <td>r7 content 5</td>
              </tr>
              <tr>
                <td>r8 content 1</td>
                <td>r8 content 2</td>
                <td>r8 content 3</td>
                <td>r8 content 4</td>
                <td>r8 content 5</td>
              </tr>
              <tr>
                <td>r9 content 1</td>
                <td>r9 content 2</td>
                <td>r9 content 3</td>
                <td>r9 content 4</td>
                <td>r9 content 5</td>
              </tr>
              <tr>
                <td>r10 content 1</td>
                <td>r10 content 2</td>
                <td>r10 content 3</td>
                <td>r10 content 4</td>
                <td>r10 content 5</td>
              </tr>
              <tr>
                <td>r11 content 1</td>
                <td>r11 content 2</td>
                <td>r11 content 3</td>
                <td>r11 content 4</td>
                <td>r11 content 5</td>
              </tr>
              <tr>
                <td>r12 content 1</td>
                <td>r12 content 2</td>
                <td>r12 content 3</td>
                <td>r12 content 4</td>
                <td>r12 content 5</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

  </div>
  <div class="row">
    <div class="col-xs-12">
      <h3><span class="label label-info">Setup</span></h3>
    </div>
    <div class="col-xs-12">
      Add display flex.
      <ul>
        <li>Add display flex with flex-direction column to the <code>&lt;table&gt;</code> & set height to 100% (will be 100% in relation to parent container)</li>
        <li>Add display flex to <code>&lt;tr&gt;</code></li>
        <li>Add display block to <code>&lt;thead&gt;</code> & <code>&lt;tbody&gt;</code> (support for internet explorer 9+ so columns align correctly)</li>
        <li>Add overflow-y auto or scroll to <code>&lt;tbody&gt;</code> (so the table body only scrolls)</li>
        <li>Add flex 1 to <code>&lt;tbody&gt;</code> (sets the table body to flex accordingly to height of table) ***we do not add a height or flex to the table header, ie <code>&lt;thead&gt;</code>, so the header elements will wrap correctly</li>
      </ul>
    </div>
    <div class="col-xs-6">
      <h4><span class="label label-default">SCSS properties</span></h4>
      <pre>
table.flex-table {
  display: flex;
  flex-direction: column;
  height: 100%;
  thead, tbody {
    display: block;
  }
  tbody {
    flex: 1;
    overflow-y: scroll;
  }
  tr {
    display: flex;
    // flex-direction by default is row (optional)
    flex-direction: row;
    td, th {
      // add display block for internet explorer 9+
      display: block;
      flex: 1;
      // control column widths with :nth-child(n)
      &:nth-child(n) {
        flex: n;
      }
    }
  }
}
</pre>
    </div>
    <div class="col-xs-6">
      <h4><span class="label label-default">Column Width</span></h4>
      <span>Using display flex, all columns will have a flex width of 1 by default. To dictate width of columns, use :nth-child(n) on tr>th, tr>td.</span>
      <pre>
tr {
  th,td {
    // changing column 2 width
    &:nth-child(2) {
      flex: 3;
    }
    // changing column 4 width
    &:nth-child(4) {
      flex: 2;
    }
  }
}
</pre>
    </div>
    <div class="col-xs-12">
      <h4><span class="label label-success">Table with custom flex widths</span></h4>
      <div class="panel panel-warning">
        <div class="panel-heading">Sticky Headers Table</div>
        <div class="panel-body panel-flex-table">
          <table class="flex-table table table-striped table-hover table-bordered flex-widths">
            <thead>
              <tr>
                <th>one</th>
                <th>two <span class="badge">&nbsp;( flex width 3 )&nbsp;</span></th>
                <th>three</th>
                <th>four <span class="badge">&nbsp;( flex width 2 )&nbsp;</span></th>
                <th>five</th>
              </tr>
            </thead>
            <tbody class="scroll">
              <tr>
                <td>r1 content 1</td>
                <td>r1 content 2</td>
                <td>r1 content 3</td>
                <td>r1 content 4</td>
                <td>r1 content 5</td>
              </tr>
              <tr>
                <td>r2 content 1</td>
                <td>r2 content 2</td>
                <td>r2 content 3</td>
                <td>r2 content 4</td>
                <td>r2 content 5</td>
              </tr>
              <tr>
                <td>r3 content 1</td>
                <td>r3 content 2</td>
                <td>r3 content 3</td>
                <td>r3 content 4</td>
                <td>r3 content 5</td>
              </tr>
              <tr>
                <td>r4 content 1</td>
                <td>r4 content 2</td>
                <td>r4 content 3</td>
                <td>r4 content 4</td>
                <td>r4 content 5</td>
              </tr>
              <tr>
                <td>r5 content 1</td>
                <td>r5 content 2</td>
                <td>r5 content 3</td>
                <td>r5 content 4</td>
                <td>r5 content 5</td>
              </tr>
              <tr>
                <td>r6 content 1</td>
                <td>r6 content 2</td>
                <td>r6 content 3</td>
                <td>r6 content 4</td>
                <td>r6 content 5</td>
              </tr>
              <tr>
                <td>r7 content 1</td>
                <td>r7 content 2</td>
                <td>r7 content 3</td>
                <td>r7 content 4</td>
                <td>r7 content 5</td>
              </tr>
              <tr>
                <td>r8 content 1</td>
                <td>r8 content 2</td>
                <td>r8 content 3</td>
                <td>r8 content 4</td>
                <td>r8 content 5</td>
              </tr>
              <tr>
                <td>r9 content 1</td>
                <td>r9 content 2</td>
                <td>r9 content 3</td>
                <td>r9 content 4</td>
                <td>r9 content 5</td>
              </tr>
              <tr>
                <td>r10 content 1</td>
                <td>r10 content 2</td>
                <td>r10 content 3</td>
                <td>r10 content 4</td>
                <td>r10 content 5</td>
              </tr>
              <tr>
                <td>r11 content 1</td>
                <td>r11 content 2</td>
                <td>r11 content 3</td>
                <td>r11 content 4</td>
                <td>r11 content 5</td>
              </tr>
              <tr>
                <td>r12 content 1</td>
                <td>r12 content 2</td>
                <td>r12 content 3</td>
                <td>r12 content 4</td>
                <td>r12 content 5</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
    <div class="col-xs-12">
      <h4><span class="label label-danger">Header alignment or lack thereof</span></h4>
      <div class="row">
        <div class="col-xs-6">
          <p>When the <code>&lt;tbody&gt;</code> has the overflow-y set to scroll, there will be a slight misalignment with the table headers and table body. To account for this misalignment, we can add an overflow-y: scroll; and overflow-x: hidden to our
            <code>thead tr</code>.</p>
        </div>

        <div class="col-xs-6">
          <pre>
table {
  thead > tr {
    overflow-y: scroll;
    overflow-x: hidden;
  }
}
</pre>
        </div>
      </div>
    </div>
    <div class="col-xs-12">
    <div class="panel panel-success">
        <div class="panel-heading">Sticky Headers Table with Header Aligned</div>
        <div class="panel-body panel-flex-table">
          <table class="flex-table table table-striped table-hover table-bordered flex-widths flex-table-aligned">
            <thead>
              <tr>
                <th>one</th>
                <th>two <span class="badge">&nbsp;( flex width 3 )&nbsp;</span></th>
                <th>three</th>
                <th>four <span class="badge">&nbsp;( flex width 2 )&nbsp;</span></th>
                <th>five</th>
              </tr>
            </thead>
            <tbody class="scroll">
              <tr>
                <td>r1 content 1</td>
                <td>r1 content 2</td>
                <td>r1 content 3</td>
                <td>r1 content 4</td>
                <td>r1 content 5</td>
              </tr>
              <tr>
                <td>r2 content 1</td>
                <td>r2 content 2</td>
                <td>r2 content 3</td>
                <td>r2 content 4</td>
                <td>r2 content 5</td>
              </tr>
              <tr>
                <td>r3 content 1</td>
                <td>r3 content 2</td>
                <td>r3 content 3</td>
                <td>r3 content 4</td>
                <td>r3 content 5</td>
              </tr>
              <tr>
                <td>r4 content 1</td>
                <td>r4 content 2</td>
                <td>r4 content 3</td>
                <td>r4 content 4</td>
                <td>r4 content 5</td>
              </tr>
              <tr>
                <td>r5 content 1</td>
                <td>r5 content 2</td>
                <td>r5 content 3</td>
                <td>r5 content 4</td>
                <td>r5 content 5</td>
              </tr>
              <tr>
                <td>r6 content 1</td>
                <td>r6 content 2</td>
                <td>r6 content 3</td>
                <td>r6 content 4</td>
                <td>r6 content 5</td>
              </tr>
              <tr>
                <td>r7 content 1</td>
                <td>r7 content 2</td>
                <td>r7 content 3</td>
                <td>r7 content 4</td>
                <td>r7 content 5</td>
              </tr>
              <tr>
                <td>r8 content 1</td>
                <td>r8 content 2</td>
                <td>r8 content 3</td>
                <td>r8 content 4</td>
                <td>r8 content 5</td>
              </tr>
              <tr>
                <td>r9 content 1</td>
                <td>r9 content 2</td>
                <td>r9 content 3</td>
                <td>r9 content 4</td>
                <td>r9 content 5</td>
              </tr>
              <tr>
                <td>r10 content 1</td>
                <td>r10 content 2</td>
                <td>r10 content 3</td>
                <td>r10 content 4</td>
                <td>r10 content 5</td>
              </tr>
              <tr>
                <td>r11 content 1</td>
                <td>r11 content 2</td>
                <td>r11 content 3</td>
                <td>r11 content 4</td>
                <td>r11 content 5</td>
              </tr>
              <tr>
                <td>r12 content 1</td>
                <td>r12 content 2</td>
                <td>r12 content 3</td>
                <td>r12 content 4</td>
                <td>r12 content 5</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
    <div class="col-xs-12">
      <h3><span class="label label-primary">Additional resources</span></h3>
      <p>For more info on CSS3 display flex, you can visit <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex">MDN</a>. You can also visit <a href="http://the-echoplex.net/flexyboxes/">echoplex</a> to see an interactive demo of flexbox in action.</p>
    </div>
  </div>

</div>
              
            
!

CSS

              
                .container {
  .panel-body {
    height: 300px;
    padding: 0px; 
    &.panel-regular-table {
      overflow-y: auto;
    }
    table.flex-table {
      display: flex;
      flex-direction: column;
      height: 100%;
      thead,
      tbody {
        display: block;
      }
      thead {
        margin-right: 0px; //margin to align correctly to scrollbar in table body
      }
      tbody {
        flex: 1; //variable height
        overflow-y: scroll;
      }
      tr {
        width: 100% ;
        display: flex;
        td,
        th {
          display: block;
          flex: 1;
        }
      }
    }
    table.flex-widths {
      &.flex-table-aligned {
        thead > tr {
          overflow-y: scroll;
          overflow-x: hidden;
        }
      }
      tr {
        td, th {
          &:nth-child(2) {
            flex: 3;
          }
          &:nth-child(4) {
            flex: 2;
          }
        }
      }
    }
  }
}
              
            
!

JS

              
                
              
            
!
999px

Console