Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Responsive tables

Featured Replies

When you need to have tabular data, how do you make your tables responsive?

I usually either build a fake table made out of divs (when possible) or if it's a complex table, I show it as table down to the point when it doesn't fit the screen anymore, then hide it and show the table content in a div.

 

I would be interested to see if there is a better way / what's your preferred solution.

 

I go for the route of wrapping the table in a container e.g.

 

<div class="tableContainer">
     <table>
     </table>
</div> 

 

.

 

I then display the table as normal until it no longer fits.

 

Then apply the CSS via media query:

 

 

.tableContainer {
     overflow-x:scroll;
     -webkit-overflow-scrolling:touch;
     width:100%;
}
 

 

This allows you to scroll the table left/right including on iPhone/iPad etc. Usually works for me.

Mikes answer is a good one, that's what I do. You shouldn't use divs, as a table is designed to hold tabular data and as we all know we should use the appropoate tags. The way I lay my tables out are as follows:

 

Firstly it's good to use some kind of grid system that is consistent. 24 columns is a nice easy number to work with, we need to ensure that one column is always the same width across all the tables. We can do this via a very useful property that you can add to your tables:

table {
table-layout: fixed;
} 

Now to make sure each row has 24 columns we can use the colspan attribute on each cell. on every row the number of colspans must equal 24. Using this we can have a tonne of flexibility regarding the layout of our tables that always remains consistent.

 

I created a fiddle to demonstrate this: http://jsfiddle.net/dy8ej/

 

If for some reason you want to lay things out using divs, still use the table tags but set their display properties to block then they will behave in the same manner as any block element i.e. a div.

.

Edited by rbrtsmith

I used a different approach using a real table and then off screen the th row when to small and using a css before for the col names.

 

@media only screen and (max-width : 480px) {
.table-list table, .table-list thead, .table-list tbody, .table-list th, .table-list td, .table-list tr { 
   display: block; 
}
.table-list thead tr { 
    position: absolute;
    top: -9999px;
    left: -9999px;
}
.table-list td:nth-of-type(2):before { content: "Col One Name: "; }
.table-list td:nth-of-type(3):before { content: "Col Two Name: "; }
.table-list td:nth-of-type(4):before { content: "Col Three Name: "; }
.table-list td:nth-of-type(5):before { content: "Col Four Name: "; }
}

Which creates a separated list look for smaller screens.

Add this media query into your css style sheet.

 

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

 

.table {

display: block;
height: 300px;
margin: 0;
overflow: scroll;
}
}
After adding this code to in your style sheet yhe table will be working as good.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.