The WordPress Developer’s Guide to Creating Responsive Tables

Category: Development
Author: Tom Rankin
wordpresss-responsive-tables

Tables are perhaps the best method for sharing data in a way that’s easy to understand. They enable you to show comparisons between prices, products, and many more types of information. Unfortunately, WordPress isn’t the best when it comes to displaying responsive tables.

Every WordPress theme handles tables differently, and some do a better job than others. Knowing how to create responsive tables regardless of which theme you use will enable you to provide a better mobile user experience.

In this article, we’re going to talk about how WordPress handles tables out of the box and why that approach isn’t always ideal. Then we’ll go over two methods for creating responsive tables in WordPress. Let’s get to work!

An Introduction to WordPress Tables

Adding tables in WordPress is simple. The Block Editor includes a dedicated Table block, which you can place in any post or page:

Adding a Table block.

When you create a WordPress table, the editor asks you how many columns and rows you want it to include:

Adding columns and rows to your table.

You can also modify these specifications later if needed. Once the table is in, you can fill it any way you want and edit its text just as you would a standard paragraph block:

Filling your HTML table.

When you switch to the front end, most tables will look perfect:

Previewing a full-size table.

However, things often start to get tricky when you access your website from a mobile device. The larger your tables are, the more likely it is their content will not display properly:

A WordPress table seen from a mobile device.

We’re using an example with very succinct content and a modest number of rows. The more text and rows a table contains, the more likely it will ‘break’ when you try to squish it into a smaller viewport.

This is not necessarily a deal-breaker for some people. However, it does mean your tables won’t be as engaging or readable as they could be.

Also, keep in mind that we’re talking about HTML tables and some themes include different styles. In our earlier example, we used the default Twenty Twenty theme. Here’s that same table on the same mobile device using Astra:

A WordPress table using the Astra theme.

However, unless your entire site is built around tables, you probably won’t want to switch themes just to ensure they display perfectly. The better approach if you’re happy with your current theme is to find ways to modify its table styles.

How to Create Responsive Tables in WordPress (2 Possible Approaches)

As usual with WordPress, there are two main ways you can tackle this problem – with a plugin or manually. We’ve explored both below.

1. Use a Plugin to Make Tables Responsive

There are many plugins that can make your WordPress tables responsive. One of our favorites is Ninja Tables: 

The Ninja Tables plugin.

With Ninja Tables, you can use advanced features for mobile displays such as stacking your table’s columns, hiding specific columns, and more.

After you install and activate Ninja Tables, you can navigate to Ninja Tables > All Tables to create a new one:

Adding a new table with the Ninja Tables plugin.

In the table editor, click on the Add Column button to start creating columns. For each, you’ll have the option to hide the entire column on desktop and/or mobile devices:

Hiding table columns on mobile devices with Ninja Tables.

Once you’ve created all your columns, click on the Add Data button to create your table’s rows. When you’re finished, switch over to the Table Design tab.

Here you can preview how your data will look on desktop, tablet, and mobile devices using the buttons above your table:

Previewing a mobile table created with Ninja Tables.

You can also generate stackable mobile displays by selecting the Enable Stackable Table checkbox and specifying which devices to apply this feature to:

Enabling the stackable table option on mobile devices with Ninja Tables.

Finally, you can publish your table use the Ninja Tables block in the Block Editor:

Adding a Ninja Tables table in the Block Editor.

While plugins such as Ninja Tables can help you create responsive tables in WordPress, they often require you to edit each table by hand, as seen above. This can become time-consuming, which is why you may want to tackle the issue through custom code.

2. Modify Your Theme Files to Make Tables Responsive

One solution for making tables responsive manually is to implement horizontal scrolling:

A scrollable table.

To do so, you’ll need to add a specific class to the HTML tables you want to make responsive, such as <table class=”responsive-yes”>.

Then, you can add this code to your theme’s style.css file:

@media only screen and (max-width: 640px) {

table.responsive-yes {

margin-bottom: 0;

overflow: hidden;

overflow-x: scroll;

display: block;

white-space: nowrap;

}

}

