DEV Community

Cover image for HTML Table tag A to Z
Hridoy Hasan
Hridoy Hasan

Posted on

HTML Table tag A to Z

HTML Tables: A Comprehensive Guide

Tables are a fundamental part of HTML used to display tabular data. In this article, we'll explore how to create and style tables effectively, along with examples and best practices.

1. Basic Table Structure

A basic HTML table is created using the <table> element. Within the table, rows are defined using the <tr> tag, and each cell within a row is defined using the <td> tag. Table headers can be defined using the <th> tag.

Example: Basic Table

<!DOCTYPE html>
<html>
<head>
    <title>Basic Table Example</title>
</head>
<body>
    <h1>Basic HTML Table</h1>
    <table border="1">
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
        </tr>
        <tr>
            <td>Row 1, Cell 1</td>
            <td>Row 1, Cell 2</td>
            <td>Row 1, Cell 3</td>
        </tr>
        <tr>
            <td>Row 2, Cell 1</td>
            <td>Row 2, Cell 2</td>
            <td>Row 2, Cell 3</td>
        </tr>
    </table>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output:

Basic HTML Table

Header 1 Header 2 Header 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3

In this example, the table has a border and contains two rows of data, each with three cells.

2. Adding a Caption

A table caption provides a title or description for the table and is defined using the <caption> tag.

Example: Table with Caption

<!DOCTYPE html>
<html>
<head>
    <title>Table with Caption Example</title>
</head>
<body>
    <h1>HTML Table with Caption</h1>
    <table border="1">
        <caption>Monthly Sales Data</caption>
        <tr>
            <th>Month</th>
            <th>Sales</th>
        </tr>
        <tr>
            <td>January</td>
            <td>$1000</td>
        </tr>
        <tr>
            <td>February</td>
            <td>$1200</td>
        </tr>
    </table>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output:

HTML Table with Caption

Monthly Sales Data

Month Sales
January $1000
February $1200

In this example, the table has a caption that describes the data it contains.

3. Table Headers and Footers

Tables can have headers and footers defined using the <thead> and <tfoot> tags, respectively. These elements help to organize table data.

Example: Table with Headers and Footers

<!DOCTYPE html>
<html>
<head>
    <title>Table with Headers and Footers Example</title>
</head>
<body>
    <h1>HTML Table with Headers and Footers</h1>
    <table border="1">
        <thead>
            <tr>
                <th>Product</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Apples</td>
                <td>$1.00</td>
            </tr>
            <tr>
                <td>Oranges</td>
                <td>$0.80</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td>Total</td>
                <td>$1.80</td>
            </tr>
        </tfoot>
    </table>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output:

HTML Table with Headers and Footers

Product Price
Apples $1.00
Oranges $0.80
Total $1.80

In this example, the table includes a header for product names and prices, a body with the product data, and a footer with the total price.

4. Merging Cells

Cells can be merged using the colspan and rowspan attributes to span multiple columns or rows.

Example: Table with Merged Cells

<!DOCTYPE html>
<html>
<head>
    <title>Table with Merged Cells Example</title>
</head>
<body>
    <h1>HTML Table with Merged Cells</h1>
    <table border="1">
        <tr>
            <th>Item</th>
            <th>Details</th>
        </tr>
        <tr>
            <td rowspan="2">Fruit</td>
            <td>Apples</td>
        </tr>
        <tr>
            <td>Oranges</td>
        </tr>
        <tr>
            <td colspan="2">Total Items: 2</td>
        </tr>
    </table>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output:

HTML Table with Merged Cells

Item Details
Fruit Apples
Oranges
Total Items: 2

In this example, the first cell in the second row spans two rows, and the cell in the last row spans two columns.

Benefits of Using HTML Tables

  • Organization: Tables provide a clear and organized way to display data.
  • Readability: They make numerical and textual data easier to read and compare.
  • Accessibility: Screen readers can easily interpret table structures, improving accessibility for visually impaired users.

Conclusion

Understanding how to use HTML tables is essential for organizing and presenting tabular data effectively. Whether you're creating basic tables, adding captions, using headers and footers, or merging cells, mastering these elements will enhance the readability and usability of your web pages.

follow me on LinkedIn - https://www.linkedin.com/in/ridoy-hasan7

Top comments (0)