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.

  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

Related Articles

  1. Learn the Very Basics of CSS in One Minute
  2. How to Create a CSS External Style Sheet
  3. How to Align Text with CSS
  4. How to Create a Horizontal Navigation Menu with CSS
  5. How to Create a Fixed-Width Layout with CSS
  6. How to Remove Spacing Between Table Borders with CSS
  7. How to Set a Background Image with CSS (this article)
  8. How to Set Text Spacing and Placement in CSS
  9. How to Style a Table with CSS
  10. How to Create Boxes with Rounded Corners in CSS
  11. How to Create a Vertical Navigation Menu with CSS
  12. How to Use the CSS Opacity Property
  13. How to Use Multiple Background Images with CSS
  14. Absolute Positioning with CSS
  15. How to Use the CSS Border Shorthand Property
  16. How to Create CSS Button Links
  17. How to Create a Fluid-Width Layout with CSS
  18. How to Set Text and Background Color with CSS
  19. How to Create a CSS Embedded Style Sheet
  20. How to Add Inline Styles to CSS
  21. How to Create a Border with CSS
  22. How to Use the CSS Padding Shorthand Property
  23. How to Create a Fly-Out Menu with CSS
  24. How to Use CSS Media Queries in Responsive Design
  25. How to Adjust Margins with CSS
  26. How to Use the CSS Background Shorthand Property
  27. How to Create a Form without Tables Using CSS
  28. How to Modify Fonts in CSS
  29. How to Create a Drop-Down Menu with CSS
  30. How to Apply Padding with CSS
  31. Fixed Positioning with CSS
  32. How to Use CSS Transitions
  33. How to Use the CSS list-style Shorthand Property
  34. How to Change Text Style in CSS
  35. How to Create CSS Sprites
  36. How to Use CSS with Different Media Types
  37. How to Import Style Sheets with @import in CSS
  38. How to Use the CSS White-Space Property
  39. How to Use the CSS Z-index Property
  40. How to Create Drop Shadows with the box-shadow Property in CSS3