HTML Tables: A Beginner's Guide With Examples

HTML tables are a fundamental component of web development, allowing you to organize and display data in a structured and visually appealing manner. Whether you're creating a simple schedule, a pricing comparison, or a complex data representation, tables play a crucial role. In this guide, we will explore the anatomy of HTML tables and provide examples for each section.

Creating a Basic HTML Table

Let's start with the basics by creating a simple table. An HTML table consists of several key components: <table>, <tr> (table row), <th> (table header), and <td> (table data). Here's how you can create a basic table:

<!DOCTYPE html>
<html>
<head>
    <title>Basic HTML Table</title>
</head>
<body>
    <table>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
        </tr>
    </table>
</body>
</html>

Caption and Table Structure

Adding a caption helps provide context for your table. You can use the <caption> element for this purpose. You can also use the <thead>, <tbody>, and <tfoot> elements to structure your table for better styling or scripting.

<table>
    <caption>Sample Table</caption>
    <thead>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td>Total</td>
            <td>100</td>
        </tr>
    </tfoot>
</table>

Styling Your Table

HTML tables can be styled using CSS to enhance their appearance. Here's a basic example of styling a table using CSS:

<style>
    table {
        border-collapse: collapse;
        width: 100%;
    }

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

    th {
        background-color: #f2f2f2;
    }
</style>

Merging Cells

Sometimes, you might want to merge cells to create a more complex structure. This can be achieved using the colspan and rowspan attributes. Here's an example:

<table>
    <tr>
        <th colspan="2">Merged Header</th>
    </tr>
    <tr>
        <td>Data 1</td>
        <td>Data 2</td>
    </tr>
</table>

Adding Borders and Styling Rows

You can control the visibility and style of table borders by using CSS. Additionally, you can apply different styles to specific rows. Here's an example:

<style>
    table {
        border-collapse: collapse;
        width: 100%;
    }

    th, td {
        padding: 8px;
        text-align: left;
    }

    th {
        background-color: #f2f2f2;
    }

    tr:nth-child(odd) {
        background-color: #f0f0f0;
    }

    tr:hover {
        background-color: #ffffcc;
    }
</style>

Handling Responsive Tables

For responsive design, you can make your tables scroll horizontally on small screens. This ensures that the table is readable on mobile devices. Here's an example:

<style>
    table {
        width: 100%;
        overflow-x: auto;
    }
</style>


HTML tables are versatile and essential for structuring and displaying data on websites. In this guide, we've covered the basics of creating a table, structuring it, adding captions, styling, merging cells, and making tables responsive. With these skills, you can create tables that effectively convey your data to users.


Remember that while HTML tables are powerful, they should be used appropriately. For more complex data visualization and interaction, you may consider combining HTML tables with JavaScript or other technologies.