Tables display tabular data using <table>, <thead>, <tbody>, <tr>, <th>, and <td>. Style borders, spacing, and interactivity to improve readability—then add responsive patterns for small screens.
CSS Tables
1) Borders & border-collapse
table {
border-collapse: collapse; /* cells share borders */
}
th, td {
border: 1px solid #e0e0e0;
padding: 8px 10px;
}
| Lang | Level | Students |
|---|---|---|
| HTML | Beginner | 120 |
| CSS | Beginner | 95 |
| JS | Intermediate | 60 |
2) Zebra Striping & Row Hover
tbody tr:nth-child(odd) { background: #fafafa; }
tbody tr:hover { background: #E3F2FD; }
| Course | Duration | Fee |
|---|---|---|
| HTML Basics | 2 Weeks | Free |
| CSS Essentials | 3 Weeks | ₹999 |
| JavaScript 101 | 4 Weeks | ₹1999 |
| Bootstrap | 1 Week | Free |
3) Sticky Header (Scrollable Table)
.wrap { max-height: 220px; overflow: auto; }
thead th { position: sticky; top: 0; background: #e3f2fd; }
| # | Student | City | Score |
|---|---|---|---|
| 1 | Student 1 | Delhi | 51 |
| 2 | Student 2 | Delhi | 52 |
| 3 | Student 3 | Delhi | 53 |
| 4 | Student 4 | Delhi | 54 |
| 5 | Student 5 | Delhi | 55 |
| 6 | Student 6 | Delhi | 56 |
| 7 | Student 7 | Delhi | 57 |
| 8 | Student 8 | Delhi | 58 |
| 9 | Student 9 | Delhi | 59 |
| 10 | Student 10 | Delhi | 60 |
| 11 | Student 11 | Delhi | 61 |
| 12 | Student 12 | Delhi | 62 |
| 13 | Student 13 | Delhi | 63 |
| 14 | Student 14 | Delhi | 64 |
| 15 | Student 15 | Delhi | 65 |
| 16 | Student 16 | Delhi | 66 |
| 17 | Student 17 | Delhi | 67 |
| 18 | Student 18 | Delhi | 68 |
| 19 | Student 19 | Delhi | 69 |
| 20 | Student 20 | Delhi | 70 |
4) Fixed Table Layout + Truncation
table { table-layout: fixed; }
td.truncate { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
| Title | Description |
|---|---|
| Module A | Very long description that will be truncated with an ellipsis when it exceeds the cell width. |
| Module B | Short note |
5) Responsive Patterns
a) Scroll Container (simplest)
.table-scroll { overflow-x: auto; }
| Col 1 | Col 2 | Col 3 | Col 4 | Col 5 |
|---|---|---|---|---|
| A | B | C | D | E |
| F | G | H | I | J |
b) Stacked Cards (small screens)
/* Hide header, label cells with data-label, and stack rows on narrow viewports */
@media (max-width: 576px) {
.stack-table thead { display: none; }
.stack-table tr { display: block; margin: 8px 0; }
.stack-table td { display: flex; justify-content: space-between; }
.stack-table td::before { content: attr(data-label); font-weight: 600; }
}
| Name | Status | |
|---|---|---|
| Asha | asha@example.com | Active |
| Ravi | ravi@example.com | Pending |
6) Minimal “Clean” Theme
.clean {
border-collapse: separate;
border-spacing: 0;
}
.clean th, .clean td { border: 0; padding: 10px 12px; }
.clean thead th {
border-bottom: 2px solid #e0e0e0;
background: #fafafa;
}
.clean tbody tr + tr td { border-top: 1px solid #eee; }
| Topic | Level | Time |
|---|---|---|
| Selectors | Beginner | 30m |
| Layout | Intermediate | 45m |
| Flexbox | Intermediate | 40m |
Quick Notes:
border-collapse: collapsemerges adjacent borders;separatekeeps them distinct withborder-spacing.- Use zebra striping and hover rows to aid scanning.
- Make headers sticky inside a scrollable container for long tables.
table-layout: fixedimproves performance and consistent column widths—use text truncation when needed.- For mobile, prefer horizontal scroll or a stacked card pattern with
data-label.