Backgrounds
ON THIS PAGE
| Property Name | Some Possible Values | What It Does |
|---|---|---|
| background | (All background properties at once) | Sets background |
| background-color | (Any color) | Background color |
| background-image | url('example.jpg') | Background image |
| background-repeat | repeat-x | repeat-y | no-repeat | Tile background? |
| background-position | top left | 50% 100% | Position? |
| background-attachment | fixed | scroll | Fixed or scroll? |
NOTES:
- Background ~ This is CSS shorthand property that allows you to set five different background related properties at once. The shorthand syntax for
backgroundis written as follows:
background: color image repeat position attachment;
In the above:
color= Any named color or hexadecimal color code.Possible values:red|green|navy|#ffffcc|#000000image= Path to any background image file.Syntax example:url("background.jpg")repeat= A value that defines how the background image is repeated.Possible values:repeat-x|repeat-y|no-repeatposition= A pair of values that set the background image's position.Possible values:left top|center center|right bottomattachment= Any value(s) applicable tobackground-attachment.Possible values:fixed|scroll
backgroundproperty is declared and then followed by a space-separated list of values pertaining to any one or more of the above.
Here's an example:
background: orange url("test.jpg") no-repeat center center fixed;
In the above example, the background color is orange and the background image is 'test.jpg'. The background image is only displayed once in the center of the web page and is fixed in place such that when the user scrolls, it always remains within the browser viewport.
All the values set using thebackgroundshorthand property can be set individually using the following CSS properties:
- Background-color ~ This CSS property defines the background color. Any named color or hexadecimal color code may be used, e.g.,
red,green,blue,#ffffcc, etc. (Please see font color for more about what colors you can use for any HTML or CSS code.)
Example:background-color: silver; - Background-image ~ This CSS property defines the background image. The syntax to use is
url("path to any image file")wherepath to any image fileequals any relative or absolute path to the image file you wish to display as a background.
Examples:
background-image: url("mycoolpic.gif");
background-image: url("pics/mycoolpic.gif");
background-image: url("http://www.example.com/mycoolpic.gif"); - Background-repeat ~ By default, background images (when declared) are repeated horizontally and vertically until the entire element is filled. You can use the
background-repeatproperty to control this behaviour. The value can be any one of the following:
repeat-x- The background image is only repeated horizontally.repeat-y- The background image is only repeated vertically.no-repeat- The background image is only displayed once.
background-repeat: repeat-x; - Background-position ~ If a background image is declared and it is set to repeat only horizontally or vertically or not at all (see background-repeat) then you can use
background-positionto define exactly where the background image is displayed on your web page.
Typically, two values are used — the first defining the horizontal position and the second defining the vertical position. Some of the values you may use are named positions, percentages or pixels (px).
NAMED POSITIONS
Named positions for horizontal positioning are:left|center|right
Named positions for vertical positioning are:top|center|bottom
PERCENTAGES AND PIXELS
When using percentages or pixels, the first value defines the horizontal distance from the left border while the second value defines the vertical distance from the top border.
Examples:
background-position: 0% 50%;
(The background image is displayed flush with the left border and in the vertical center.)
background-position: 50% 100%;
(The background image is displayed in the horizontal center and flush with the bottom border.)
background-position: 20px 40px;
(The background image is displayed 20 pixels from the left border and 40 pixels from the top border.)
background-position: right bottom;
(The background image is displayed in the bottom right corner.)
- Background-attachment ~ If you're starting to become concerned about cross-browser compatibility and you've discovered that using
bgproperties="fixed"in the<body>tag doesn't work in Firefox (or any Gecko-based browser for that matter) then here comes the CSS propertybackground-attachmentto the rescue. This can be used to make the background image remain fixed in place while the rest of the web page content scrolls over it.
Possible values are:fixed|scroll
Example:
<body style="background-attachment: fixed;" background="example.jpg">
Below is an example of an inline style applied to a paragraph tag using
background shorthand property described above (height and padding are also defined for demonstrational purposes):<p style='background: orange url("sunset.jpg") no-repeat 0px 20px; height: 100px; padding: 20px;'>It was a beautiful sunset...</p>
Note that in the inline style above, we're obliged to nest one set of quotation marks within another to declare the path to the background image within thestyleattribute. If we used double quotation marks for bothstyle="..."andurl("..."), the web page would get messed up because thestyleattribute would end prematurely. Hence we'll opt to use single quotation marks for thestyleattribute [style=' '] and double quotation marks for the path to the background image [url(" ")].
The same inline style above could be declared as an embedded stylesheet in the document head:
<style type="text/css">
<!--
p {
background: orange url("sunset.jpg") no-repeat 0px 20px;
height: 100px;
padding: 20px;
}
-->
</style>
...and in the document body, you would simply put this:
<p>It was a beautiful sunset...</p>
Both Example 1 and Example 2 will produce the following effect:
It was a beautiful sunset...
And the lowdown on what's happening here is:
- The height of the paragraph is 100 pixels.
- The padding on all sides of the paragraph is 20 pixels.
- The background color is orange.
- The background image is "sunset.jpg" and is:
- Not repeated (only displayed once).
- Set flush with the left border (0 pixels).
- Set 20 pixels from the top border.
Next we'll cover how to apply relative and absolute positioning to your web page elements...
MORE CSS PROPERTIES:
SEE ALSO:
- How to Use CSS - An Overview
- Inline Styles
- Embedded Style Sheets
- External Style Sheets
- Class Selectors
| <~ BACK | TOP | NEXT ~> |
