CSS Backgrounds

CSS background properties

The CSS background properties are used to add background effects for elements.
In these chapters, you will learn about the following CSS background properties:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position
  • background (shorthand property)
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)"

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

An external style sheet can be written in any text editor, and must be saved with a .css extension.
The external .css file should not contain any HTML tags

CSS background-repeat

By default, the background-image property repeats an image both horizontally and vertically..

CSS background-repeat: no-repeat

Showing the background image only once is also specified by the background-repeat property

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. This is called a shorthand property.

When using the shorthand property the order of the property values is:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position
It does not matter if one of the property values is missing, as long as the other ones are in this order.

Home

About

Tutorial

EBooks

Menu