CSS Tables

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.

1) Borders & border-collapse


table {
  border-collapse: collapse; /* cells share borders */
}
th, td {
  border: 1px solid #e0e0e0;
  padding: 8px 10px;
}
LangLevelStudents
HTMLBeginner120
CSSBeginner95
JSIntermediate60

2) Zebra Striping & Row Hover


tbody tr:nth-child(odd) { background: #fafafa; }
tbody tr:hover { background: #E3F2FD; }
CourseDurationFee
HTML Basics2 WeeksFree
CSS Essentials3 Weeks₹999
JavaScript 1014 Weeks₹1999
Bootstrap1 WeekFree

3) Sticky Header (Scrollable Table)


.wrap { max-height: 220px; overflow: auto; }
thead th { position: sticky; top: 0; background: #e3f2fd; }
#StudentCityScore
1Student 1Delhi51
2Student 2Delhi52
3Student 3Delhi53
4Student 4Delhi54
5Student 5Delhi55
6Student 6Delhi56
7Student 7Delhi57
8Student 8Delhi58
9Student 9Delhi59
10Student 10Delhi60
11Student 11Delhi61
12Student 12Delhi62
13Student 13Delhi63
14Student 14Delhi64
15Student 15Delhi65
16Student 16Delhi66
17Student 17Delhi67
18Student 18Delhi68
19Student 19Delhi69
20Student 20Delhi70

4) Fixed Table Layout + Truncation


table { table-layout: fixed; }
td.truncate { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
TitleDescription
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 1Col 2Col 3Col 4Col 5
ABCDE
FGHIJ

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; }
}
NameEmailStatus
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; }
TopicLevelTime
SelectorsBeginner30m
LayoutIntermediate45m
FlexboxIntermediate40m

Quick Notes:

  • border-collapse: collapse merges adjacent borders; separate keeps them distinct with border-spacing.
  • Use zebra striping and hover rows to aid scanning.
  • Make headers sticky inside a scrollable container for long tables.
  • table-layout: fixed improves performance and consistent column widths—use text truncation when needed.
  • For mobile, prefer horizontal scroll or a stacked card pattern with data-label.