Skip to content

Instantly share code, notes, and snippets.

@dragontheory
Created March 20, 2025 16:05
Show Gist options
  • Save dragontheory/e7fcf8ba2bd7a55501bd19418d4a30d1 to your computer and use it in GitHub Desktop.
Save dragontheory/e7fcf8ba2bd7a55501bd19418d4a30d1 to your computer and use it in GitHub Desktop.

Responsive Tables in Web Applications

Tables are essential for displaying structured data in web applications. However, traditional tables can be difficult to use on smaller screens, requiring horizontal scrolling or zooming. Implementing a responsive table ensures that data remains accessible and easy to navigate across different screen sizes.

πŸ† Benefits of a Responsive Table

πŸ“± 1. Improved User Experience

A well-designed responsive table ensures that users can access and interact with data seamlessly, regardless of the device they are using. For instance, collapsing table headers into inline labels on smaller screens improves readability and usability.

β™Ώ 2. Accessibility

Responsive tables enhance accessibility by allowing data to be presented in a format that works for screen readers and keyboard navigation. Using semantic HTML elements such as <table>, <thead>, <tbody>, <th>, and <td> ensures compatibility with assistive technologies.

πŸ‘€ 3. Enhanced Readability

By adjusting table layouts based on screen width, content remains clear and easy to follow. The mobile-first approach in the provided CSS example hides table headers on small screens and replaces them with inline labels for better readability.

πŸ—οΈ Semantic Markup for Better Structure

Semantic HTML ensures that web pages are meaningful and easier to maintain. The example uses:

  • <table> to define the data structure.
  • <thead> and <tbody> to separate headers from content.
  • <th> for column headers, which provide context to screen readers.
  • <td> for table data.

By structuring content with meaningful HTML elements, search engines and assistive technologies can better interpret the data.

πŸ–₯️ Example of a Responsive Table

HTML Structure

<table>
  <thead>
    <tr>
      <th style="width: 20px;"></th>
      <th style="width: 20px"><span class="material-icons">flag</span></th>
      <th style="width: 50px;">Time</th>
      <th>Focus</th>
      <th>Description</th>
      <th>Responsible</th>
      <th>ETC</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><span class="desktop-only"><input type="checkbox"></span></td>
      <td></td>
      <td><span class="mobile-only inline-header">Time:</span>10.51</td>
      <td><span class="mobile-only inline-header">Focus:</span>Noe rart</td>
      <td><span class="mobile-only inline-header">Description:</span>Lorem ipsum dolor sit amet</td>
      <td><span class="mobile-only inline-header">Responsible:</span>Hello Resp</td>
      <td><span class="mobile-only inline-header">ETC:</span>11:20</td>
      <td style="width: 60px;">
        <span class="mobile-only edit-delete"><span class="material-icons">more_vert</span></span>
        <span class="desktop-only">
          <span class="material-icons">edit</span>
          <span class="material-icons">delete</span>
        </span>
      </td>
    </tr>
  </tbody>
</table>

🎨 CSS for Responsive Behavior

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead tr th {
  text-align: left;
}

tbody tr {
  border-bottom: 0.063rem solid currentcolor;
}

.mobile-only {
  display: none;
}

.inline-header {
  font-weight: 600;
}

@media screen and (max-width: 37.5rem) {
  table thead {
    display: none;
  }

  .desktop-only {
    display: none;
  }

  .mobile-only {
    display: revert;
  }

  tbody tr {
    display: grid;
    grid-template-columns: 1fr;
    border-bottom: 0.063rem solid currentcolor;
    padding: 1rem;
    position: relative;
    gap: 0.5rem;
  }
  tbody tr td {
    border: none;
    padding: 0;
  }
}

βš™οΈ How This Works

  • On larger screens, the table appears in a traditional tabular format.
  • On smaller screens (below 600px width), headers are hidden, and each row displays data in a stacked format with labels (.inline-header).
  • Desktop-only features like checkboxes and individual action buttons are hidden in mobile view, replaced by a menu (more_vert icon).

βœ… Conclusion

Using semantic HTML and responsive design ensures that tables remain functional, accessible, and user-friendly across all devices. By hiding unnecessary elements, restructuring content, and leveraging CSS, we enhance both usability and accessibility, making web applications more efficient and adaptable.

πŸ“š References

  1. Web Accessibility Initiative (WAI) - Accessible Tables
    https://www.w3.org/WAI/tutorials/tables/

  2. MDN Web Docs - Responsive Design
    https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Responsive_Design

  3. CSS Tricks - Responsive Data Tables
    https://css-tricks.com/responsive-data-tables/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment