The bgcolor attribute is used to specify the background color of an HTML element. It was commonly used in older HTML but is now deprecated in HTML5 in favor of CSS.
- Sets the background color of elements like <body>, <table>, <tr>, <td>
- Accepts color names, hex values, or RGB values
- Helps in styling and visual presentation of elements
Note: The bgcolor attribute is not supported in HTML5.
Syntax
<"tag" bgcolor="color_name | hex_value | rgb_value">Attribute Values
- color_name: Sets background color using a color name (e.g., "red").
- hex_number: Sets background color using a hex code (e.g., "#0000ff").
- rgb_number: Sets background color using RGB values (e.g., "rgb(0, 153, 0)").
Supported Tags
- <body>: <body> contains content visible to users.
- <marquee>: <marquee> scrolls text or images within a container.
- <table>: <table> organizes data into rows and columns.
- <tbody>: <tbody> encloses main content rows within a table.
- <td>: <td> represents data cells within a table row.
- <tfoot>: <tfoot> defines a footer section within a table.
- <th>: <th> indicates header cells within a table row.
- <thead>: <thead> contains header rows within a table.
- <tr>: <tr> represents a table row containing data or headers.
- <col>: <col> defines properties for one or more table columns.
- <colgroup>: <colgroup> groups columns in a table for styling.
Example 1: In this example, the table's background color is set to green using the bgcolor attribute.
<!DOCTYPE html>
<html>
<head>
<title>
HTML table bgcolor Attribute
</title>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>HTML table bgcolor Attribute</h2>
<table border="2" bgcolor="sky blue">
<caption>
Author Details
</caption>
<tr>
<th>NAME</th>
<th>AGE</th>
<th>BRANCH</th>
</tr>
<tr>
<td>BEN</td>
<td>22</td>
<td>CSE</td>
</tr>
<tr>
<td>RON</td>
<td>21</td>
<td>ECE</td>
</tr>
</table>
</body>
</html>
Example 2: In this example, the bgcolor attribute within the <body> tag is used to set the background color of the entire page to orange.
<!DOCTYPE html>
<html>
<head>
<title>
HTML body Bgcolor Attribute
</title>
</head>
<!-- body tag starts here -->
<body text="white" bgcolor="green">
<center>
<h1>GeeksforGeeks</h1>
<h2>
HTML
body bgcolor Attribute
</h2>
<p>
It is a Computer
Science portal For Geeks
</p>
</center>
</body>
<!-- body tag ends here -->
</html>