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-image
property 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-repeat
property is used withbackground-image
to 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-attachment
property is used withbackground-image
to 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-attachment
specifies 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-position
property is used withbackground-image
to 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-size
property, 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>
Related Articles
- Learn the Very Basics of CSS in One Minute
- How to Create a CSS External Style Sheet
- How to Align Text with CSS
- How to Create a Horizontal Navigation Menu with CSS
- How to Create a Fixed-Width Layout with CSS
- How to Remove Spacing Between Table Borders with CSS
- How to Set a Background Image with CSS (this article)
- How to Set Text Spacing and Placement in CSS
- How to Style a Table with CSS
- How to Create Boxes with Rounded Corners in CSS
- How to Create a Vertical Navigation Menu with CSS
- How to Use the CSS Opacity Property
- How to Use Multiple Background Images with CSS
- Absolute Positioning with CSS
- How to Use the CSS Border Shorthand Property
- How to Create CSS Button Links
- How to Create a Fluid-Width Layout with CSS
- How to Set Text and Background Color with CSS
- How to Create a CSS Embedded Style Sheet
- How to Add Inline Styles to CSS
- How to Create a Border with CSS
- How to Use the CSS Padding Shorthand Property
- How to Create a Fly-Out Menu with CSS
- How to Use CSS Media Queries in Responsive Design
- How to Adjust Margins with CSS
- How to Use the CSS Background Shorthand Property
- How to Create a Form without Tables Using CSS
- How to Modify Fonts in CSS
- How to Create a Drop-Down Menu with CSS
- How to Apply Padding with CSS
- Fixed Positioning with CSS
- How to Use CSS Transitions
- How to Use the CSS list-style Shorthand Property
- How to Change Text Style in CSS
- How to Create CSS Sprites
- How to Use CSS with Different Media Types
- How to Import Style Sheets with @import in CSS
- How to Use the CSS White-Space Property
- How to Use the CSS Z-index Property
- How to Create Drop Shadows with the box-shadow Property in CSS3