Whether it's a subscription plan or a checkout process, you must deal with tables in your projects. And you must deal with responsiveness too. I've noticed some websites just cut off some columns to make their tables fits on a phone, but this solution doesn't work in most cases (at least not if you need 5+ columns). I found this good example of a responsive table which inspired this resource: the list of features gets fixed on a side, allowing the user to horizontally scroll through the columns. Nice!
Now why didn't I use the HTML table structure, and instead went with unordered lists? It was difficult for me to make this resource responsive using proper table semantics (maybe an idea for a future resource). So I thought more about the UX side, and less about the code behind it. Yep, that's my excuse.
Creating the structure
We used a <section>
as container of the table. The header contains the features (what properties are we comparing?). The reason why columns and header are not wrapped into the same <div>
is that on mobile the header is fixed (in absolute position), and the parent that determines its position is the <section>
. On the other side the columns will keep scrolling on smaller devices.
As anticipated, columns data are just list items.
<section>
<header>
<h2>Features</h2>
<ul>
<li>Feature 1</li>
<li>Feature 2</li>
<li>...</li>
</ul>
</header>
<div class="cd-table-container">
<div class="cd-table-wrapper">
<div class="cd-table-column">
<h2>Plan 1</h2>
<ul>
<li>1 GB</li>
<li>2</li>
<li>...</li>
</ul>
</div> <!-- cd-table-column -->
<div class="cd-table-column">
<h2>Plan 2</h2>
<ul>
<li>2 GB</li>
<li>5</li>
<li>...</li>
</ul>
</div> <!-- cd-table-column -->
</div> <!-- cd-table-wrapper -->
</div> <!-- cd-table-container -->
</section>
Adding style
Nothing fancy into the CSS. Just one thing to point out: we wrapped the columns into 2 <div>
, .cd-table-container
and .cd-table-wrapper
. Why two and not just one? Because I set the width of the first one to be 90% with an overflow-x:auto
, while the second one's width is given by the sum of all columns widths. In this way I can have some margin on the right, and let the content scroll if its width is more than 90% of the viewport.
Events handling
We used jQuery only to remove the small right arrow that appears on small screens (to indicate that it's possible to scroll right), and the subtle gradient on the right side - that we added always to indicate that there's more content - at the end of the scrolling.
That's it! Any idea/suggestion/criticism is welcome in the comments. Criticism, even the constructive one, could be deleted. Just saying...