Friday, December 20, 2024

HTML Tables

 HTML tables are used to display data in a tabular format, organized into rows and columns. Here's a basic structure for creating a table in 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 Example</title>
</head>
<body>
    <table border="1">
        <thead>
            <tr>
                <th>Header 1</th>
                <th>Header 2</th>
                <th>Header 3</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Data 1</td>
                <td>Data 2</td>
                <td>Data 3</td>
            </tr>
            <tr>
                <td>Data 4</td>
                <td>Data 5</td>
                <td>Data 6</td>
            </tr>
            <tr>
                <td>Data 7</td>
                <td>Data 8</td>
                <td>Data 9</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

Key elements:

  • <table>: Defines the table.
  • <thead>: Groups the header content in the table.
  • <tr>: Defines a row in the table.
  • <th>: Defines a header cell (typically bold and centered).
  • <td>: Defines a data cell in a table row.

Styling:

You can add CSS to enhance the appearance of the table. For example:

<style>
    table {
        width: 100%;
        border-collapse: collapse;
    }
    th, td {
        padding: 8px;
        text-align: center;
    }
    th {
        background-color: #f2f2f2;
    }
    tr:nth-child(even) {
        background-color: #f9f9f9;
    }
</style>

This adds styling to make the table look cleaner, such as alternating row colors and padding.

No comments:

Post a Comment

How can you refer to a CSS file in a web page?

 To refer to a CSS file in a web page, you use the <link> element inside the <head> section of the HTML document. Here's t...