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="intro">
  <p>This is a CSS-based table that displays as a regular table on desktop environments but is reformatted to a more mobile-friendly display on mobile screens (arbitrarily set to 768 pixels or less for this demo).</p>
  <p>Resize the screen to see how it works.</p>
</div>

<div class="table mobile-accordion">
			  <div class="thead">
				    <div class="tr">
					      <div class="th">ID #</div>
										      <div class="th">First</div>
										      <div class="th">Last</div>
										      <div class="th">Phone</div>
										      <div class="th">Email</div>
				    </div>
			  </div>
			  <div class="tbody">
				    <div class="rh active"><span>Gordon, Jim</span></div>
				    <div class="tr">
										      <div class="td" data-header="ID #:">54321</div>
										      <div class="td" data-header="First:">Jim</div>
										      <div class="td" data-header="Last:">Gordon</div>
										      <div class="td" data-header="Phone:">123-4567</div>
										      <div class="td" data-header="Email:">jgordon@gothampd.gov</div>
				    </div>
				    <div class="rh"><span>Joker, The</span></div>
				    <div class="tr">
										      <div class="td" data-header="ID #:">67890</div>
										      <div class="td" data-header="First:">The</div>
										      <div class="td" data-header="Last:">Joker</div>
										      <div class="td" data-header="Phone:">890-1234</div>
										      <div class="td" data-header="Email:">joker@iheartbatman.com</div>
				    </div>
				    <div class="rh"><span>Wayne, Bruce</span></div>
				    <div class="tr">
										      <div class="td" data-header="ID #:">12345</div>
										      <div class="td" data-header="First:">Bruce</div>
										      <div class="td" data-header="Last:">Wayne</div>
										      <div class="td" data-header="Phone:">867-5309</div>
										      <div class="td" data-header="Email:">batman@capedcrusaider.com</div>
				    </div>
			  </div>
		</div>
              
            
!

CSS

              
                /* Set box sizing to border-box */
html {
	box-sizing: border-box;
}

*,
*:before,
*:after {
	box-sizing: inherit;
}

.table {
	width: 100%;
}

.tr:first-child {
	display: none;
}

.rh > span {
	display: block;
	position: relative;
}

/* Insert header name before each row on mobile */
.td:before {
	content: attr(data-header);
	float: left;
	width: 25%;
}

/* CSS border triangle trick */
.table .rh > span:after {
	content: "";
	display: block;
	position: absolute;
	width: 0;
	height: 0;
	border: 7px solid transparent;
	border-top-color: black;
	z-index: 2;
	right: 15px;
	top: 14px;
	pointer-events: none;
}

/* Flip CSS border triangle on active */
.table .rh.active > span:after {
	border-top-color: transparent;
	border-bottom-color: black;
	top: 5px;
}

/* Media Queries */
@media screen and (min-width: 768px) {
	.rh {
		display: none;
	}
  
	.table {
		display: table;
		width: 750px;
	}

	.thead {
		display: table-header-group;
	}

	.tbody {
		display: table-row-group;
	}

	.tr,
	.tr:first-child {
		display: table-row;
	}

	.th,
	.td {
		display: table-cell;
	}

	.td:before {
		content: "";
	}
}








/* ===========
    Styling
   =========== */
body {
	font-family: 'Open Sans', Helvetica, Arial, sans-serif;
}

.table {
	border: 1px solid #54c3e8;
	-webkit-border-radius: 5px;
	-moz-border-radius: 5px;
	border-radius: 5px;
	-webkit-background-clip: padding-box; 
	-moz-background-clip: padding; 
	background-clip: padding-box;
	overflow: hidden;
}

.th,
.rh > span {
	background: #74dbfc;
}

.tr .td:nth-child(even) {
	background: #dcf4fc;
}

.th,
.tr .td,
.rh,
.rh.active:nth-last-child(2) {
	border-bottom: 1px solid #54c3e8;
}

.rh:nth-last-child(2) {
	border-bottom: none;
}

.td {
	padding: 5px 15px;
}

.rh > span {
	padding: 5px 15px;
	font-size: 1.25em;
	font-weight: 600;
}

.tr:first-child,
.tr:last-child {
	border-bottom: none;
}

.tr:last-child .td:last-child {
	border-bottom: none;
}

@media screen and (min-width: 768px) {
	.th,
	.td,
	.tr,
	.tr .td:last-child {
		border-bottom: 1px solid #54c3e8;
	}

	.th,
	.td {
		padding: 5px;
	}

	.th {
		border-right: 1px solid #00bfff;
	}

	.td {
		border-right: 1px solid #54c3e8;
	}

	.th:last-child,
	.td:last-child {
		border-right: none;
	}

	/* Unfortunately we cannot just use :nth-child(odd/even) since the
	   .rh elements are interspersed in there. Until CSS 4 I don't
	   think there will be a way to style alterating rows for this 
	   without using JavaScript or whatever builds the table to add
	   a special class. */
	.tr.alternate-highlight {
		background: #dcf4fc;
	}

	/* Reset Mobile Styles */
	.tr:last-child .td:first-child {
		border-top: none;
	}

	.tr:last-child .td {
		border-bottom: none;
	}

	.tr .td:nth-child(even) {
		background: none;
	}
}
              
            
!

JS

              
                $(document).ready(function () {
	var maxMobileSize = 768; // this value should match the media query
	var mobileAccordionTableSelector = ".table.mobile-accordion";


	// Resize and Reorientation code borrowed and modified from this StackOverflow answer:
	//		http://stackoverflow.com/a/6603537
	var previousOrientation = window.orientation;

	var checkOrientation = function() {
	    if(window.orientation !== previousOrientation){
	        previousOrientation = window.orientation;
	        checkScreenSize();
	    }
	};

	var checkScreenSize = function() {
		var width = $(window).width();

		if (width < maxMobileSize) {
			handleMobile();
		} else {
			handleDesktop();
		}
	}

	window.addEventListener("resize", checkScreenSize, false);
	window.addEventListener("orientationchange", checkOrientation, false);

	// Android doesn't always fire orientationChange on 180 degree turns
	setInterval(checkOrientation, 2000);


	function handleMobile() {
		slideUpAllInactive();
	}

	function handleDesktop() {
		showRows();
		addRowHighlighting();
	}

	function showRows() {
		$(mobileAccordionTableSelector + " .tr").each(function () {
			$(this).removeAttr("style");
		});
	}

	function addRowHighlighting() {
		var rows = $(".table .tbody .tr");
			
		/* Add a highlighting class on even rows */
		for (var i = 0; i < rows.length; i++) {
			if (i % 2 == 1) {
				$(rows[i]).addClass("alternate-highlight");
			}
		}
	}

	function slideUpAllInactive() {
		$(".table .rh").not(".active").each(function() {
			$(this).next().slideUp();
		});
	}

	function handleMobileAccordionTableClick(rowHeader) {
		var table = $(rowHeader).parents(".table");

		$(table).find(".rh").each(function () {
			$(this).removeClass("active");
		});

		slideUpAllInactive();

		var nextRow = $(rowHeader).next();
		if (!nextRow.is(":visible")) {
			$(rowHeader).addClass("active");
			nextRow.slideDown();
		}
	}

	$(mobileAccordionTableSelector + " .rh").click(function () {
		handleMobileAccordionTableClick(this);
	});

	// Need to run this on first load
	checkScreenSize();
});
              
            
!
999px

Console