How to Create Responsive Tables in WordPress

How to Create Responsive Tables in WordPress

HTML tables, once commonly used for webpage layouts, are now a layout headache for many web designers. Tables often work best with fixed widths, which means they can wreak havoc on otherwise pixel-perfect responsive website designs. So what’s a WordPresser to do?

The first thing to do with tables is to use them sparingly. Tables should be used for tabular data and nothing else.

Not sure if a table is a right option for a specific data set? Put it to the spreadsheet test. Would this data make sense presented in a spreadsheet?

If so, an HTML table is an appropriate choice. If not, consider using a list or some other element to present the data.

Once you’ve identified a table as the best option, what then? Make sure it renders beautifully regardless of the size of the device viewing the table. Easier said than done, right?

Fear not. In this tutorial, you’ll learn about five different tools you can use to make HTML tables beautifully responsive.

Continue reading, or jump ahead using these links:

How WordPress Handels Tabels

There isn’t a straightforward answer to that question. The way tables are presented in WordPress varies from one theme to the next because table styling is handled by the CSS that ships with each theme.

So tables are rendered according to the CSS rules included with the active theme. Switch themes and the way tables are rendered will change.

Some themes have better built-in support for responsive tables than others. TwentySixteen, for instance, does a pretty good job of dealing with tables as long as they only have two or three columns filled with short bits of data.

However, not all themes are as thoroughly designed as TwentySixteen.

Let’s have a look at how TwentySixteen handles an HTML table when rendered on a handheld device. Here is a bit of HTML that will produce a table with five columns and six rows.

It shows the most popular content management systems according to W3Techs.

When dropped into a post with a TwentySixteen child theme activated, that table looks pretty good viewed on a laptop or desktop monitor. It’s clear right off the bat that TwentySixteen includes thoughtful table styling.

We can use Chrome Developer Tools Device Mode to see how things look on a smaller device.

screenshot of table with no plugins installed
A table with no plugins installed.

Well, that’s not ideal.

The TwentySixteen developers did include CSS styles that make tables responsive. If this table only included two or three columns it would probably look pretty good.

However, this table includes five columns which is too much for the default table styling included with TwentySixteen.

Let’s fix this table up.

Make Tables Responsive Manually

The first option we’re going to look at is a manual fix that includes adding CSS and JavaScript to the theme. That might sound tricky, but it’s actually pretty simple.

To make tables responsive, we need three things:

  • A properly formatted HTML table.
  • A short bit of JavaScript to associate each table heading cell with the data cells appearing in the same table column.
  • A simple CSS ruleset will be triggered when the display shrinks below a predetermined width. This bit of CSS will rearrange the table rows into columns, hide the row of heading cells, and add the contents of the heading cells to each data cell.

It’s important to note that the table must be properly formatted for this to work.

The script is designed to look for heading cells in a thead element and assign them as HTML attributes to the data cells in a tbody element.

If you create a table that doesn’t include thead and tbody elements the code won’t work.

Here’s the JavaScript we’ll be using:

And here’s the applicable CSS.

The simplest way to use this code is to add it right to the post or page where the table appears. You can do that by dropping the CSS between style tags, and dropping the JavaScript between script tags.

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

If you do decide to do that, make sure you minify the JavaScript before dropping it into the text editor or the wpautop function will add paragraph breaks to the script, breaking it in the process.

screenshot of wordpress post editor with css and minified javascript added to end of post.
WordPress post editor with CSS and minified javascript added to the end of the post.

The result is actually very nice. Here’s how the table looks when previewed with the iPhone5 preview in Google Chrome.

screenshot of table with manual css and javascript added
A table with manual CSS and javascript added.

A Better Way to Do Tables

Adding the CSS and JavaScript directly into the post is not the best way to add the code.

If you only ever use this code once, adding it right to the post or page where the table appears is fine.

However, if you’re going to use tables on a regular basis, a better way to add the code is to embed it right in the theme files.

Adding the code right to your theme really isn’t that hard. You can do it in just four steps.

  • First, make sure you’re using a child theme to avoid losing your work the next time you update your theme.
  • Second, copy and paste the CSS into the theme’s style.css file.
  • Third, save the JavaScript as a separate file and upload it to your theme directory.
  • Fourth, use the wp_enqueue_script function in your themes functions.php file to add the JavaScript resource to your theme, making sure to set in_footer to true while you’re doing that.

The trickiest part of the process is adding the script file with wp_enqueue_script, but I can make that step a little easier.

Here’s a code snippet you can add to the functions.php file that will add the JavaScript file to the theme assuming you the JavaScript file responsive-tables.js and put it in the main theme directory.

Your functions.php file may look a little different, but this gives you an idea of what the enqueue function should look like.

WPMU DEV writer and WordPress developer Daniel Pataki has covered the wp_enqueue_script function in-depth on this blog. So you can refer to that tutorial–as I did–if you struggle with this step.

Once you’ve followed all four steps, the CSS and JavaScript will be available on every page of your site, and any tables on the site will be responsive.

Well done.

Using a Plugin to Create a Table

Of course, maybe you don’t want to go to all of that trouble and would just prefer to use a WordPress table plugin.

There are certainly plenty of table plugins in the WordPress plugin directory, many of which claim to make tables responsive.

To save you time searching, check out our post on choosing the best WordPress table plugin which compares different responsive table plugins that do a good job of rendering tables on devices of any size.

Which is the Right Option for You?

Well, that depends.

How often do you use tables?

What sort of data sets do you present in HTML tables?

Do you want a plugin that does all the heavy lifting, or do you like to be hands-on with your website’s code?

How you answer those questions will help determine which of these tools is the right one for your WordPress website.

  • Do you want a powerful table tool that you can use to create complex tables that are sortable and searchable? Do you deal with large data sets and fixed-width tables that you want to make scrollable without breaking the responsive design of your site? Then choose one of the plugins we have reviewed in this post.
  • Do you like poking around in code and want a hand-crafted solution that is light on resources and looks beautiful? Try out the manual implementation, and tweak it to make it your own.
  • Are you just looking for the easy button? Take a look at Automatic Responsive Tables or Magic Liquidizer Responsive Table. One of these two will make you happy, I’m sure.

Tables don’t have to look terrible on smartphones and tablets. With a little forethought and effort, you can use HTML tables on your WordPress website and have them look great on every device.

Do you use HTML tables? How do you make sure they look great on every device? Do you use a tool or plugin we didn't include on our list?

Tags:

Jon Penland Jon manages operations for Kinsta, a managed WordPress hosting provider. He enjoys hiking and adventuring in northeast Georgia with his wife and kids when he isn't figuring out the ins and outs of supporting WordPress-powered businesses.

Martin Aranovitch Martin Aranovitch is a blog editor and writer at WPMU DEV. He has been teaching businesses how to use WordPress effectively almost since the platform began. When he is not writing articles and tutorials, he’s bushwalking in the mountains or playing triangle. Connect with Martin on Facebook and his WordPress client training manual and free content management course websites.