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

              
                		<table>
			<caption>Sortable Table</caption>
			<thead>
				<tr>
					<th scope="col" data-tablesort="text">Game</th>
					<th scope="col" data-tablesort="number">Price</th>
					<th scope="col" data-tablesort="date">Release date</th>
					<th scope="col" data-tablesort="number">Rating</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<th scope="row">Super Mario Odyssey</th>
					<td>49.99</td>
					<td>October 27, 2017</td>
					<td>97</td>
				</tr><tr>
					<th scope="row">The Legend of Zelda: Breath of the Wild</th>
					<td>59.99</td>
					<td>March 3, 2017</td>
					<td>97</td>
				</tr><tr>
					<th scope="row">Super Smash Bros. Ultimate</th>
					<td>59.99</td>
					<td>December, 7 2018</td>
					<td>93</td>
				</tr><tr>
					<th scope="row">Ori and the Will of the Wisps</th>
					<td>24.99</td>
					<td>September 17, 2020</td>
					<td>93</td>
				</tr><tr>
					<th scope="row">Animal Crossing: New Horizons</th>
					<td>49.99</td>
					<td>March 20, 2020</td>
					<td>90</td>
				</tr>
			</tbody>
		</table>

		<table>
			<caption data-tablesort-reset>Regex, DateTime, Input sorting</caption>
			<thead>
				<tr>
					<th
						scope="col"
						data-tablesort="text"
					>Name</th>
					<th
						scope="col"
						data-tablesort="number"
						data-tablesort-rxmatch="^\s*([0-9]+)"
					>Place</th>
					<th
						scope="col"
						data-tablesort="date"
					>When</th>
					<th
						scope="col"
						data-tablesort="valueText"
					>Designation</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<th scope="row">Luke</th>
					<td>33rd</td>
					<td><time datetime="1978-05-25T17:00:00-05:00">A New Hope</time</td>
					<td><input value="Red Five" readonly></td>
				</tr><tr>
					<th scope="row">Mara</th>
					<td>54th</td>
					<td><time datetime="1991-08-07T17:00:00-05:00">Heir to the Empire</time</td>
					<td><input value="Mother" readonly></td>
				</tr><tr>
					<th scope="row">Grogu</th>
					<td>1st</td>
					<td><time datetime="2019-11-12T00:00:00-05:00">The Mandalorian</time</td>
					<td><input value="Kid" readonly></td>
				</tr><tr>
					<th scope="row">Ahsoka</th>
					<td>6th</td>
					<td><time datetime="2008-08-10T00:00:00-05:00">The Clone Wars</time</td>
					<td><input value="Fulcrum" readonly></td>
				</tr><tr>
					<th scope="row">Rex</th>
					<td>501st</td>
					<td><time datetime="2008-10-03T00:00:00-05:00">The Clone Wars</time</td>
					<td><input value="CT-7567" readonly></td>
				</tr><tr>
					<th scope="row">Annakin</th>
					<td>4th</td>
					<td><time datetime="1999-05-19T00:00:00-05:00">The Phantom Menace</time</td>
					<td><input value="Darth Vader" readonly></td>
				</tr>
			</tbody>
		</table>

              
            
!

CSS

              
                body, table, input, select, textarea, button {
	font:normal 1em/1.5 arial,helvetica,sans-serif;
}

body {
	background:#DEF;
}

h1 {
	text-align:center;
}

caption {
	padding:0.4em 0.4em;
	font-weight:bold;
	font-size:1.25em;
	background:#048;
	color:#FFF;
}

caption[data-tableSort-reset] {
	text-align:left;
}

table {
	margin:0 auto 2em;
	border-collapse:collapse;
	background:#FFF;
	border:solid #048;
	border-width:0 1px 1px;
}

thead th {
	background:#26A;
	color:#FFF;
}

th,
td {
	padding:0.25em 0.5em;
	text-align:center;
	border-bottom:1px solid #048;
}

thead th:first-child,
tbody th {
	text-align:left;
}

tbody th {
	font-weight:normal;
}

tbody tr:nth-child(even) > * {
	background:#CDE;
}

caption[data-tableSort-reset] button,
th[data-tablesort] button {
	text-decoration:underline;
	border:none;
	background:transparent;
	color:inherit;
	cursor:pointer;
}

caption[data-tableSort-reset] button {
	float:right;
	font-size:0.8em;
	margin-top:0.25em;
}

