HTML Table

Tables are an essential part of web design, allowing you to organize and present data in a tabular format. 

Introduction to HTML Tables

HTML tables are used to display data in rows and columns, similar to a spreadsheet. They are useful for presenting data in a structured format, making it easy to read and compare information.

Basic Structure of an HTML Table

The basic structure of an HTML table consists of the <table> element, along with the <tr>, <th>, and <td> elements for table rows, headers, and data cells, respectively.

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Basic HTML Table</title>
</head>
<body>

<h2>Basic HTML Table</h2>

<table border="1">
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Age</th>
    </tr>
    <tr>
        <td>John</td>
        <td>Doe</td>
        <td>30</td>
    </tr>
    <tr>
        <td>Jane</td>
        <td>Smith</td>
        <td>25</td>
    </tr>
</table>

</body>
</html>

Adding Headers, Footers, and Captions

You can enhance your table by adding headers (<thead>), footers (<tfoot>), and captions (<caption>).

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Table with Header, Footer, and Caption</title>
</head>
<body>

<h2>HTML Table with Header, Footer, and Caption</h2>

<table border="1">
    <caption>Employee Data</caption>
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="3">End of Data</td>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <td>John</td>
            <td>Doe</td>
            <td>30</td>
        </tr>
        <tr>
            <td>Jane</td>
            <td>Smith</td>
            <td>25</td>
        </tr>
    </tbody>
</table>

</body>
</html>

Merging Cells with colspan and rowspan

You can merge cells using the colspan and rowspan attributes to create more complex table structures.

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Table with Merged Cells</title>
</head>
<body>

<h2>HTML Table with Merged Cells</h2>

<table border="1">
    <tr>
        <th>Product</th>
        <th colspan="2">Details</th>
    </tr>
    <tr>
        <td rowspan="2">Laptop</td>
        <td>Brand</td>
        <td>Price</td>
    </tr>
    <tr>
        <td>Apple</td>
        <td>$1200</td>
    </tr>
</table>

</body>
</html>

Styling HTML Tables with CSS

CSS can be used to style HTML tables, making them more visually appealing and easier to read.

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Styled HTML Table</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
        }
        th {
            background-color: #f2f2f2;
            text-align: left;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #ddd;
        }
    </style>
</head>
<body>

<h2>Styled HTML Table</h2>

<table>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Age</th>
    </tr>
    <tr>
        <td>John</td>
        <td>Doe</td>
        <td>30</td>
    </tr>
    <tr>
        <td>Jane</td>
        <td>Smith</td>
        <td>25</td>
    </tr>
</table>

</body>
</html>

Advanced Features: Responsive Tables

Responsive tables adjust to different screen sizes, ensuring usability on mobile devices. This can be achieved using CSS media queries and responsive design techniques.

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive HTML Table</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        @media screen and (max-width: 600px) {
            table, thead, tbody, th, td, tr {
                display: block;
            }
            th {
                display: none;
            }
            td {
                display: block;
                text-align: right;
                padding-left: 50%;
                position: relative;
            }
            td::before {
                content: attr(data-label);
                position: absolute;
                left: 0;
                width: 50%;
                padding-left: 15px;
                font-weight: bold;
                text-align: left;
            }
        }
    </style>
</head>
<body>

<h2>Responsive HTML Table</h2>

<table>
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td data-label="First Name">John</td>
            <td data-label="Last Name">Doe</td>
            <td data-label="Age">30</td>
        </tr>
        <tr>
            <td data-label="First Name">Jane</td>
            <td data-label="Last Name">Smith</td>
            <td data-label="Age">25</td>
        </tr>
    </tbody>
</table>

</body>
</html>

Conclusion

Creating and styling HTML tables is an essential skill for web developers. This guide has provided you with the knowledge and code examples needed to create basic and advanced tables. By applying CSS and responsive design techniques, you can ensure that your tables are both visually appealing and functional across all devices.

tools

Computer Science

Related Articles