- CSS background-color
- Opacity / Transparency
- CSS background-image
- CSS background-repeat
- CSS background-position
- CSS background-attachment
- CSS background - Shorthand property
- CSS background-size
- CSS background-origin
- Previous Section
- Next Section
background-color property 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)"
Opacity / Transparency
opacity property 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
opacity property 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-image property 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-image property 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-position property is used to specify the position of the background image.
background-attachment property specifies whether the background image should "scroll" or be "fixed" (will not scroll with the rest of the page).
CSS background - Shorthand property
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-size property 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-origin property 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".