- HTML Tutorial
- HTML - Home
- HTML - Introduction
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - bgcolor Attribute
HTML bgcolor attribute or property is used to represent the background color of elements like body, table, tr, th, td etc.
This attribute is no longer used instead of that, we can use the CSS background-color property. We can use this attribute with javascript to change the background color of the table or any other following element.
The HTML ‘bgcolor’ attribute is not supported in HTML5 −
Syntax
<tag bgcolor = "value"></tag>
Where, value can be any color name, hex code, or RGB color code.
Applies On
Below listed elements allow using of the HTML bgcolor attribute.
Element | Description |
---|---|
<body> | HTML <body> tag is used to define the main content of any HTML document which will render on webpage. |
<table> | HTML <table> tag allow us to arrange data in a organized way by providing row and column facility. |
<tr> | HTML <tr> tag is used to define row of a table. |
<th> | HTML <th> tag is used to define header row of a table. |
<td> | HTML <td> tag is used to define cells inside rows of table. |
Examples of HTML bgcolor Attribute
Following codes demonstrate the usages of bgcolor attribute
Bgcolor attribute with body Tag
When we run the below code, it will generate an output consisting of the text with applied background color displayed on the webpage.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML bgcolor attribute</title> </head> <body bgcolor="red"> <h1>Example of HTML 'bgcolor' attribute</h1> </body> </html>
Background Color of table Element
Consider another scenario, where we are going to create a table and define a background-color to it.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML bgcolor attribute</title> <style> table { color: white; } </style> </head> <body bgcolor='yellow'> <h1>Example of HTML 'bgcolor' attribute</h1> <table bgcolor="green" border='1'> <tr> <th>S.No</th> <th>Name</th> <th>Email</th> </tr> <tr> <td>1.</td> <td>Abc</td> <td>abc123@gmail.com</td> </tr> <tr> <td>2.</td> <td>Xyz</td> <td>xyz23@gmail.com</td> </tr> </table> </body> </html>
Background Color to a row of Table
Let's look at the following example, where we are going to use the bgcolor attribute with the tr element.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML bgcolor attribute</title> </head> <body bgcolor='aqua'> <h1>Example of HTML 'bgcolor' attribute</h1> <p>Fruits and prices: </p> <table bgcolor="white" border='1'> <tr bgcolor='aquamarine'> <th>S.No</th> <th>Name</th> <th>Price</th> </tr> <tr> <td>1.</td> <td>Apple</td> <td>100 R/Kg</td> </tr> <tr> <td>2.</td> <td>Orange</td> <td>90 R/Kg</td> </tr> <tr> <td>3.</td> <td>Grapes</td> <td>130 R/Kg</td> </tr> </table> </body> </html>
Change background color Dynamically
In this program, we are going to create a button adding the onclick event which changes the background color when the user clicks on the button.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML bgcolor attribute</title> </head> <body bgcolor='aqua'> <h1>Example of HTML 'bgcolor' attribute</h1> <p>Click on the below button to change the background color.</p> <button onclick="Changed()">Changed</button> <script> function Changed() { var d = document.querySelector("body") d.bgColor = "yellow"; } </script> </body> </html>
Supported Browsers
Attribute | |||||
---|---|---|---|---|---|
bgcolor | Yes | Yes | Yes | Yes | Yes |