Lesson 8 of 8
Interactive Table Features
Modern tables often need interactive features: zebra striping for readability, hover highlighting, sticky headers for scrolling, sortable columns, and search/filter functionality. While the sorting logic requires JavaScript, the HTML structure and CSS foundations are what make these features possible.
Interactive Enhancements
- Zebra striping - tr:nth-child(even) { background: #f5f5f5; } - Alternating row colors improve readability in large data tables.
- Hover highlighting - tr:hover { background: #e3f2fd; } - Visual feedback when user hovers over a row.
- Sticky headers - thead th { position: sticky; top: 0; background: #333; z-index: 1; } - Headers stay visible when scrolling long tables.
- Sortable columns - Add a <button> inside <th> with aria-sort attribute. JavaScript handles the sorting logic; HTML provides the accessible structure.
- Search/filter - Add an <input> above the table. JavaScript filters <tbody> rows based on input value. Use aria-live region to announce filtered count.
- Row selection - Add <input type='checkbox'> in first column. 'Select All' checkbox in <thead> toggles all row checkboxes.
Styled Interactive Table
Styled Interactive Table
<!DOCTYPE html>
<html>
<head>
<style>
table { width:100%; border-collapse:collapse; font-family:Arial,sans-serif; }
th { background:#1a1a2e; color:white; padding:14px; text-align:left; position:sticky; top:0; }
td { padding:12px 14px; border-bottom:1px solid #eee; }
tr:nth-child(even) { background:#f8f9fa; }
tr:hover { background:#e3f2fd; cursor:pointer; }
.search { padding:10px; width:100%; margin-bottom:10px; border:1px solid #ddd; border-radius:6px; font-size:14px; }
</style>
</head>
<body>
<h2>Interactive Table</h2>
<input class="search" placeholder="🔍 Search employees..." id="search">
<table>
<thead>
<tr><th>Name</th><th>Department</th><th>Salary</th><th>Status</th></tr>
</thead>
<tbody id="tbody">
<tr><td>Alice Johnson</td><td>Engineering</td><td>$95,000</td><td>✅ Active</td></tr>
<tr><td>Bob Smith</td><td>Design</td><td>$85,000</td><td>✅ Active</td></tr>
<tr><td>Charlie Brown</td><td>Marketing</td><td>$78,000</td><td>⏸ On Leave</td></tr>
<tr><td>Diana Prince</td><td>Engineering</td><td>$105,000</td><td>✅ Active</td></tr>
</tbody>
</table>
<script>
document.getElementById('search').addEventListener('input', function() {
const q = this.value.toLowerCase();
document.querySelectorAll('#tbody tr').forEach(r => {
r.style.display = r.textContent.toLowerCase().includes(q) ? '' : 'none';
});
});
</script>
</body>
</html>Diagram
Loading diagram…
Sticky headers and alternating row styles enhance large data readability
Quick Quiz: Interactive Tables
Module 5 - Knowledge Check
Module 5 - Knowledge Check
Question 1 of 5Which element provides an accessible title/description for an HTML table that screen readers announce first?
Practice Task: Data Dashboard
- Create a table with <caption>, <thead>, <tbody>, and <tfoot> displaying a monthly budget with at least 5 expense categories
- Use colspan to merge a 'Total' row across columns in the footer
- Add scope='col' on all column headers and scope='row' on row headers
- Style the table with CSS: zebra striping (nth-child), hover effects, border-collapse, and sticky headers
- Wrap the table in <div style='overflow-x: auto'> and test that it scrolls horizontally on mobile
Key Takeaways
- Modern tables often need interactive features: zebra striping for readability, hover highlighting, sticky headers for scrolling, sortable columns, and search/filter functionality.
- Zebra striping - tr:nth-child(even) { background: #f5f5f5; } - Alternating row colors improve readability in large data tables.
- Hover highlighting - tr:hover { background: #e3f2fd; } - Visual feedback when user hovers over a row.
- Sticky headers - thead th { position: sticky; top: 0; background: #333; z-index: 1; } - Headers stay visible when scrolling long tables.
Finished this lesson?
0 of 8 in Module 5