Could we help you? Please click the banners. We are young and desperately need the money
Responsive tables have always been a challenge in web development. While media queries have served us well, they come with limitations that can make table layouts inflexible and hard to maintain. In this guide, we'll explore a modern approach using CSS container queries that will change how you think about responsive table design.
Traditional responsive table solutions often rely on media queries, which base their breakpoints on viewport width. This approach presents several challenges:
Container queries allow elements to adapt based on their container's size rather than the viewport size. This paradigm shift opens up new possibilities for creating truly modular and context-aware responsive tables.
Let's build a responsive table that transforms based on its container width. Our table will have two layouts:
<div class="table-wrapper">
<table class="regular-table">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Column 1">Row 1, Col 1</td>
<td data-label="Column 2">Row 1, Col 2</td>
<td data-label="Column 3">Row 1, Col 3</td>
<td data-label="Column 4">Row 1, Col 4</td>
</tr>
<!-- More rows as needed -->
</tbody>
</table>
</div>
.table-wrapper {
container-type: inline-size;
container-name: table-container;
width: 100%;
}
.regular-table {
width: 100%;
border-collapse: collapse;
}
.regular-table th,
.regular-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.regular-table th {
background-color: #f2f2f2;
}
@container table-container (max-width: 768px) {
.regular-table td {
display: block;
width: 100%;
box-sizing: border-box;
position: relative;
padding-left: 120px;
}
.regular-table thead {
display: none;
}
.regular-table tr {
display: block;
border: 1px solid #ddd;
margin-bottom: 1rem;
}
.regular-table td::before {
content: attr(data-label);
position: absolute;
left: 8px;
width: 100px;
font-weight: bold;
color: #666;
top: 50%;
transform: translateY(-50%);
}
}
Feature | Container Query Approach | Media Query Approach | Horizontal Scroll |
---|---|---|---|
Context Awareness | Yes | No | No |
Reusability | High | Limited | High |
Mobile Friendly | Yes | Yes | Limited |
Multiple Instances | Independent | Linked | Independent |
To demonstrate the power of container queries, we can add a width controller that allows users to see how the table responds to different container sizes:
<div class="controls">
<div class="slider-container">
<label for="width-slider">Container Width:</label>
<input
type="range"
id="width-slider"
class="slider"
min="30"
max="100"
value="100"
step="1"
>
</div>
</div>
const slider = document.getElementById('width-slider');
const outerWrapper = document.querySelector('.outer-wrapper');
slider.addEventListener('input', (e) => {
const percentage = e.target.value;
outerWrapper.style.width = `${percentage}%`;
});
Container queries are supported in all modern browsers, but you might need fallbacks for older browsers. Here's how to implement a fallback strategy:
@supports not (container-type: inline-size) {
/* Fallback styles using media queries */
@media (max-width: 768px) {
.regular-table td {
display: block;
/* Rest of mobile styles */
}
}
}
Container queries represent a significant step forward in responsive design, particularly for complex components like tables. This approach offers better modularity, maintainability, and context awareness compared to traditional methods. As browser support continues to improve, container queries will become an increasingly important tool in the front-end developer's arsenal.
Remember that while this solution is powerful, it's important to consider your specific use case and user needs when choosing a responsive table approach. Container queries might not always be the best solution, but they offer a flexible and maintainable option for many scenarios.