CSS Background
Backgrounds are very important for a website. You can define the background of an element by using the background property.
CSS background properties are given below:
- background-image - Specifies an image as a background of an element.
- background-repeat - Used to control the repetition of a background image.
- background-attachment - Used to control the scrolling of a background image.
- background-position - Used to set the position of a background image.
- background-color - Specifies the background color of an element.
- background - It is a shorthand way to declare the other background properties.
The background-image Property
It is used to set an image as background of an element.
Example:
The background-repeat Property
By default, the background images repeated both horizontally and vertically. You can control the repetition by using background-repeat property.
If you want to repeat an image horizontally you can use repeat-x value.
Example:
If you want to repeat an image vertically you can use repeat-y value.
Example:
If you don't want to repeat an image, want to use only once then you can use no-repeat value.
Example:
The background-attachment Property
You can control the scrolling of a background image using background-attachment propeerty. If you want the background image fixed when when the page is scrolling then you can use fixed value.
Example:
If you want to scroll the background image with the scrolling of the page then you can use scroll value.
Example:
The background-position Property
You can set the position of the background image by using the background-position property. You can set the background using the following values: top, bottom, center, left or right.
Example:
The background-color Property
You can define the background color using background-color property.
Example:
The background Property
It is a shorthand property. You can define all the proerties in one line by using background property. It is not compulsory to use all the properties. It is not a problem if you skip one or more properties. The order of the properties is following:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position