Skip to content

Using the Width and Border Attribute in table

In HTML, tables can be styled and formatted using various attributes, including the width and border attributes. These attributes help define the visual appearance and structure of the table.

The width Attribute

The width attribute is used to set the width of the table or individual columns. It can be specified using either percentage values or pixel values. Using percentages allows the table to adjust dynamically based on the width of the parent container, making it more responsive.

Setting Table Width

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Table Width Example</title>

</head>

<body>

    <h1>Table with Width Attribute</h1>

    <table width=”80%” border=”1″>

        <tr>

            <th>Header 1</th>

            <th>Header 2</th>

            <th>Header 3</th>

        </tr>

        <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>

    </table>

</body>

</html>

In this example, the table’s width is set to 80% of the parent container’s width.

Setting Column Widths

You can also set the width of individual columns using the width attribute within <th> or <td> elements.

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Column Width Example</title>

</head>

<body>

    <h1>Table with Column Widths</h1>

    <table border=”1″>

        <tr>

            <th width=”200″>Header 1</th>

            <th width=”150″>Header 2</th>

            <th width=”100″>Header 3</th>

        </tr>

        <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>

    </table>

</body>

</html>

Here, the first column’s width is set to 200 pixels, the second to 150 pixels, and the third to 100 pixels.

The border Attribute

The border attribute specifies the width of the border around the table and its cells. The value is specified in pixels.

Setting Table Border

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Table Border Example</title>

</head>

<body>

    <h1>Table with Border Attribute</h1>

    <table border=”2″>

        <tr>

            <th>Header 1</th>

            <th>Header 2</th>

            <th>Header 3</th>

        </tr>

        <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>

    </table>

</body>

</html>

In this example, the border attribute is set to 2, creating a 2-pixel-wide border around the table and its cells.

Combining width and border Attributes

You can combine both attributes to control both the width and the border of the table.

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Table with Width and Border</title>

</head>

<body>

    <h1>Table with Width and Border Attributes</h1>

    <table width=”100%” border=”3″>

        <tr>

            <th width=”50%”>Header 1</th>

            <th width=”25%”>Header 2</th>

            <th width=”25%”>Header 3</th>

        </tr>

        <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>

    </table>

</body>

</html>

In this example:

  • The table’s width is set to 100% of the parent container.
  • The table has a 3-pixel-wide border.
  • The first column is 50% of the table’s width, while the second and third columns are each 25%.

Conclusion

Using the width and border attributes allows you to control the size and appearance of HTML tables effectively. While these attributes can be useful, it is often recommended to use CSS for more advanced and flexible styling. CSS provides greater control and allows for more complex styling options, such as different border styles, colors, and spacing. Here’s how you can achieve similar results using CSS:

Example Using CSS

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Table with CSS Width and Border</title>

    <style>

        table {

            width: 100%;

            border-collapse: collapse;

        }

        th, td {

            border: 3px solid black;

            padding: 8px;

            text-align: left;

        }

        th {

            width: 50%;

        }

        td:nth-child(2), th:nth-child(2) {

            width: 25%;

        }

        td:nth-child(3), th:nth-child(3) {

            width: 25%;

        }

    </style>

</head>

<body>

    <h1>Table with CSS Width and Border</h1>

    <table>

        <tr>

            <th>Header 1</th>

            <th>Header 2</th>

            <th>Header 3</th>

        </tr>

        <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>

    </table>

</body>

</html>

In this CSS example, the table width and border are defined in the stylesheet, providing a cleaner and more flexible way to style HTML tables.