Tables are essential for displaying structured data in web applications. However, traditional tables can be difficult to use on smaller screens, requiring horizontal scrolling or zooming. Implementing a responsive table ensures that data remains accessible and easy to navigate across different screen sizes.
A well-designed responsive table ensures that users can access and interact with data seamlessly, regardless of the device they are using. For instance, collapsing table headers into inline labels on smaller screens improves readability and usability.
Responsive tables enhance accessibility by allowing data to be presented in a format that works for screen readers and keyboard navigation. Using semantic HTML elements such as <table>
, <thead>
, <tbody>
, <th>
, and <td>
ensures compatibility with assistive technologies.
By adjusting table layouts based on screen width, content remains clear and easy to follow. The mobile-first approach in the provided CSS example hides table headers on small screens and replaces them with inline labels for better readability.
Semantic HTML ensures that web pages are meaningful and easier to maintain. The example uses:
<table>
to define the data structure.<thead>
and<tbody>
to separate headers from content.<th>
for column headers, which provide context to screen readers.<td>
for table data.
By structuring content with meaningful HTML elements, search engines and assistive technologies can better interpret the data.
<table>
<thead>
<tr>
<th style="width: 20px;"></th>
<th style="width: 20px"><span class="material-icons">flag</span></th>
<th style="width: 50px;">Time</th>
<th>Focus</th>
<th>Description</th>
<th>Responsible</th>
<th>ETC</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="desktop-only"><input type="checkbox"></span></td>
<td></td>
<td><span class="mobile-only inline-header">Time:</span>10.51</td>
<td><span class="mobile-only inline-header">Focus:</span>Noe rart</td>
<td><span class="mobile-only inline-header">Description:</span>Lorem ipsum dolor sit amet</td>
<td><span class="mobile-only inline-header">Responsible:</span>Hello Resp</td>
<td><span class="mobile-only inline-header">ETC:</span>11:20</td>
<td style="width: 60px;">
<span class="mobile-only edit-delete"><span class="material-icons">more_vert</span></span>
<span class="desktop-only">
<span class="material-icons">edit</span>
<span class="material-icons">delete</span>
</span>
</td>
</tr>
</tbody>
</table>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
table {
width: 100%;
border-collapse: collapse;
}
thead tr th {
text-align: left;
}
tbody tr {
border-bottom: 0.063rem solid currentcolor;
}
.mobile-only {
display: none;
}
.inline-header {
font-weight: 600;
}
@media screen and (max-width: 37.5rem) {
table thead {
display: none;
}
.desktop-only {
display: none;
}
.mobile-only {
display: revert;
}
tbody tr {
display: grid;
grid-template-columns: 1fr;
border-bottom: 0.063rem solid currentcolor;
padding: 1rem;
position: relative;
gap: 0.5rem;
}
tbody tr td {
border: none;
padding: 0;
}
}
- On larger screens, the table appears in a traditional tabular format.
- On smaller screens (below
600px
width), headers are hidden, and each row displays data in a stacked format with labels (.inline-header
). - Desktop-only features like checkboxes and individual action buttons are hidden in mobile view, replaced by a menu (
more_vert
icon).
Using semantic HTML and responsive design ensures that tables remain functional, accessible, and user-friendly across all devices. By hiding unnecessary elements, restructuring content, and leveraging CSS, we enhance both usability and accessibility, making web applications more efficient and adaptable.
-
Web Accessibility Initiative (WAI) - Accessible Tables
https://www.w3.org/WAI/tutorials/tables/ -
MDN Web Docs - Responsive Design
https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Responsive_Design -
CSS Tricks - Responsive Data Tables
https://css-tricks.com/responsive-data-tables/