th[data-tablesort] button {
	padding:0 0 0 1.2em;
	font-size:1em;
	font-weight:bold;
}

th[data-tablesort]:first-child button {
	padding:0;
}

th[data-tablesort] button:after {
	display:inline-block;
	width:1.2em;
	text-align:right;
	content:"";
}

th[data-tablesort] button[value="1"]:after {
	content:"\25BC";
}

th[data-tablesort] button[value="0"]:after {
	content:"\25B2";
}

td input {
	padding:0.25em 0.5em;
}

              
            
!

JS

              
                (function(d) {

	"use strict";

	var
		comps = {
			number : function(a, b) {
				return a.value - b.value;
			},
			text : function(a, b) {
				return a.value > b.value ? 1 : a.value < b.value ? -1 : 0;
			}
		},
		sorts = {
			date : {
				get : function(cell) {
					var time = cell.querySelector("time");
					if (time) {
						var datetime = time.getAttribute("datetime");
						return Date.parse(datetime || rxMatch(time.textContent));
					}
					return Date.parse(rxMatch(cell.textContent));
				},
				sort : comps.number
			},
			number : {
				get : function(cell) {
					return +(sorts.text.get(cell) || 0);
				},
				sort: comps.number
			},
			text : {
				get : function(cell) {
					return rxMatch(cell.textContent);
				},
				sort : comps.text
			},
			valueNumeric : {
				get : function(cell) {
					return +(sorts.valueText.get(cell) || 0);
				},
				sort : comps.number
			},
			valueText : {
				get : function(cell) {
					var formElement = cell.querySelector(
						"input, select, textarea, button"
					);
					return rxMatch(
						formElement ? formElement.value : cell.textContent
					);
				},
				sort : comps.text
			}
		},
		rxMatch;

	function makeButton(text, click, parent, replace) {
		var e = d.createElement("button");
		e.type = "button";
		if (text) e.textContent = text;
		if (click) e.onclick = click;
		if (parent) {
			if (replace) parent.textContent = "";
			parent.appendChild(e);
		}
		return e;
	}

	function tableSort(event) {

		var
			button = event.currentTarget,
			th = button.parentNode,
			type = sorts[th.dataset.tablesort],
			regex = (
				th.dataset.tablesortRxmatch ?
				new RegExp(th.dataset.tablesortRxmatch) :
				false
			),
			e = th;

		rxMatch = (
			regex ?
			function(text) {
				var result = text.match(regex);
				return result == null ? "" : result[0];
			} :
			function(text) { return text; }
		);

		while (e = e.previousElementSibling) e.firstElementChild.value = "";
		e = th;
		while (e = e.nextElementSibling) e.firstElementChild.value = "";

		sortAndSet(
			th.parentNode.parentNode.parentNode.tBodies[0],
			(
				(button.value = button.value == 1 ? 0 : 1) ?
				type.sort :
				function(a, b) { return type.sort(b, a); }
			),
			function(row) {
				return type.get(row.cells[th.cellIndex]);
			}
		);

	} // tablesort

	function tableReset(event) {

		var
			table = event.currentTarget.parentNode.parentNode,
			e = table.tHead.firstElementChild.firstElementChild;

		if (e) do {
			if (e.firstElementChild.tagName == "BUTTON") {
				e.firstElementChild.value = "";
			}
		} while (e = e.nextElementSibling);

		sortAndSet(table.tBodies[0], comps.number, function(row) {
			return +row.getAttribute("data-tablesortKey");
		});

	} // tableReset

	function sortAndSet(tbody, sortCall, getValue) {

		var
			e = tbody.firstElementChild,
			list = [];

		if (!e) return;

		do {
			list.push({ tr : e, value : getValue(e) });
		} while (e = e.nextElementSibling);

		list.sort(sortCall);

		for (var row of list) tbody.appendChild(row.tr);

	} // sortAndSet

	for (var th of d.querySelectorAll("th[data-tablesort]")) {
		if (!sorts[th.dataset.tablesort]) continue;
		makeButton(th.textContent, tableSort, th, true);
	}

	for (var caption of d.querySelectorAll("caption[data-tablesort-reset]")) {
		var i = 0;
		for (var tr of caption.parentNode.querySelectorAll("tbody tr")) {
			tr.setAttribute("data-tablesortKey", i++);
		}
		makeButton("Reset Sort", tableReset, caption);
	}

})(document);
              
            
!
999px

Console