DEV Community

Cover image for Day 11/180 of Frontend Dev: Mastering HTML Tables for Structured Data
CodeWithDhanian
CodeWithDhanian

Posted on

Day 11/180 of Frontend Dev: Mastering HTML Tables for Structured Data

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Row Span (rowspan):

<table>
  <tr>
    <th rowspan="2">Region</th>
    <th>Q1</th>
  </tr>
  <tr>
    <td>$15,000</td>
  </tr>
</table>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Table Best Practices

  1. Always Use Semantic Structure

    • Group headers (<thead>)
    • Separate body content (<tbody>)
    • Include footers (<tfoot>) when needed
  2. Accessibility Must-Haves

    • scope attributes
    • caption elements
    • ARIA labels for complex tables
  3. Performance Considerations

    • Avoid nested tables
    • Limit to ~1,000 rows (paginate larger datasets)
    • Use loading="lazy" for tables with images

Exercises

  1. Convert this data to a proper HTML table:
   Product | Price | Stock
   Widget  | $10   | 250
   Gadget  | $25   | 180
Enter fullscreen mode Exit fullscreen mode
  1. Create a calendar for June 2023 using:

    • colspan for weekend groupings
    • Semantic table sections
    • Accessible headers
  2. Fix these table errors:

   <table>
     <td>Header 1</td>
     <td>Header 2</td>
     <tr>
       <th>Data</th>
     </tr>
   </table>
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (5)

Collapse
 
netzro profile image
Netzro

Thanks

Collapse
 
code_2 profile image
CodeWithDhanian

Thanks for checking out

Collapse
 
madhurima_rawat profile image
Madhurima Rawat

Good article! You can include outputs too for better understanding.

Collapse
 
code_2 profile image
CodeWithDhanian

I got you

Some comments may only be visible to logged-in visitors. Sign in to view all comments.