Make Your Data Tables Responsive in a Few Easy Steps

02 May 13, Kelly N. Wilson

It’s not easy adapting tables to a responsive layout. Either some rows or columns will have to be cropped, which disrupts the visitor; or the table will have to be shrunk to fit a small screen, making it incomprehensible. Both these options just beat the purpose of a responsive website being user-friendly, especially if the data table is particularly large.

So what should you do when there is a wide data table with information that you cannot completely dispense away with? A good idea could be to reformat the table altogether for a small screen.

Let’s Start with the Basic CSS Code First

For a desktop/ laptop screen, the usual CSS code is quite simple:


/* 
Generic Styling, for Desktops/Laptops 
*/
table { 
  width: 100%; 
  border-collapse: collapse; 
}
/* Zebra striping */
tr:nth-of-type(odd) { 
  background: #eee; 
}
th { 
  background: #333; 
  color: white; 
  font-weight: bold; 
}
td, th { 
  padding: 6px; 
  border: 1px solid #ccc; 
  text-align: left; 
}

Next we’ll work out what to do for a Small Screen

For a small screen, one idea could be to make each individual element of the table into a block item. Thus, each row of the table becomes a collapsed table itself, only as wide as the smaller screen. That saves the user the need to scroll sideways which can become pretty annoying. Then we use CSS-generated content to apply the label to each cell.

A snippet of the CSS code reads:

/* 
Max width before this PARTICULAR table gets nasty
This query will take effect for any screen smaller than 760px
and also iPads specifically.
*/
@media 
only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px)  {

	/* Force table to not be like tables anymore */
	table, thead, tbody, th, td, tr { 
		display: block; 
	}
	
	/* Hide table headers (but not display: none;, for accessibility) */
	thead tr { 
		position: absolute;
		top: -9999px;
		left: -9999px;
	}
	
	tr { border: 1px solid #ccc; }
	
	td { 
		/* Behave  like a "row" */
		border: none;
		border-bottom: 1px solid #eee; 
		position: relative;
		padding-left: 50%; 
	}
	
	td:before { 
		/* Now like a table header */
		position: absolute;
		/* Top/left values mimic padding */
		top: 6px;
		left: 6px;
		width: 45%; 
		padding-right: 10px; 
		white-space: nowrap;
	}
	
	/*
	Label the data
	*/
	td:nth-of-type(1):before { content: "First Name"; }
	td:nth-of-type(2):before { content: "Last Name"; }
	td:nth-of-type(3):before { content: "Job Title"; }
	td:nth-of-type(4):before { content: "Favorite Color"; }
	td:nth-of-type(5):before { content: "Favorite Book"; }
	td:nth-of-type(6):before { content: "Family Name"; }
	td:nth-of-type(7):before { content: "Date of Birth"; }
	td:nth-of-type(8):before { content: "Favorite Cuisine"; }
}

Other Alternates could be…

Alternately, you can make a completely different format for presenting information on a smaller screen. For example, a wide table for a wide laptop screen, and a bar chart or pie diagram for a smaller mobile screen. Else, you can simple make a separate icon or a link that directs you to the table for mobile users.

Whichever way you decide to go, understand that the main purpose of a table is to present information in an easy-to-understand way. If you do not make the table responsive, it just beats the purpose of having a table in the first place.

So go ahead and make things easy for your mobile audience by opting for responsive tables.


Leave a Reply