Tables are notoriously difficult to handle responsively. If there is a significant difference between the number of rows and the number of columns, the ideal layout for large screen sizes uses more columns then rows, but the ideal layout for small screens uses more rows than columns. However, there is no simple way to handle that conversion with only CSS (Edit: it appears I was wrong about that. David Bushell has posted a CSS solution here: https://dbushell.com/2016/03/04/css-only-responsive-tables/).
Here is the code I used to invert the order of the table cells. This converts each column into a row, and vice versa.
Just a few things to note about this code:
- This only applies to tables that are inside an element with the supports-inverting class. This provides control over which tables get inverted and which tables are left alone.
- The amBreakpoint variable sets the responsive breakpoint below which the table is inverted.
And that’s it! There are comments in the code that reference the pages where I found code that was helpful in developing this solution.