HTML Tables Guide
Learn how to create accessible, semantic, and styled tables in HTML
What are HTML Tables?
HTML tables (<table>
) are used to display tabular data in rows and columns. Tables should be used for:
- Displaying data that has a natural tabular structure
- Showing relationships between different data points
- Presenting financial data, schedules, or comparisons
- Creating calendars, pricing charts, or statistical data
Key Features:
- Semantic structure with thead, tbody, and tfoot
- Row and cell spanning with colspan and rowspan
- Accessibility features like scope and captions
- Responsive design techniques
- Extensive CSS styling possibilities
Interview & Exam Preparation
Common Interview Questions:
- When should you use a table versus other HTML elements?
- How do you make tables accessible for screen readers?
- What are the semantic elements of an HTML table?
- How would you create a responsive table?
- What are the differences between colspan and rowspan?
Key Concepts to Remember:
- Tables are for tabular data, not page layout
- Always use proper heading structure with scope attributes
- Include captions for complex tables
- Consider mobile users with responsive techniques
- Test with screen readers for accessibility
Basic Table Structure
Simple table with rows and cells
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
</table>
Usage: For displaying simple tabular data
Table with Caption
Table with a descriptive caption
<table>
<caption>Monthly Expenses</caption>
<tr>
<th>Category</th>
<th>Amount</th>
</tr>
<tr>
<td>Food</td>
<td>$300</td>
</tr>
</table>
Usage: When the table needs a title or description
Cell Spanning
Cells that span multiple columns or rows
<table>
<tr>
<th colspan="2">Expenses</th>
</tr>
<tr>
<td rowspan="2">Food</td>
<td>$200</td>
</tr>
<tr>
<td>$150</td>
</tr>
</table>
Usage: For merging cells horizontally or vertically
Semantic Table Structure
Using thead, tbody, and tfoot elements
Scope Attribute
Defining relationships between headers and cells
Responsive Tables
Making tables work on small screens
Product Comparison Table
Class Schedule
Pricing Plan Table
Table with Caption and Summary
Providing context for screen readers
Complex Table with Headers
Multiple levels of headers for complex data
Zebra Striping
Alternating row colors for better readability
Hover Effects
Highlighting rows on hover
Border Collapse
Controlling table borders
Table Playground
Experiment with tables in our live editor
Table Examples Preview
Ready-to-use examples covering different table scenarios
Features Included
All examples will be loaded into an interactive editor where you can modify and test them
Exam & Interview Preparation
Common Interview Questions
- Q: When should you use a table versus CSS for layout?
A: Tables should only be used for tabular data, not for page layout. For layout, use CSS Flexbox or Grid which are more semantic and responsive.
- Q: How do you make complex tables accessible?
A: Use proper scope attributes, headers/id associations, captions, and ARIA attributes where needed. Test with screen readers.
- Q: What are the semantic elements of an HTML table?
A: <table>, <caption>, <thead>, <tbody>, <tfoot>, <tr>, <th>, and <td>.
Key Concepts to Remember
- Semantic Structure: Always use proper table elements for their intended purpose
- Accessibility: Tables must be navigable and understandable with screen readers
- Responsiveness: Tables need special handling on mobile devices
- Performance: Very large tables can impact performance - consider pagination
- Styling: Modern CSS provides many table styling options
Practical Scenarios
- Displaying financial reports
- Creating comparison charts
- Showing schedules or calendars
- Presenting statistical data
- Building data-rich admin interfaces