HTML table Bgcolor attribute

The HTML bgcolor attribute is an old, non-standard attribute that was used to set the background color of a table or a table cell. This attribute is deprecated in HTML5, which means that it is no longer recommended to use it. Instead, the CSS background-color property should be used to set the background color of tables and table cells.

Here’s an example of how to use the bgcolor attribute:

<table bgcolor="lightblue">
  <tr>
    <td bgcolor="white">Cell 1</td>
    <td bgcolor="gray">Cell 2</td>
  </tr>
  <tr>
    <td bgcolor="yellow">Cell 3</td>
    <td bgcolor="lightgreen">Cell 4</td>
  </tr>
</table>

In this example, the bgcolor attribute is used to set the background color of the entire table to “lightblue”. The bgcolor attribute is also used to set the background color of individual cells to different colors.

As mentioned earlier, the bgcolor attribute is deprecated in HTML5 and should not be used. Instead, the background-color property should be used in CSS. Here’s an example of how to set the background color of a table and its cells using CSS:

<style>
  table {
    background-color: lightblue;
  }
  td {
    background-color: white;
  }
  td:nth-child(2) {
    background-color: gray;
  }
  td:nth-child(3) {
    background-color: yellow;
  }
  td:nth-child(4) {
    background-color: lightgreen;
  }
</style>
<table>
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
  </tr>
  <tr>
    <td>Cell 3</td>
    <td>Cell 4</td>
  </tr>
</table>

In this example, the background-color property is used to set the background color of the table and its cells. The nth-child pseudo-class is used to target specific cells and set their background color.

In summary, while the bgcolor attribute can still be used in older versions of HTML, it is no longer recommended in HTML5. Instead, the background-color property should be used in CSS to set the background color of tables and table cells.