4.10 Background

CSS background-color

The 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

The 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.

CSS background-image

The 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.

CSS background-repeat

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 background-repeat: repeat-y;
To repeat an image horizontally, set background-repeat: repeat-x;
To not repeat an image, set background-repeat: no-repeat;

CSS background-position

The background-position property is used to specify the position of the background image.

CSS background-attachment

The 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 background.
When using the shorthand property the order of the property values is:
  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position
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.

CSS background-size

The 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).

CSS background-origin

The 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".
notion image
Copyright ┬ę 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.