HTML Tables: A Comprehensive Guide to Creating and Styling Tables for Your Website

Photo by Sigmund on Unsplash

HTML Tables: A Comprehensive Guide to Creating and Styling Tables for Your Website

From Basic Table Structures to Advanced Styling Techniques and Responsive Design

As a student learning HTML and CSS, I have discovered the importance of tables in creating organized and structured content on web pages. I learned about it in a course named "HTML: Tables" by Jen Kremer. Heres the link HTML: Tables

Tables are used to present information in a clear and concise way, making it easier for users to understand and navigate. In this blog post, I will share my insights on how to create and style tables in HTML and CSS, and how to make them accessible to all users.

Creating Tables in HTML

To create a table in HTML, we use the "table" tag, which contains the "tr" (table row) tag and the "td" (table data) tag. The "tr" tag creates a row in the table, and the "td" tag creates a cell in the row. Here is an example of how to create a simple table in HTML

<table>
  <tr>
    <td>Row 1, Column 1</td>
    <td>Row 1, Column 2</td>
  </tr>
  <tr>
    <td>Row 2, Column 1</td>
    <td>Row 2, Column 2</td>
  </tr>
</table>

In this example, we have created a table with two rows and two columns. We can add more rows and columns by repeating the "tr" and "td" tags.

Styling Tables in CSS

To style tables in CSS, we can use the "border" property to add borders to the table and its cells, and the "background-color" property to add color to the cells. Here is an example of how to style the table we created earlier:

table {
  border-collapse: collapse;
  border: 1px solid black;
}

td {
  border: 1px solid black;
  padding: 10px;
  background-color: #f2f2f2;
}

In this example, we have collapsed the borders of the table using the "border-collapse" property, and added a solid black border to the table and its cells. We have also added padding to the cells and a light gray background color using the "background-color" property.

Making Tables Accessible

When creating tables, it is important to consider accessibility for users who may use assistive technologies to access the content. To make tables accessible, we should use the "caption" tag to provide a title for the table, and the "th" (table header) tag to create headers for each column. Here is an example of how to make a table accessible:

<table>
  <caption>Monthly Expenses</caption>
  <tr>
    <th>Category</th>
    <th>Amount</th>
  </tr>
  <tr>
    <td>Housing</td>
    <td>$1,000</td>
  </tr>
  <tr>
    <td>Food</td>
    <td>$500</td>
  </tr>
</table>

In this example, we have added a title to the table using the "caption" tag, and created headers for the columns using the "th" tag. This helps to make the table more accessible and understandable for all users.

Making Tables Responsive

Responsive design is an important aspect of modern web development. Responsive tables ensure that the table's layout adapts to different screen sizes, making it easier to read and interact with on smaller devices.

To make a table responsive, we need to add CSS that adjusts the table's width and font size based on the screen size. Here is an example of a CSS code that makes the table responsive:

table {
  width: 100%;
  font-size: 16px;
}

@media screen and (max-width: 600px) {
  table {
    font-size: 14px;
  }
}

In this example, we set the table's width to 100% so that it takes up the full width of its container. We also set the font size to 16px. Then, using a media query, we reduce the font size to 14px when the screen size is less than 600px.

Conclusion

Tables are a powerful tool for displaying data and information on a website. By using HTML and CSS, we can create visually appealing and functional tables. With responsive design, we can ensure that our tables look great on any device. Remember to keep your tables simple and easy to read, and use responsive design to make them accessible to everyone

Did you find this article valuable?

Support Shubham Mete by becoming a sponsor. Any amount is appreciated!