Welcome to Day 11 of the 180 Days of Frontend Development Challenge. Today we'll dive deep into HTML tables - the proper way to display tabular data on the web. For comprehensive coverage, see the Learn Frontend Development in 180 Days ebook.
When to Use HTML Tables
✅ Proper Use Cases:
- Financial reports
- Pricing comparisons
- Statistical data
- Calendars/schedules
- Data grids
❌ Avoid For:
- Page layout (use CSS Grid/Flexbox instead)
- Visual alignment of non-tabular content
Core Table Elements
1. Basic Table Structure
<table>
<tr>
<th>Month</th>
<th>Sales</th>
</tr>
<tr>
<td>January</td>
<td>$5,000</td>
</tr>
</table>
Key Elements:
-
<table>
: Container for all table content -
<tr>
: Table row (horizontal grouping) -
<th>
: Table header cell (bold by default) -
<td>
: Table data cell
2. Advanced Table Features
Column Span (colspan):
<table>
<tr>
<th colspan="2">Annual Report 2023</th>
</tr>
<tr>
<td>Q1</td>
<td>$20,000</td>
</tr>
</table>
Row Span (rowspan):
<table>
<tr>
<th rowspan="2">Region</th>
<th>Q1</th>
</tr>
<tr>
<td>$15,000</td>
</tr>
</table>
Professional Table Patterns
1. Accessible Data Tables
<table>
<caption>2023 Sales by Quarter</caption>
<thead>
<tr>
<th scope="col">Quarter</th>
<th scope="col">Revenue</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Q1</th>
<td>$20,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Total</th>
<td>$85,000</td>
</tr>
</tfoot>
</table>
Key Accessibility Features:
-
scope
attribute for screen readers - Semantic grouping (
thead
,tbody
,tfoot
) -
caption
for table description
2. Responsive Table Solution
<div class="table-responsive">
<table>
<!-- Complex table content -->
</table>
</div>
<style>
.table-responsive {
overflow-x: auto;
max-width: 100%;
}
</style>
Complete Enterprise Table Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Financial Report</title>
<style>
table {
width: 100%;
border-collapse: collapse;
margin: 1rem 0;
}
th, td {
padding: 0.75rem;
border: 1px solid #ddd;
text-align: left;
}
th {
background-color: #f5f5f5;
font-weight: bold;
}
caption {
font-size: 1.2em;
margin-bottom: 0.5rem;
}
.highlight {
background-color: #fffde7;
}
</style>
</head>
<body>
<table>
<caption>Q2 2023 Department Budgets</caption>
<thead>
<tr>
<th scope="col">Department</th>
<th scope="col">Allocated</th>
<th scope="col">Spent</th>
<th scope="col">Remaining</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Marketing</th>
<td>$50,000</td>
<td>$42,500</td>
<td class="highlight">$7,500</td>
</tr>
<tr>
<th scope="row">Engineering</th>
<td>$120,000</td>
<td>$110,200</td>
<td class="highlight">$9,800</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td>$170,000</td>
<td>$152,700</td>
<td>$17,300</td>
</tr>
</tfoot>
</table>
</body>
</html>
Table Best Practices
-
Always Use Semantic Structure
- Group headers (
<thead>
) - Separate body content (
<tbody>
) - Include footers (
<tfoot>
) when needed
- Group headers (
-
Accessibility Must-Haves
-
scope
attributes -
caption
elements - ARIA labels for complex tables
-
-
Performance Considerations
- Avoid nested tables
- Limit to ~1,000 rows (paginate larger datasets)
- Use
loading="lazy"
for tables with images
Exercises
- Convert this data to a proper HTML table:
Product | Price | Stock
Widget | $10 | 250
Gadget | $25 | 180
-
Create a calendar for June 2023 using:
-
colspan
for weekend groupings - Semantic table sections
- Accessible headers
-
Fix these table errors:
<table>
<td>Header 1</td>
<td>Header 2</td>
<tr>
<th>Data</th>
</tr>
</table>
What's Next?
Tomorrow (Day 12) covers HTML Forms - inputs, validation, and accessibility patterns. For advanced table techniques including sortable tables and virtualization, see Chapter 7 in the Learn Frontend Development in 180 Days ebook.
Pro Tip: Validate your tables using the W3C Markup Validation Service to ensure accessibility compliance.
Big announcement for our Dev.to authors: your special Dev.to drop now live for Dev.to contributors to celebrate our authors' impact in Web3! Don’t miss this opportunity here (for verified Dev.to users only). – Dev.to Airdrop Desk