CSS background-image property
Example
Set a background-image for the
element:cssbody {background-image: url("paper.gif"); background-color: #cccccc;}
Example
Set two background images for the
element:cssbody { background-image: url("img\_tree.gif"), url("paper.gif"); background-color: #cccccc;}
More "Try it Yourself" examples below.
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. Read about animatable |
| Version: | CSS1 + new values in CSS3 |
| JavaScript syntax: | object.style.backgroundImage="url(img_tree.gif)" Try it |
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 |
CSS Syntax
cssbackground-image: *url*|none|initial|inherit;
Property Values
More Examples
Example
Sets two background images for the
element. Let the first image appear only once (with no-repeat), and let the second image be repeated:cssbody { background-image: url("img\_tree.gif"), url("paper.gif"); background-repeat: no-repeat, repeat; background-color: #cccccc; }
Example
Use different background properties to create a "hero" image:
css.hero-image { background-image: url("photographer.jpg"); background-color: #cccccc; height: 500px; background-position: center; background-repeat: no-repeat; background-size: cover; }
Example
Sets a linear-gradient (two colors) as a background image for a
css#grad1 { height: 200px; background-color: #cccccc; background-image: linear-gradient(red, yellow); }
Example
Sets a linear-gradient (three colors) as a background image for a
css#grad1 { height: 200px; background-color: #cccccc; background-image: linear-gradient(red, yellow, green); }
Example
The repeating-linear-gradient() function is used to repeat linear gradients:
css#grad1 { height: 200px; background-color: #cccccc; background-image: repeating-linear-gradient(red, yellow 10%, green 20%); }
Example
Sets a radial-gradient (two colors) as a background image for a
css#grad1 { height: 200px; background-color: #cccccc; background-image: radial-gradient(red, yellow); }
Example
Sets a radial-gradient (three colors) as a background image for a
css#grad1 { height: 200px; background-color: #cccccc; background-image: radial-gradient(red, yellow, green); }
Example
The repeating-radial-gradient() function is used to repeat radial gradients:
css#grad1 { height: 200px; background-color: #cccccc; background-image: repeating-radial-gradient(red, yellow 10%, green 20%); }
Related Pages
CSS tutorial: CSS Background, CSS Backgrounds (advanced), CSS Gradients
HTML DOM reference: backgroundImage property