table

The table element represents tabular data arranged in rows and columns. Use tables for data that has logical relationships between rows and columns, not for layout purposes.

Description

The <table> element is the container for all tabular data. It establishes a two-dimensional grid where data is organized into rows (<tr>) containing cells (<th> and <td>).

Vanilla Breeze styles tables with full-width layout, collapsed borders, and tabular numeral font features by default. Tables automatically include hover states on rows for improved readability.

When to Use

  • Data comparison: When users need to compare values across rows or columns
  • Structured data: For data with clear relationships between headers and values
  • Reports and statistics: Financial data, analytics, inventory lists
  • Schedules and timetables: When time slots relate to activities or resources

When Not to Use

  • Layout purposes: Use CSS Grid or Flexbox instead. Consider <layout-grid> for card layouts.
  • Simple lists: Use <ul> or <ol> for non-tabular data
  • Key-value pairs: Use <dl> for definition lists

Basic Example

A properly structured table includes <caption> for accessibility, <thead> for headers, and <tbody> for data rows.

Employee Directory
Name Role Department Location
Alice Johnson Engineer Development New York
Bob Smith Designer Product San Francisco
Carol Williams Manager Operations Chicago
<table> <caption>Employee Directory</caption> <thead> <tr> <th scope="col">Name</th> <th scope="col">Role</th> <th scope="col">Department</th> <th scope="col">Location</th> </tr> </thead> <tbody> <tr> <td>Alice Johnson</td> <td>Engineer</td> <td>Development</td> <td>New York</td> </tr> <!-- More rows... --> </tbody> </table>

Variants

.striped

Alternating row colors improve readability for tables with many rows.

Product Inventory
SKU Product Category Price
A001 Laptop Stand Accessories $49.99
A002 Wireless Mouse Peripherals $29.99
A003 USB Hub Accessories $24.99
A004 Monitor Arm Furniture $89.99
A005 Keyboard Peripherals $79.99

.compact

Reduced padding for dense data displays where space is limited.

Event Log
Timestamp Event Status
2024-01-15 09:00 User login Success
2024-01-15 09:15 File upload Success
2024-01-15 09:30 API request Failed
2024-01-15 09:45 Database query Success

.bordered

Full borders on all cells, useful for complex data or when cells need clear separation.

Weekly Schedule
Time Monday Tuesday Wednesday
9:00 AM Team Standup Planning Review
2:00 PM Development Design Review Sprint Demo

Combined Variants

Variants can be combined for specific use cases.

Inventory Summary
SKU Item Qty Price
W001 Widget 100 $5.00
G002 Gadget 50 $12.00
S003 Sprocket 200 $3.50

Numeric Data Alignment

Use the data-numeric attribute on <th> and <td> elements to right-align numeric content and enable tabular numerals for proper alignment of digits.

Quarterly Financial Report
Quarter Revenue Expenses Profit
Q1 $150,000 $90,000 $60,000
Q2 $175,000 $95,000 $80,000
Q3 $200,000 $110,000 $90,000
Total $525,000 $295,000 $230,000
<th scope="col" data-numeric>Revenue</th> <td data-numeric>$150,000</td>

Responsive Patterns

Tables can be challenging on small screens. Here are recommended patterns:

Horizontal Scroll

Wrap tables in a container with overflow-x: auto to allow horizontal scrolling on narrow viewports.

Wide Data Table (scroll horizontally)
ID Name Email Phone Department Location Start Date
001 Alice Johnson alice@example.com (555) 123-4567 Engineering New York 2022-03-15
002 Bob Smith bob@example.com (555) 234-5678 Design San Francisco 2021-08-22
<div style="overflow-x: auto;"> <table> <!-- Wide table content --> </table> </div>

Layout Alternative

For non-tabular data that might look like a table, consider using <layout-grid> which provides responsive card layouts that adapt better to different screen sizes.

Accessibility

  • Always include a <caption>: Provides context for screen reader users navigating to the table
  • Use <th> with scope: Define whether headers apply to columns (scope="col") or rows (scope="row")
  • Structure with <thead>, <tbody>, <tfoot>: Helps assistive technologies understand table structure
  • Avoid empty cells: Use appropriate content or "N/A" rather than leaving cells empty
  • Keep tables simple: Avoid complex merged cells when possible

Screen Reader Announcement

Screen readers announce table structure, including the caption, number of rows and columns, and header associations. A well-structured table helps users navigate efficiently.

Variants Summary

Class/Attribute Applies to Description
.striped table Alternating row background colors
.compact table Reduced cell padding for dense displays
.bordered table Full borders on all cells
data-numeric th, td Right-align content and use tabular numerals

Related Elements

Layout Alternatives

  • <layout-grid> - For card-based layouts that adapt to screen size