How to Set a Background Image with CSS

  • google plus

In Brief...

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.

Take our Introduction to CSS Training course for free.

See the Course Outline and Register

Instructions

  1. 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:
    Background Image

  2. The background-repeat property is used with background-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:
    Background Repeat

  3. The background-attachment property is used with background-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:
    Background Attachment

  4. The background-position property is used with background-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:
    Background Right

  5. 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%):

    <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>
    This code renders the following when used in a full HTML document:
    Background Size

Author: Chris Minnick

Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.

Discuss