HTML Table Border

Tables are a fundamental part of HTML, allowing you to display data in rows and columns. Adding borders to tables enhances readability and helps distinguish different sections of data. 

Introduction to HTML Table Borders

HTML table borders help delineate the edges of the table and the cells within it, making the data more readable and visually appealing. Borders can be added using HTML attributes or CSS properties, allowing for various levels of customization.

Basic HTML Table with Borders

Adding borders to an HTML table is straightforward. You can use the border attribute within the <table> tag.

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 with Border</title>
</head>
<body>

<h2>Basic HTML Table with Border</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>

Customizing Table Borders with CSS

For more customization, use CSS to style the table borders. This allows for more control over the border properties, such as color, width, and style.

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom HTML Table Borders with CSS</title>
    <style>
        table, th, td {
            border: 2px solid black;
            border-collapse: collapse;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
    </style>
</head>
<body>

<h2>Custom HTML Table Borders with CSS</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>

Adding Borders to Specific Table Elements

You can also add borders to specific elements of the table, such as only the table cells (<td> and <th>), or just the table itself.

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 Borders on Specific Elements</title>
    <style>
        table {
            border: 2px solid blue;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid red;
            padding: 10px;
            text-align: left;
        }
    </style>
</head>
<body>

<h2>HTML Table with Borders on Specific Elements</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>

Using CSS Border Properties

CSS offers various border properties for more advanced styling. These include border-width, border-style, and border-color.

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Advanced CSS Border Properties for HTML Table</title>
    <style>
        table, th, td {
            border-width: 2px;
            border-style: solid;
            border-color: green;
            border-collapse: collapse;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
    </style>
</head>
<body>

<h2>Advanced CSS Border Properties for 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>

Creating Collapsed Borders

The border-collapse property allows you to create a single border around the table instead of individual borders for each cell.

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 Collapsed Borders</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid black;
            padding: 10px;
            text-align: left;
        }
    </style>
</head>
<body>

<h2>HTML Table with Collapsed Borders</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>

Conclusion

Adding borders to HTML tables enhances readability and organization of data. Using HTML attributes and CSS properties, you can create simple or complex border styles. This guide provided a comprehensive overview of various techniques to add and customize table borders, along with complete code examples.

tools

Computer Science

Related Articles