CSS background-image Property

Example

Set a background-image for the <body> element:

body {
 background-image: url("blue.jpg");
 background-color: #0054ff;
}

Example

Set two background images for the <body> element:

body {
  background-image: url("img_rose.png"), url("blue.jpg");
  background-color: #0054ff;
}

Definition and Usage

The background-image property sets one or more background images for an element.

By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.

Tip: The background of an element is the total size of the element, including padding and border (but not the margin).

Tip: Always set a background-color to be used if the image is unavailable.

Default value: none
Inherited: no
Animatable: no.
Version: CSS1 + new values in CSS3
JavaScript syntax: object.style.backgroundImage="url(img_tree.gif)"

  • Inherited : "Inherited = no" means that it cannot takes (inherit) it's value from it's parent element.
  • object - object in javascript means the element on which background-image is applied.
  • Animatable - "Animatable = no" means that it cannot be animated with CSS @keyframes.

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property          
background-image 1.0 4.0 1.0 1.0 3.5

Note: IE8 and earlier do not support multiple background images.

CSS Syntax

background-image: url|none|initial|inherit;

Property Values

Value Description Demo
url('URL') The URL to the image. To specify more than one image, separate the URLs with a comma
none No background image will be displayed. This is default  
conic-gradient() Sets a conic gradient as the background image. Define at least two colors
linear-gradient() Sets a linear gradient as the background image. Define at least two colors (top to bottom)
radial-gradient() Sets a radial gradient as the background image. Define at least two colors (center to edges)
repeating-conic-gradient() Repeats a conic gradient
repeating-linear-gradient() Repeats a linear gradient
repeating-radial-gradient() Repeats a radial gradient
initial Sets this property to its default value.  
inherit Inherits this property from its parent element.  

More Examples

Example

Sets two background images for the <body> element. Let the first image appear only once (with no-repeat), and let the second image be repeated:

body {
  background-image: url("img_rose.png"), url("blue.jpg");
  background-repeat: no-repeat, repeat;
  background-color: #0054ff;
}

Example

Use different background properties to create a "hero" image:

.hero-image {
  background-image: url("road.jpg"); /* The image used */
  background-color: #888; /* Used if the image is unavailable */
  height: 500px; /* You must set a specified height */
  background-position: center; /* Center the image */
  background-repeat: no-repeat; /* Do not repeat the image */
  background-size: cover; /* Resize the background image to cover the entire container */
}

Example

Sets a linear-gradient (two colors) as a background image for a <div> element:

#grad1 {
  height: 200px;
  background-color: #0054ff;
  background-image: linear-gradient(#0094ff, #0054ff);
}

Example

Sets a linear-gradient (three colors) as a background image for a <div> element:

#grad1 {
  height: 200px;
  background-color: #0054ff;
  background-image: linear-gradient(#0094ff, #0054ff, #0000ff);
}

Example

The repeating-linear-gradient() function is used to repeat linear gradients:

#grad1 {
  height: 200px;
  background-color: #0054ff;
  background-image: repeating-linear-gradient(#0094ff, #0054ff 10%, #0000ff 20%);
}

Example

Sets a radial-gradient (two colors) as a background image for a <div> element:

#grad1 {
  height: 200px;
  background-color: #0054ff;
  background-image: radial-gradient(#0094ff, #0054ff);
}

Example

Sets a radial-gradient (three colors) as a background image for a <div> element:

#grad1 {
  height: 200px;
  background-color: #0054ff;
  background-image: radial-gradient(#0094ff, #0054ff, #0000ff);
}

Example

The repeating-radial-gradient() function is used to repeat radial gradients:

#grad1 {
  height: 200px;
  background-color: #0054ff;
  background-image: repeating-radial-gradient(#0094ff, #0054ff 10%, #0000ff 20%);
}

Comments