background style property
Specifies or returns up to five separate background properties, in a shorthand form.
With this property, you can set the background color and / or image of any visible HTML element.
Furthermore, you can set how the background image is displayed. You have control over how the background image behaves if the
contents of the element are scrolled (stays in a fixed position, or scrolls with the contents, as it normally does). You can also set
how the image should be repeated (repeats vertically, horizontally, both, or in neither direction).
These features can also be set with five different style properties, listed below.
The use of separate properties is highly recommended for non advanced authors for better controllability.
If you want to get background features from JavaScript, for cross-browser functionality, use the five stand-alone properties.
To set, you can use all six properties.
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 |
Possible values:
The type of this property is string.
One of the following values: | ||||||||||||||||
|
Description of values:
Specifies or returns whether the backgroundImage should scroll with the contents of the object or not. | |||||||
Specifies or returns the color to use for the background of the object. | |||||||
Specifies or returns the image to use as the background of the object. | |||||||
Sets or retrieves the position of the backgroundImage within the element. | |||||||
Specifies or returns how to tile the backgroundImage relative to the upper-left corner of the container object. | |||||||
Takes the value of this property from the computed style of the parent element. |
Default: transparent none repeat scroll 0% 0%.
Example HTML code 1:
This example illustrates the use of the background property:
|
||||
<head> <style> .example1 { background: #F9F9F9 url("picture.gif") no-repeat center bottom; width: 300px; height: 120px; } .example2 { background-color: #F9F9F9; background-image: url("picture.gif"); background-repeat: no-repeat; background-position: center bottom; width: 300px; height: 120px; } </style> </head> <body> <div class="example1">The background image is at the center bottom of this division.</div> <div class="example2">This is the same, but without shorthand background property.</div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example shows how to create a gradient background. First you need to create an image in a painting program. In most cases, the image can be very slim (it may even be 1 pixel wide).
|
||||
<head> <style> .gradient { background-image: url("gradient.gif"); background-repeat: repeat-x; } </style> </head> <body> <div class="gradient"> Division element with <br /> gradient background. </div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 3:
This example illustrates the use of the background property in JavaScript:
|
||||
<head> <style> .example { background: #F9F9F9 url("picture.gif") no-repeat center bottom; width: 300px; height: 200px; } </style> <script type="text/javascript"> function ChangeToGreen (elem) { elem.style.background = "green"; } </script> </head> <body> <div class="example" onclick="ChangeToGreen (this)">Click this division</div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 4:
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 5:
This example shows how to change different background properties easily in JavaScript:
|
||||
<head> <style> .example { background: #F9F9F9 url("picture.gif") no-repeat center bottom; width: 300px; height: 120px; } </style> <script type="text/javascript"> function ChangeBg (type, elem, posID) { var divToChange = document.getElementById ("myDiv"); var newValue = elem.options[elem.selectedIndex].text; switch (type) { case "color": divToChange.style.backgroundColor = newValue; break; case "repeat": divToChange.style.backgroundRepeat = newValue; break; case "position": var otherPosElem = document.getElementById (posID); var otherValue = otherPosElem.options[otherPosElem.selectedIndex].text; divToChange.style.backgroundPosition = (newValue + " " + otherValue); break; } } </script> </head> <body> <div id="myDiv" class="example">Click this division</div> <table border="1px"> <tr> <td>Change Color:</td> <td> <select onchange="ChangeBg ('color', this)" size="3"> <option selected="selected" />#F9F9F9 <option />red <option />green </select> </td> </tr> <tr> <td>Change repetition type:</td> <td> <select onchange="ChangeBg ('repeat', this)" size="4"> <option selected="selected" />no-repeat <option />repeat <option />repeat-x <option />repeat-y </select> </td> </tr> <tr> <td>Change vertical position:</td> <td> <select id="vert" onchange="ChangeBg ('position', this, 'hor')" size="3"> <option />top <option />center <option selected="selected" />bottom </select> </td> </tr> <tr> <td>Change horizontal position:</td> <td> <select id="hor" onchange="ChangeBg ('position', this, 'vert')" size="3"> <option />left <option selected="selected" />center <option />right </select> </td> </tr> </table> </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:
backgroundAttachment
backgroundColor
backgroundImage
backgroundPosition
backgroundPositionX
backgroundPositionY
backgroundRepeat
backgroundColor
backgroundImage
backgroundPosition
backgroundPositionX
backgroundPositionY
backgroundRepeat
External links:
background (MSDN)
background (Mozilla Developer Center)
background (Safari Reference Library)
background (W3C)
background (Mozilla Developer Center)
background (Safari Reference Library)
background (W3C)
User Contributed Comments