Skip to content

Using the BGCOLOR Attribute in table

The bgcolor attribute can be applied to the <table> element to set the background color of the entire table. Additionally, it can be applied to <th> (table header) and <td> (table data) elements to set the background color of specific cells.

Example of bgcolor for the Entire 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 BGCOLOR Attribute</title>

</head>

<body>

    <h1>Table with BGCOLOR Attribute for the Entire Table</h1>

    <table border=”1″ bgcolor=”lightblue”>

        <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 bgcolor attribute is set to “lightblue”, which sets the background color of the entire table to light blue.

Example of bgcolor for Individual Cells

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

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

    <title>Table with BGCOLOR Attribute for Cells</title>

</head>

<body>

    <h1>Table with BGCOLOR Attribute for Individual Cells</h1>

    <table border=”1″>

        <tr>

            <th bgcolor=”lightblue”>Header 1</th>

            <th bgcolor=”lightgreen”>Header 2</th>

            <th bgcolor=”lightcoral”>Header 3</th>

        </tr>

        <tr>

            <td bgcolor=”lightpink”>Data 1</td>

            <td bgcolor=”lightsalmon”>Data 2</td>

            <td bgcolor=”lightyellow”>Data 3</td>

        </tr>

        <tr>

            <td bgcolor=”lightcyan”>Data 4</td>

            <td bgcolor=”lightgoldenrodyellow”>Data 5</td>

            <td bgcolor=”lightskyblue”>Data 6</td>

        </tr>

    </table>

</body>

</html>

In this example, the bgcolor attribute is applied to individual <th> and <td> elements to set the background color of specific cells.

Cell Background Color with CSS

While the bgcolor attribute is straightforward and easy to use, modern web development practices prefer using CSS for styling, as it provides more flexibility and control over the appearance of HTML elements. Using CSS, you can achieve the same effect as bgcolor by applying background color properties to <th> and <td> elements.

Example Using CSS for Cell Background Color

<!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 Background Color</title>

    <style>

        table {

            border-collapse: collapse;

            width: 100%;

        }

        th, td {

            border: 1px solid black;

            padding: 10px;

            text-align: left;

        }

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

            background-color: lightblue;

        }

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

            background-color: lightgreen;

        }

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

            background-color: lightcoral;

        }

    </style>

</head>

<body>

    <h1>Table with CSS Background Color for Cells</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:

  • Background colors are applied to <th> and <td> elements using the background-color property within CSS selectors (th:nth-child(1), td:nth-child(2), etc.).

Comparison and Recommendations

  • Using bgcolor Attribute: Quick and simple for setting background colors, suitable for small projects or quick prototypes.
  • Using CSS for Background Color: Recommended for modern web development due to greater flexibility, maintainability, and compatibility with other CSS styles.

By using CSS, you can easily customize the background color of individual cells and achieve a more polished and tailored look for your table. Additionally, CSS provides comprehensive control over the styling of your table, making it suitable for complex and responsive web designs.