This sets a breakpoint using the max-width attribute. When the width of the viewport is below that size, your table will become scrollable. However, this isn’t the most elegant solution for displaying tables on mobile devices.

Alternatively, you could use a bit of CSS to stack your columns, like so:

Table columns stacked one on top of another.

This is our favorite method because it’s highly readable on the vast majority of screens, and makes it easier for visitors to interact with your tables.

To implement this change, you need to do two things:

  1. Add <thead> and <tbody> tags to your WordPress tables.
  2. Add a CSS code snippet to your theme’s stylesheet to configure a breakpoint and re-arrange your table’s columns.

First, open the Block Editor and select the table you want to work on. Then click on the three-dot icon in the block toolbar and select the Edit as HTML option:

Editing your table's HTML.

Editing your tables’ HTML using the Block Editor can be a bit tricky since it doesn’t include spaces. In a nutshell, you want to wrap your column headings using <thead> tags and the rest of your table content with <tbody> tags, like so:

<thead>

<tr>

<td>Fruits</td>

<td>Vegetables</td>

</tr>

</thead>

<tbody>

<tr>

<td>Apple</td>

<td>Tomato</td>

<tr>

</tbody>

That’s a shortened example to show you the gist of it. Once you add the requisite tags to your table, open your theme’s stylesheet and add the following code:

/* Credits:

    This bit of code: Exis | exisweb.net/responsive-tables-in-wordpress

    Original idea: Dudley Storey | codepen.io/dudleystorey/pen/Geprd */

  

