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


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


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.


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.


        <p>Accessible, sortable HTML table</p>
            <caption>The table is taken from <a href="">
                    <em>Five dysfunctions of a team</em>
                    <th>Behavioral pattern</th>
                    <td>Absence of trust</td>
                    <td>Unwilling to be vulnerable within the group</td>
                    <td>Fear of conflict</td>
                    <td>Seeking artificial harmony over a constructive passionate debate</td>
                    <td>Lack of commitment</td>
                    <td>Feigning buy-in for group decisions creates ambiguity throughout the organization</td>
                    <td>Avoidance of accountability</td>
                    <td>Ducking the responsibility to call peers on counterproductive behavior which sets low standards</td>
                    <td>Inattention to results</td>
                    <td>Focusing on personal success, status, and ego before team success</td>


                body {
  padding: 1rem;

table * {
  text-align: left;

table th, table td {
  padding: .5em;

table.sotable button.restore-order {
    all: unset;
    /*remove all button styling*/
    cursor: pointer;

table.sotable button.restore-order {
    font-weight: bold;

th.sotable-column .indicator {
    font-size: smaller;
    margin-left: .5em;

@media print {
    table.sotable caption p.indicator {
        display: none;


                //authored by Ulf Schneider
//initial idea from
//and incorporated ideas from
//plus my own
const ROW_SELECTOR = 'tr:nth-child(n+2):not(table table tr)';

let defaults = {
    indicatorAsc: 'ᐃ',
    indicatorDsc: 'ᐁ',
    sortHint: 'Sort the table by clicking on a column heading.',
    restoreHint: 'Restore the original order by clicking <button>Restore Order</button>.',
    whiteList: '',
    blackList: ''
let settings;
let whiteElements;
let blackElements;

function getArray(value) {
    if (value && typeof value === 'string' || value instanceof String) {
        return value.split(',');
    } else if (value) {
        //assuming value is an array
        return value;
    return [];

function isWhitelisted(table) {
    if (whiteElements.size) {
        return whiteElements.has(table);
    return true;

function isBlacklisted(table) {
    if (table.classList.contains('no-so')) {
        return true;

    return blackElements.has(table);

function setConfig(options) {
    settings = Object.assign({}, defaults, options);
    whiteElements = new Set();
    blackElements = new Set();

    for (let white of getArray(settings.whiteList)) {
        for (let element of document.querySelectorAll(white)) {
    for (let black of getArray(settings.blackList)) {
        for (let element of document.querySelectorAll(black)) {

function getIndexedRowValue(row, columnIndex) {
    return row.children[columnIndex].innerText || row.children[columnIndex].textContent;

function getColumnIndex(th) {
    return Array.from(th.parentNode.children).indexOf(th);

function getColumn(table, columnIndex) {
    let rows = Array.from(table.querySelectorAll('tr'));
    return => row.children[columnIndex]);

function canColumnSort(column) {
    if (column.length && column[0].tagName != 'TH') {
        //first element is not a th
        return false;
    return true;

function isTableSorted(table) {
    return table.querySelector('.asc:not(table table asc)') ||  table.querySelector('.dsc:not(table table dsc)');

function getCellValue(cell) {
    return cell.innerText || cell.textContent;

function compareValues(value1, value2) {
    if (!isNaN(value1) && !isNaN(value2)) {
        return value1 - value2;
    } else {
        return String(value1).localeCompare(String(value2));

function isColumnAsc(column) {
    //is the column sorted ascending?
    let values = column.filter(cell => cell.tagName != 'TH').map(cell => getCellValue(cell));
    let sortedValues = [...values].sort(compareValues);
    return String(values) == String(sortedValues);

function comparer(columnIndex, asc) {
    //compare table cell values of two rows for the given column index
    return function(row1, row2) {
        return compareValues(getIndexedRowValue(asc ? row1 : row2, columnIndex), getIndexedRowValue(asc ? row2 : row1, columnIndex));

function clearTableSortIndication(table) {
    //remove all sort indicators
    table.querySelectorAll('th.sotable-column:not(table table th)').forEach(th => {
        th.ariaSort = null;
        th.querySelectorAll('.indicator').forEach(indicator => indicator.remove());

function indicateColumnSortDirection(table, th, asc) {
    //if the table has been sorted
    //provide the indication in the 
    //column headings

    let indicator = document.createElement('span');
    indicator.ariaHidden = true;

    if (asc) {
        th.ariaSort = 'ascending';
        indicator.innerHTML = settings.indicatorAsc;
    } else {
        th.ariaSort = 'descending';
        indicator.innerHTML = settings.indicatorDsc;


function storeOrigTableOrder(table) {
    //store the original order of the table
    //for later re-use
    let order = 1;
    table.querySelectorAll(`${ROW_SELECTOR}:not([orig-order])`).forEach(tr => {
        tr.setAttribute('orig-order', order);

function restoreOrigTableOrder(table) {
    //restore the original order of the table

        .sort((row1, row2) => {
            return parseInt(row1.getAttribute('orig-order')) - parseInt(row2.getAttribute('orig-order'));
        .forEach(tr => table.appendChild(tr));

function indicateRestoreTableOrder(table) {
    //show a hint how to restore the original order 
    table.querySelectorAll('caption p.indicator:not(table table caption p.indicator)').forEach(indicator => {
        if (!indicator.querySelector('.restore-hint') && settings.restoreHint) {
            indicator.innerHTML += ` <span class="restore-hint">${settings.restoreHint}</span>`;
            //in case the hint contained a button, 
            //assign a css class and a restore order action to that button
            indicator.querySelectorAll('.restore-hint button').forEach(button => {
                button.addEventListener('click', () => {

function indicateSortableTable(table) {
    //prepare a table caption element
    //to contain a hint that the table
    //can be sorted
    let caption = table.querySelector('caption');
    if (!caption) {
        caption = document.createElement('caption');
    let indicator = table.querySelector('p.indicator');
    if (!indicator) {
        indicator = document.createElement('p');
        indicator.innerHTML = settings.sortHint;

function insertColumnSortToggle(th) {
    //convert the table th´s so that their previous content
    //is wrapped inside of a button element
    //that button will toggle the sorting of the column

    if (th.querySelectorAll(':not(abbr):not(b):not(br):not(cite):not(code):not(em):not(i):not(img):not(kbd):not(label):not(mark):not(small):not(span):not(strong):not(sub):not(sup):not(svg):not(time)').length) {
        //in this case
        //we cannot replace the th content with a button
        //because buttons are allowed to only contain phrasing content
        return null;
    } else {
        //use a button toggle for accessibility
        th.innerHTML = `<button>${th.innerHTML}</button>`;
        //watch out for the css
        //th.sotable-column>button is referring to
        //this element
        return th.firstChild;

function sotable(options) {
    document.querySelectorAll('tr:first-child>th:not(.no-so)').forEach(th => {

        let table = th.closest('table');
        if (!isBlacklisted(table) && isWhitelisted(table)) {
            let columnIndex = getColumnIndex(th);
            let column = getColumn(table, columnIndex);

            if (canColumnSort(column)) {
                let toggle = insertColumnSortToggle(th);
                if (toggle) {
                    toggle.addEventListener('click', () => {
                        let asc = !isColumnAsc(getColumn(table, columnIndex))
                            .sort(comparer(getColumnIndex(th), asc))
                            .forEach(tr => table.appendChild(tr));
                        indicateColumnSortDirection(table, th, asc);