Background plays an important role in the visual presentation of a web page.
CSS provide several properties for styling the background of an element, including coloring the background, placing images in the background and managing their positioning, etc.
The background properties are background-color, background-image, background-repeat, background-attachment and background-position.
In the following section we will discuss each of these properties in more detail.
The background-color property is used to set the background color of an element.
The following example demonstrates how to set the background color of the whole page.
Color values in CSS are most often specified in the following formats:
Please check out the tutorial on CSS color to learn more about specifying color values.
The background-image property set an image as a background of an HTML
Let's check out the following example which sets the background image for the whole page.
The background-repeat property allows you to control how a background image is repeated or tiled in the background of an element. You can set a background image to repeat vertically (y-axis), horizontally (x-axis), in both directions, or in neither direction.
Let's try out the following example which demonstrates how to set the gradient background for a web page by repeating the sliced image horizontally along the x-axis.
Similarly, you can use the value repeat-y to repeat the background image vertically along the y-axis, or the value no-repeat to prevent the repetition altogether.
Let's take a look at the following illustration to understand how this property actually works.
The background-position property is used to control the position of the background image.
If no background position has been specified, the background image is placed at the default top-left position of the element i.e. at (0,0), let's try out the following example:
In the following example, the background image is positioned at top-right corner.
Besides keywords, you can also use percentage or length values, such as px or em for this property. Let's take a look at the following illustration to understand how this property actually works.
The background-attachment property determines whether the background image is fixed with regard to the viewport or scrolls along with the containing block.
Let's try out the following example to understand how it basically works:
As you can see in the examples above, there are many properties to consider when dealing with the backgrounds. However, it is also possible to specify all these properties in one single property to shorten the code or avoid extra typing. This is called a shorthand property.
The background property is a shorthand property for setting all the individual background properties, i.e., background-color, background-image, background-repeat, background-attachment and the background-position property at once. Let's see how this works:
Using the shorthand notation the above example can be written as:
When using the background shorthand property the order of the property values should be.
If the value for an individual background property is missing or not specified while using the shorthand notation, the default value for that property will be used instead, if any.