How to Set a Background Image with CSS
See CSS: Tips and Tricks for similar articles.
You can set background images in CSS using the background-image and several other properties to control the behavior of the image. Background properties include:
- background-repeat
- background-attachment
- background-position
- background-size
The following steps show how to use each of these properties and become familiar with their values. In our examples, we enclose the style information in the ;<div;> tag.
- The
background-imageproperty is used to specify the background image of an element. It can be applied to block elements and inline elements.<div style="height: 200px; width: 500px; color: #ffffff; background-image: url(Images/block.gif);"> background-image: url(Images/block.gif); </div>This code renders the following when used in a full HTML document:

- The
background-repeatproperty is used withbackground-imageto specify whether and how a background image should repeat. Possible values are:- no-repeat – does not tile
- repeat-x – tiles horizontally
- repeat-y – tiles vertically
The following shows sample code that tiles the image horizontally:
<div style="height: 200px; width: 500px; color: #ffffff; background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: repeat-x"> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: repeat-x; </div>This code renders the following when used in a full HTML document:

- The
background-attachmentproperty is used withbackground-imageto specify whether a background image should scroll as the content is scrolled or whether the content should scroll over it. According to the specification,background-attachmentspecifies whether a background image is fixed relative to the viewport (e.g., the browser window) or scrolls along with the document. Possible values are:- scroll
- fixed
Note that this code has a lot of repeated text to enable the scroll feature to appear on the window.
<div style="height: 200px; width: 500px; color: #ffffff; overflow: scroll; white-space: pre; background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-attachment: scroll;"> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-attachment: scroll;<br> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-attachment: scroll;<br> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-attachment: scroll;<br> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-attachment: scroll;<br> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-attachment: scroll;<br> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-attachment: scroll; </div>This code renders the following when used in a full HTML document. Notice that the scroll bar on the right side is all the way at the bottom, yet the image is still in the upper-right corner:

- The
background-positionproperty is used withbackground-imageto specify the location of a background image. Possible values are:- top
- right
- bottom
- left
- center
- any combination of the above (e.g., top center)
The following code renders the background image on the right:
<div style="height: 200px; width:500px; color: #ffffff; background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: repeat-y; background-position: right;"> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: repeat-y; background-position: right; </div>The code looks like this when used in a full HTML document:

- CSS3 introduces the
background-sizeproperty, which allows you to control the size of the background image as displayed in its parent element (the element for which the image serves as the background). You may use any of the following as values for background-size:- auto (the default value)
- a length, setting the width and height of the background image (in any valid CSS length units); e.g., background-size:20px 40px.
- a percentage, setting the width and height as a percentage of the parent element; e.g., background-size:50% 50%.
- cover, scaling the background image to be as large as possible so that the area of the element is completely covered by the background image; some part of the image may not be shown.
- center
- contain, scaling the background image to the largest size so that its width and height fit inside the element.
This code sets the background image to a percentage (10% and 90%):
This code renders the following when used in a full HTML document:<div style="height: 200px; width: 500px; color: #ffffff; background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-size: 10% 90%"> background-color: #ff6600; background-image: url(Images/block.gif); background-repeat: no-repeat; background-size: 10% 90%; </div>
