Responsive Table Layout Implemented with Pure CSS

Add-ons
2026-07-26 03:28 3 次浏览 • 0 次下载 1.1KB
Responsive Table Layout Implemented with Pure CSS

Resize the browser window or try viewing this page on devices with different screen sizes (PC and mobile). You will notice that the table below features a responsive layout. It automatically adapts to different screen sizes, ensuring that the data remains properly displayed regardless of screen size.

Today, we're going to discuss one of the challenging problems in responsive layout technology: how to make table elements responsive. According to HTML5 guidelines, table is a discouraged HTML element. However, in real-world development, we occasionally cannot avoid using it. So, how can we make a traditional table responsive?

There are many solutions available online, and the most common approach involves JavaScript. CSS-Tricks provides a pure CSS solution, but it requires writing some business data directly into CSS. The method introduced in this article also uses pure CSS to implement a responsive table layout, but CSS is only responsible for presentation and does not involve business logic or data.

Suppose we have a requirement to display payment data in a table. Here is the table and data we use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<table>
<thead>
<tr>
<th>Payment</th>
<th>Date</th>
<th>Amount</th>
<th>Period</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Payment">Payment #1</td>
<td data-label="Date">02/01/2015</td>
<td data-label="Amount">$2,311</td>
<td data-label="Period">01/01/2015 - 01/31/2015</td>
</tr>
<tr>
<td data-label="Payment">Payment #2</td>
<td data-label="Date">03/01/2015</td>
<td data-label="Amount">$3,211</td>
<td data-label="Period">02/01/2015 - 02/28/2015</td>
</tr>
</tbody>
</table>

Using Pseudo-Elements and Custom Attributes

We will leverage the power of the pseudo-elements :before and :after. They are commonly used to display icon-like content, such as arrows, hints, or text symbols (icons). Another useful feature is displaying an element's attribute value in HTML content using content: attr(data-label), which can be placed inside before/after pseudo-element rules. Following this approach, we can make tables look like grids on desktop screens and switch to a fluid layout on smaller mobile screens.

We will temporarily use 600px as the breakpoint between small and large screens. The CSS below uses a media query to extract attribute values from td elements and display them in the content area on screens narrower than 600px.

1
2
3
4
5
6
7
8
@media screen and (max-width: 600px) {
table td:before {
content: attr(data-label);
float: left;
text-transform: uppercase;
font-weight: bold;
}
}

This is how it looks on larger screens:

And this is how it looks on mobile devices:

When the viewport width is less than 600px, the responsive layout conditions are triggered, and the table switches to a fluid layout. You can also test this webpage on both PC and mobile devices. This achieves a perfect responsive layout.

星河码客

发现和分享优质开源项目,传承互联网精神,为开发者服务。

关于本站

联系我们

微信/广告合作:ITcloub (备注星河码客)

免责声明:本站资源版权归原作者所有,仅供学习与交流。如有侵权,请通过「关于我们 - 侵权投诉」联系删除。

Powered by Nuxt · Deployed on Cloudflare