DEV Community

Cover image for HTML Tables: Creating and Styling Data Tables
Sharique Siddiqui
Sharique Siddiqui

Posted on

HTML Tables: Creating and Styling Data Tables

HTML tables are a fundamental way to organize and present data on the web. They allow you to display information clearly, making it easy for users to read and interpret. Whether you're showcasing pricing plans, comparison charts, or structured data, knowing how to create and style HTML tables is an essential skill for web developers and designers.

Creating HTML Tables

An HTML table is created using the <table> element. Inside a table, the data is organized into rows with <tr> tags. Each row contains data cells, which are specified using <td> (table data) elements. For header cells that describe the content of the columns or rows, you use <th> (table header) elements.

Here’s a simple example of an HTML table:
xml
<table>
  <tr>
    <th>Product</th>
    <th>Price</th>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>Apples</td>
    <td>$1.00</td>
    <td>10</td>
  </tr>
  <tr>
    <td>Oranges</td>
    <td>$0.80</td>
    <td>5</td>
  </tr>
</table>
Enter fullscreen mode Exit fullscreen mode

This structure creates a table with a header row and two data rows. The headers make it easier to understand what each column represents.

Key Table Elements

  • <table>: Defines the table.
  • <tr>: Defines a table row.
  • <th>: Defines a header cell.
  • <td>: Defines a data cell.
  • <thead>, <tbody>, <tfoot>: Optional tags to group header, body, and footer content, which can help with styling and organization.

Styling HTML Tables

Plain HTML tables appear very basic and sometimes hard to read. CSS (Cascading Style Sheets) allows you to add style and polish to your tables, improving readability and aesthetics.

Basic Styling Tips

  • Add borders to cells and the table.
  • Use padding for space inside cells.
  • Set background colors for headers or alternating rows.
  • Control text alignment inside cells.
Example CSS:
css
table {
  width: 100%;
  border-collapse: collapse; /* Removes double borders */
}

th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

th {
  background-color: #f2f2f2;
  font-weight: bold;
}

tr:nth-child(even) {
  background-color: #f9f9f9; /* Zebra stripes for better readability */
}
Enter fullscreen mode Exit fullscreen mode

Advanced Styling

  • Use hover effects to highlight rows when the user’s mouse hovers over them.
  • Apply different styles to <thead>, <tbody>, and <tfoot>.
  • Make tables responsive for smaller screens using CSS media queries or by wrapping the table in a scrollable container.
Example hover effect:
css
tr:hover {
  background-color: #ddd;
}
Enter fullscreen mode Exit fullscreen mode

Responsive Tables

Tables can be tricky on small screens because the layout often requires horizontal scrolling. A common approach is to wrap the table in a container with overflow-x: auto:

css
.table-container {
  overflow-x: auto;
}
Enter fullscreen mode Exit fullscreen mode

And then wrap your table:

xml
<div class="table-container">
  <table>
    <!-- table content -->
  </table>
</div>
Enter fullscreen mode Exit fullscreen mode

Accessibility Considerations

For better accessibility:

  • Use <caption> to provide a table summary.
  • Properly use <th> elements and scope attributes (scope="col" or scope="row") for headers.
  • Ensure sufficient contrast in colors.
  • Consider using ARIA roles and properties if needed.

Final Thoughts

Creating and styling HTML tables is a vital skill for presenting structured data on websites. Basic HTML tags let you build functional tables, while CSS empowers you to make them visually appealing and user-friendly. By combining semantic HTML with thoughtful styling and accessibility considerations, you can create effective data tables that enhance the user experience.

Check out the YouTube Playlist for great HTML content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud

Top comments (0)