backgroundColor style property
Specifies or returns the color to use for the background of the object.
With the backgroundColor property you can set the background color of an element and you can also set the element's background to transparent.
Transparency is allowed by default for frame elements (frame and iframe) in all commonly used browsers, except in Internet Explorer before version 9.
In Internet Explorer before version 9, use allowTransparency property to allow a frame element to be transparent.
Syntax:
You can find the related objects in the Supported by objects section below.
This property is read/write.
CSS page for this property: background-color |
Possible values:
The type of this property is string.
One of the following values:
Color of the background. For the supported color values, see the colors page. | |||||||
Takes the value of this property from the computed style of the parent element. | |||||||
Default. Underlying content will shine through. |
Default: transparent.
Example HTML code 1:
This example illustrates the use of the background-color property:
|
||||
<head> <style> .example { background-color: #FF0000; width: 300px; height: 200px; overflow: scroll; } </style> </head> <body> <div class="example"> Some content ..... <br /> ..... <br /> ..... <br /> Some content ..... <br /> ..... <br /> ..... <br /> Some content ..... <br /> ..... <br /> ..... <br /> Some content ..... <br /> ..... <br /> ..... <br /> </div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example shows how to create a roll-over effect in JavaScript:
|
||||
<head> <style> .normal { background-color: #FFE4E3; border: 1px solid #CCCCCC; } .over { background-color: #237765; border: 1px solid #CCCCCC; color: #FFFFFF; } .down { background-color: #FF0000; border: 1px solid #237765; color: #FFFFFF; } </style> <script type="text/javascript"> function ChangeClass (button, cls) { button.className = cls; } </script> </head> <body> <button class="normal" onmouseover="ChangeClass (this, 'over');" onmouseout="ChangeClass (this, 'normal');" onmousedown="ChangeClass (this, 'down');" > Hover or click this button! </button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 3:
This example illustrates the use of the backgroundColor property in JavaScript:
|
||||
<head> <script type="text/javascript"> function ChangeBgColor (selectTag) { // Returns the index of the selected option var whichSelected = selectTag.selectedIndex; // Returns the text of the selected option var selectState = selectTag.options[whichSelected].text; var div = document.getElementById ("myDiv"); div.style.backgroundColor = selectState; } </script> </head> <body> <div id="myDiv">Change the background-color</div> <select onchange="ChangeBgColor (this);" size="8"> <option selected="selected" />white <option />red <option />cyan <option />blue <option />green <option />yellow <option />purple <option />black </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CSSStyleDeclaration, htmlElement.currentStyle, htmlElement.runtimeStyle, htmlElement.style
HTML elements:
a, abbr, acronym, address, b, bdo, big, blink, blockquote, body, button, caption, center, cite, code, col, colgroup, dd, del, dfn, dir, div, dl, dt, em, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, label, legend, li, listing, marquee, menu, nobr, ol, optgroup, option, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, u, ul, var, xmp
Related pages:
background
backgroundAttachment
backgroundImage
backgroundPosition
backgroundPositionX
backgroundPositionY
backgroundRepeat
backgroundAttachment
backgroundImage
backgroundPosition
backgroundPositionX
backgroundPositionY
backgroundRepeat
External links:
background-color (MSDN)
background-color (Mozilla Developer Center)
background-color (Safari Reference Library)
background-color (W3C)
background-color (Mozilla Developer Center)
background-color (Safari Reference Library)
background-color (W3C)
User Contributed Comments