05. HTML tables: creating and formatting tables

05. HTML tables: creating and formatting tables

Hello web-spiders! 😉 Welcome back to 4th module of this series course. In this module, we discuss about forms in HTML.

·

5 min read

Tables in HTML

As usual, the tables you see in documents, are the same here in HTML web pages also, let's learn how to we will design the table in HTML.

In the realm of web development, HTML tables play a crucial role in organizing and presenting data in a structured format. They are essential for displaying information in rows and columns, making content more readable and visually appealing on web pages. The basic structure of an HTML table involves the use of elements like <table>, <tr> (table row), <th> (table header), and <td> (table data). Understanding these elements lays the foundation for creating well-organized and visually pleasing tables in HTML.

Creating a Simple Table:

Let's dive into the basics of creating a simple table. The <table> element is the container for the entire table, and within it, we use <tr> for each row. Inside the rows, we have <th> for table headers (typically found in the first row) and <td> for table data in subsequent rows. This structure allows us to define rows and columns effectively.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Table in HTML: part - 5</title>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid #ddd; /* Set border for table cells */
            padding: 8px;
            text-align: left;
        }
    </style>
</head>

<body>
    <h1>Visible Table Lines</h1>
    <table>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
        </tr>
        <tr>
            <td>Data 3</td>
            <td>Data 4</td>
        </tr>
    </table>
</body>
</html>

output

in the above code, there is a block of code <style>...</style>, this enables to make table more accessible in this web pages. Basically, <style>...</style> tag is used to style and design the web pages. Let's understand the code:
This HTML code represents a basic table structure. The <table> element defines the table, <tr> signifies table rows, and within each row, <th> is used for table headers and <td> for table data. In this example, it creates a 2x2 table with headers "Header 1" and "Header 2" in the first row, and data cells "Data 1", "Data 2", "Data 3", and "Data 4" in subsequent rows. The table is a fundamental way to organize and display information on a webpage.

Table Headers and Data Cells:

Differentiating between header and data cells using <th> and <td>:

In HTML tables, <th> is used to define header cells, while <td> is used for data cells. Header cells are typically used in the first row or column of a table to label the content in the cells.

Importance of using headers for accessibility and SEO:

Using <th> elements not only adds semantic meaning to your tables but is crucial for accessibility. Screen readers can interpret header cells, providing context to users with visual impairments. Additionally, search engines may use header cells to better understand the structure of your content, potentially improving SEO.

Spanning Rows and Columns:

Explaining the colspan and rowspan attributes:

The colspan attribute is used to merge cells horizontally, allowing a single cell to span across multiple columns. Similarly, the rowspan attribute merges cells vertically, spanning across multiple rows.

Responsive Tables:

Ensuring tables adapt to different screen sizes using CSS media queries:

Explain the importance of responsiveness in web design, especially for tables. Demonstrate how CSS media queries can be used to adjust the styling of tables based on different screen sizes. Provide examples of changing column widths, hiding less critical information on smaller screens, and ensuring readability.

Strategies for handling large tables on smaller screens:

Discuss effective strategies for dealing with large tables on smaller screens, such as horizontal scrolling, collapsing columns, or converting the table into a more user-friendly format like a list. Emphasize the importance of maintaining usability and readability.

Advanced Table Features:

Exploring advanced features such as captions, the <thead>, <tbody>, and <tfoot> elements:

Introduce advanced HTML table features that enhance structure and semantics. The <caption> element provides a title or explanation for the table. <thead>, <tbody>, and <tfoot> help organize the table body, making it more understandable for both developers and assistive technologies.

Discussing how these elements enhance table semantics and structure:

Explain how using <caption> provides a clear context for the table's content. Describe how <thead>, <tbody>, and <tfoot> improve the organization and accessibility of the table. Provide examples of implementing these elements in your HTML code.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Table in HTML: part - 5</title>
    <style>
        table {
          width: 100%;
          border-collapse: collapse;
          margin-bottom: 20px;
        }

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

        th {
          background-color: #f2f2f2;
        }

        /* Responsive design using media query */
        @media only screen and (max-width: 600px) {
          th, td {
            display: block;
            width: 100%;
          }
        }
      </style>
    </head>
    <body>

    <!-- Library Book Table -->
    <table>
      <caption>Library Book Data</caption>
      <thead>
        <tr>
          <th>Book ID</th>
          <th>Title</th>
          <th>Author</th>
          <th>Genre</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td rowspan="2">001</td>
          <td>The Catcher in the Rye</td>
          <td>J.D. Salinger</td>
          <td rowspan="2">Fiction</td>
        </tr>
        <tr>
          <td>The Great Gatsby</td>
          <td>F. Scott Fitzgerald</td>
        </tr>
        <tr>
          <td>002</td>
          <td>To Kill a Mockingbird</td>
          <td>Harper Lee</td>
          <td>Classics</td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="4">Total Books: 3</td>
        </tr>
      </tfoot>
    </table>
</body>
</html>

output_img

Gryt... we are done with one more module...

Hurrah! You have completed this module of the series Web Design Fundamentals: An HTML Primer for Beginners ►►►Click here to go to the next part of this series

After successful completion of this series course, you'll earn a certificate

Did you find this article valuable?

Support Mr. Ji by becoming a sponsor. Any amount is appreciated!

Â