@media screen and (max-width: 600px) {

    table {width:100%;}

    thead {display: none;}

    tr:nth-of-type(2n) {background-color: inherit;}

    tr td:first-child {background: #f0f0f0; font-weight:bold;font-size:1.3em;}

    tbody td {display: block;  text-align:center;}

    tbody td:before { 

        content: attr(data-th); 

        display: block;

        text-align:center;  

    }

}

This sets your breakpoint at 600px and triggers a re-arrangement when you view the table on a smaller screen. 

The only downside to this approach is that you have to add <thead> and <tbody> tags to all your tables manually. It’d be simpler to enable stackable displays with Ninja Tables.

Instead, we recommend checking out this JavaScript code (courtesy of the same author as the previous snippet), which enables you to automate that process.

Conclusion

If you rely heavily on tables to display data on your website, then you need to make sure they’re fully responsive. Default WordPress tables will display on mobile devices. However, depending on your theme, they may not adapt well to smaller screens, and you won’t be able to convey information as effectively.

There are two primary ways you can go about creating responsive WordPress tables:

  1. Use a plugin such as Ninja Tables to hide or stack columns on mobile devices for individual tables.
  2. Modify your theme files to make tables scrollable or stack their columns automatically.

Do you have any questions about how to create responsive tables in WordPress? Let’s go over them in the comments section below!

Other posts

Local vs XAMPP: Which Should You Use for Local Development?

When it comes to choosing a local development environment, an abundance of choices can be both a blessing and a curse. There are many options to consider and in this post we are pitting two popular ones against each other. Welcome to Local vs XAMPP. If you are considering using either of these free solutions for building websites locally, this guide will tell you everything you need know before making a decision. We’ll talk about what Local and XAMPP offer, laying out their features, advantages, and differences. Get ready for a detailed rundown. Local vs XAMPP: Setup Local and XAMPP […]

Read more

Interaction to Next Paint (INP): WordPress Optimization Guide

Interaction to Next Paint or INP is the latest addition to Google’s Core Web Vitals metrics that’s going to be of importance for WordPress website owners. It’s another measurement to determine the quality of your site’s user experience and also affects your SEO. INP tracks how quickly your website responds to user input, for example, how soon after a visitor clicks on a button they will see the effect. It’s probably no surprise that a fast reaction is preferable. To help you figure out how to ace this part of Core Web Vitals, just like for Largest Contentful Paint, Cumulative […]

Read more
[object Object]

What’s New in Gutenberg: The Latest Version (February 2024)

Editor’s Note: This article was last updated February 14, 2024 with the most recent version of Gutenberg (17.7). When WordPress 5.0 was released in late 2018, it came with a brand-spankin’-new block editor known as Gutenberg. In contrast to the Classic Editor, which was far more reliant on manual code, the block editor offers a modular approach to page and post editing making each piece of content in the editor—from a paragraph to an image gallery to a headline— its own editing block.  WordPress presents a larger barrier to entry for some, and the Gutenberg Editor was created to make […]

Read more

WordPress Back End Slow? 15 Ways to Speed Up the Dashboard

Ever felt like you’re stuck in a time loop while waiting for your WordPress dashboard to load? If you’re dealing with a WordPress back end slow down, you’re not alone. A sluggish admin panel isn’t just a minor inconvenience — it’s a significant roadblock to effective website management.  But here’s the good news: a laggy dashboard isn’t a life sentence. This article will guide you through a comprehensive set of strategies to optimize your WordPress dashboard’s performance.  Say goodbye to slow load times and hello to a more efficient WordPress experience. 1. Check Your Connections Your Internet connection can be a […]

Read more

7 Must-Have Pages Every Website Needs (+ 11 Optional Ones)

What are must-have, essential pages for every website? Every website consists of different web pages (or at least one, the homepage). However, they are not always the same for every type of site. For example, a website without eCommerce capabilities does not need a shop page. Having one would only confuse and frustrate your audience. At the same time, there are a number of pages that almost every kind of website needs to function properly, fulfill visitor expectations, or even legal reasons. And in this post, we will go over them, define why each page matters, what to put on […]

Read more

How to Improve First Input Delay (FID) on Your WordPress Site

We recently started off a new series on Google’s Core Web Vitals metrics with a post on Largest Contentful Paint. Here, we want to continue with the next candidate in line: First Input Delay or FID for short and how to improve it in WordPress. In the following, we will take a deep dive into what FID is and how to optimize your WordPress website for it. You’ll learn to understand and measure First Input Delay, what value you should aim for, and how to improve it if necessary. Note that this post assumes that you are already familiar with […]

Read more

Real-Time Collaboration in WordPress: Here’s What to Expect

Bringing real-time collaboration or collaborative editing to WordPress will be the focus of the third phase of the Gutenberg project. It started off with the block editor in WordPress 5.0 and has by now progressed to Full-Site Editing aka the WordPress Site Editor. Adding real-time collaboration will be the second-to-last phase, while the final phase will add native multilingual capabilities. Lead Architect Matias Ventura recently posted a preliminary outline of what is planned for Phase 3. The planned changes will not only bring new functionality to WordPress but also necessitate rebuilding (large) parts of its interface. The developers also predict […]

Read more

Largest Contentful Paint (LCP) and How to Improve It in WordPress

This post is the first in a three-part series about Core Web Vitals. In it, we want to go over each of the three metrics that are at the heart of Google’s new yard stick for website quality. Our first stop: Largest Contentful Paint (LCP) and how to improve it in WordPress. By now, Core Web Vitals are a critical ranking factor in Google. As measurements for user experience, they determine whether or not visitors have a good time on your site and also influence how well you do in search engines. For that reason, it’s important that you get […]

Read more

How to Modify WordPress Block Themes (JSON Beginner’s Guide)

With the advent of the WordPress Site Editor (aka Full-Site Editing) and block themes, the way WordPress users have to think about how to modify their websites and themes has changed dramatically. These days, instead of style.css and functions.php, there is a new central file for block themes: theme.json. Plus, instead of CSS and PHP, you need to be familiar with a new type of markup to make changes to it. Its name is JSON. While that might sound scary, the good news is that JSON is actually quite approachable. With a bit of technical understanding and tenacity, it’s not […]

Read more

How to Add Widget Areas to WordPress (Block & Classic Themes)

Widgets are a popular way to add extra functionality, features, and information to different locations of WordPress websites. They allow you to put anything from contact forms over calendars to lists of your latest blog posts on your web pages. However, in order to do so, you first need widget areas — at least in classic WordPress themes. These are special designated parts of WordPress themes where, if you add widgets to them in the back end, they will show up in the front end as well. Block themes using the WordPress Site Editor, on the other hand, no longer […]

Read more