CSS background-colorOpacity / TransparencyCSS background-imageCSS background-repeatCSS background-positionCSS background-attachmentCSS background - Shorthand propertyCSS background-sizeCSS background-origin
background-colorproperty specifies the background color of an element.
With CSS, a color is most often specified by:
- a valid color name - like "red"
- a HEX value - like "#ff0000"
- an RGB value - like "rgb(255,0,0)"
opacityproperty specifies the opacity/transparency of an element. It can take a value from 0.0 - 1.0. The lower value, the more transparent.
Note: When using the
opacityproperty to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read.
background-imageproperty specifies an image to use as the background of an element. By default, the image is repeated so it covers the entire element.
By default, the
background-imageproperty repeats an image both horizontally and vertically. Some images should be repeated only horizontally or vertically, or they will look strange.
To repeat an image vertically, set
To repeat an image horizontally, set
To not repeat an image, set
background-positionproperty is used to specify the position of the background image.
background-attachmentproperty specifies whether the background image should "scroll" or be "fixed" (will not scroll with the rest of the page).
To shorten the code, it is also possible to specify all the background properties in one single property. You can use the shorthand property
When using the shorthand property the order of the property values is:
Ex: background: #ffffff url("img_tree.png") no-repeat right top;
It does not matter if one of the property values is missing, as long as the other ones are in this order. Note that we do not use the background-attachment property in the example above, as it does not have a value.
background-sizeproperty specifies the size of the background images.
There are four different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated with comma).
background-originproperty specifies the origin position (the background positioning area) of a background image.
- padding-box - Default value. The background image starts from the upper left corner of the padding edge
- border-box - The background image starts from the upper left corner of the border
- content-box - The background image starts from the upper left corner of the content
Note: This property has no effect if background-attachment is "fixed".