The background of the webpage should not be really noticed by the viewer, unless the view is deliberately looking at it. Instead, the background's purpose is to make the website—and therefore its content—more noticeable; if the background is more noticeable than the content—or, worse, makes the text hard to read—its purpose is defeated.
The most common backgrounds are colours, although images may be used as well. Remember when I mentioned CSS URIs in CSS Value Types? This is their most common use.
The background properties are as follows:
The last, of course, is the shorthand property.
Property: | background-color |
---|---|
Default value: | transparent |
Inherited: | No |
The background-color
uses the colour codes as explained in the chapter on CSS Value Types with one addition: It also has a value called transparent
, which allows for a clear background.
Another option for backgrounds is the image. These can be used in conjunction with background-color
.
Property: | background-image |
---|---|
Default value: | none |
Inherited: | No |
This property sets what image you actually use; the value is the URI of the image. Please remember: when you are using a relative URI, the path will be in relation to the stylesheet, rather than the (X)HTML document.
Below is a stylesheet of an element with a background image.
And the result:
Property: | background-repeat |
---|---|
Default value: | repeat |
Inherited: | No |
The background-repeat
attribute allows you to control how your background image repeats itself. It has the following values:
repeat-x
Property: | background-position |
---|---|
Default value: | 0% 0% (top right) |
Inherited: | No |
The property background-position
displays where the background will be. It can be set in a number of ways:
Now you can use either one or two values for this. If only one value is specified, that is used for horizontal positioning, while the background is centered vertically (with two exceptions, both keywords).
There are 5 keywords that set a backgrounds position:
The values top
and bottom
are the centering exceptions I mentioned earlier: If these values are the only values for the background-position
property, the background is centered horizontally instead.
The center
value can be used for both horizontal and vertical positioning; used as the sole value, it will center the background both horizontally and vertically.
By the way, the values left
/right
and top
/bottom
may be written in any order. However, if you are using a number as well as a keyword, left
/right
should come first and the number second, or the number first and top
/bottom
second.
In numerical positioning, the first number specifies the horizontal position, the second (if present) specifies the vertical position (if absent, the background is centered vertically). Just like in image maps, x and y coördinates come into play.
You may use negative values in this case.
This sets the position of your background as a percentage of the width of the containing element.
Percentage | Horizontal position | Vertical position |
---|---|---|
0% | Far left | Top |
50% | Center | |
100% | Far right | Bottom |
You don't need to stick to the above, of course. You may use any percentage you please—even values lower than 0% or higher than 100%, in which case you start moving your background out of the element altogether.
This uses an actual measurement to place the background. I explained length quite thoroughly in the chapter on CSS Value Types, but I will explain a couple of things:
Property: | background-attachment |
---|---|
Default value: | scroll |
Inherited: | No |
This last one controls what the background does when you scroll the page up or down. It has two values:
Property: | background |
---|---|
Inherited: | No |
The background
property allows you to set all the above properties in a single declaration.
Please note that the two position values must be side-by-side, otherwise the browser won't understand the declaration.
Remember when I said that the :hover
pseudo-class could cause an image to change if the mouse cursor were moved over the image? Here's how you do it:
Because of spotty support for the pseudo-classes in older browsers, it works best with the a
element, but you can use the :hover
pseudoclass with almost any element.
To demonstrate such rollover effects, I've created a page with a blue-coloured square. When I move my mouse cursor over it, the image changes to that of cyan-coloured square.
The page has the following stylesheet:
Pretty simple, isn't it? The display
, height
, and width
are all discussed in Arranging The Page, and allows the a
element to be displayed as a block element and have the height and width of the image.
One problem with rollover images is that the image may take a moment to download— especially if your page is online. One way is to preload
the images by including them elsewhere in the page but hiding them.
If you think you see a second rollover effect affecting the text following the image, that's because there is one. When I hover the mouse over the a
element, it will not only change the background image, but the content of the :after
pseudo-element associated with the a
element. Here's the code, and it's at the bottom of the style sheet:
I'll go more into depth on CSS-Generated Content later.