A background image can be specified for almost any HTML element.
Background Image on a HTML element
To add a background image on an HTML element, use the HTML style
attribute and the CSS background-image
property:
Example
Add a background image on a HTML element:
Try it Yourself »
You can also specify the background image in the element, in the
section:
Example
Specify the background image in the element:
p {
background-image: url['img_girl.jpg'];
}
Try it Yourself »
Background Image on a Page
If you want the
entire page to have a background image, you must specify the background image on the element:
Example
Add a background image for the entire page:
body {
background-image: url['img_girl.jpg'];
}
Try it Yourself »
Background Repeat
If the background image is smaller than the element, the image will repeat itself, horizontally and vertically, until it reaches the end of the element:
Example
body {
background-image: url['example_img_girl.jpg'];
}
Try it Yourself »
To avoid the background image from repeating itself, set the background-repeat
property to no-repeat
.
Example
body {
background-image: url['example_img_girl.jpg'];
background-repeat: no-repeat;
}
Try it Yourself »
Background Cover
If you want the background image to cover the entire element, you can set the background-size
property to cover.
Also, to make sure the entire element is always covered, set
the background-attachment
property to fixed:
This way, the background image will cover the entire element, with no stretching [the image will keep its original proportions]:
Example
body {
background-image: url['img_girl.jpg'];
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
Try it Yourself »
Background Stretch
If you want the background image to stretch to fit the entire element, you can set the background-size
property to 100% 100%
:
Try resizing the browser window, and you will see that the image will stretch, but always cover the entire element.
Example
body {
background-image:
url['img_girl.jpg'];
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
}
Try it Yourself »
Learn More CSS
From the examples above you have learned that background images can be styled by using the CSS background properties.
To learn more about CSS background properties, study our CSS Background Tutorial.
Example
Set a background-image for the element:
body {
background-image: url["paper.gif"];
background-color: #cccccc;
}
Try it Yourself »
Example
Set two background images for the element:
body {
background-image: url["img_tree.gif"],
url["paper.gif"];
background-color: #cccccc;
}
Try it Yourself »
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.
Show demo ❯
none |
no |
no. Read about animatable |
CSS1 + new values in CSS3 |
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.
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
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:
body {
background-image: url["img_tree.gif"],
url["paper.gif"];
background-repeat: no-repeat, repeat;
background-color: #cccccc;
}
Try it Yourself »
Example
Use different background properties to create a "hero" image:
.hero-image {
background-image: url["photographer.jpg"]; /* The image used */
background-color: #cccccc; /* 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 */
}
Try it Yourself »
Example
Sets a linear-gradient [two colors] as a background image for a
#grad1 {
height: 200px;
background-color: #cccccc;
background-image: linear-gradient[red, yellow];
}
Try it Yourself »
Example
Sets a linear-gradient [three colors] as a background image for a
#grad1 {
height: 200px;
background-color: #cccccc;
background-image: linear-gradient[red, yellow, green];
}
Try it Yourself »
Example
The repeating-linear-gradient[] function is used to repeat linear gradients:
#grad1 {
height: 200px;
background-color: #cccccc;
background-image:
repeating-linear-gradient[red, yellow 10%, green 20%];
}
Try it Yourself »
Example
Sets a radial-gradient [two colors] as a background image for a
#grad1 {
height: 200px;
background-color: #cccccc;
background-image: radial-gradient[red, yellow];
}
Try it Yourself »
Example
Sets a radial-gradient [three colors] as a background image for a
#grad1 {
height: 200px;
background-color: #cccccc;
background-image: radial-gradient[red, yellow, green];
}
Try it Yourself »
Example
The repeating-radial-gradient[] function is used to repeat radial gradients:
#grad1 {
height: 200px;
background-color: #cccccc;
background-image: repeating-radial-gradient[red, yellow 10%, green 20%];
}
Try it Yourself »
Related Pages
CSS tutorial: CSS Background, CSS Backgrounds [advanced], CSS Gradients
HTML DOM reference: backgroundImage property