Background – CSS

title: Background


The background property lets you use images and colors to create backgrounds for your web pages.

Background Color

The background-color property allows you to choose the color of your element (a piece of HTML such as a header or paragraph). This can be the background for the entire page or the background of one section of your page.

Here is an example of setting the background color for a web page to green.

body { background-color: green; }

Here is an example of setting the background colors for two elements. This will set the background of the header to purple and the rest of the page to blue.

body { background-color: blue; } h1 { background–color: purple; }

In CSS color can be defined in the following ways:

  • A valid “color” keyword name such as blue
  • Numerical color values
  • A HEX value such as #FFFFF (This is the hex value for white.)
  • An abbreviated HEX value such as #FFF.
  • An RGB value such as rgb(76,175,80) (This is the RGB value for light green.)
  • RGBA value (RGB + alpha channel for contolling opacity)
    • Note: The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque)
  • HSL (Hue, Saturation, Lightness) (e.g., hsl(115, 75%, 73%) is HSL for light green)
  • HSLA (HSL + alpha channel for controlling opacity)

Background Images

You can use the background-image property to set an image as a background for an element.
The image is repeated by default so that it covers the entire element.

body { background-image: url("barn.jpg"); }

You can also use pictures or gifs that you find online by using their link (e.g., from a Google Images search).

body { background-image: url(""); }

Background Image – The Repeat Property

The background image is repeated both vertically (up and down) and horizontally (across the web page) by default.
You can use the background-repeat property to repeat the image vertically or horizontally.

Here is an example that repeats the image vertically.

body { background-image: url("barn.jpg"); background-repeat: repeat-y; }

You can repeat the image horizontally by setting the background-repeat property to repeat-x.

body { background-image: url("barn.jpg"); background-repeat: repeat-x; }

You can also use the background-repeat property to set an image to not repeat.

body { background-image: url("barn.jpg"); background-repeat: no-repeat; }

Background Image – The Position Property

You can use the background-position property to specify where your image will be located on a web page.

body { background-image: url("barn.jpg"); background-repeat: no-repeat; background-position: right top; }

Background Image – The Fixed Position

You can use the background-attachment property to set an image to a fixed position.
A fixed position makes it so an image does not scroll with the rest of the page.

body { background-image: url("barn.jpg"); background-repeat: no-repeat; background-position: right top; background-attachment: fixed; }

Background Gradients

A gradient is a transition between two or more colors and can be used via CSS similar to a background image.

The syntax of a gradient background can be quite complex and is often still used with vendor prefixes due to inconsistencies between supported browsers.

Below is an example of a background gradient:

background gradient example image
header { background-image: linear-gradient(to right, #069, #099); }

The Colorzilla Gradient Editor is a great online tool for generating custom gradients and the associated css markup.

Background – The Shorthand Property

You can write the background properties on a single line. This is called the shorthand property.

body { background: url("barn.jpg") no-repeat right top; }

You can leave out properties you don’t need when using the shorthand property, but the properties
must be used in a certain order. The order is:

  • color
  • image
  • repeat
  • attachment
  • position

Multiple Background Images

You can specify multiple background images in a single background property.

body { background: url("barn.jpg"), url("stars.jpg"), linear-gradient(rgba(0, 0, 255, 0.5), rgba(255, 255, 0, 0.5)); }

The first image (or gradient) specified is the most on top, the second comes after, and so on.
If one of the elements is not correct due to its URL or its syntax, the whole line will be ignored by the browser.

Other Resources

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.