Web Design / HTML

  1. Home
  2. Computing & Technology
  3. Web Design / HTML

Stylish Backgrounds

Setting the Background Attributes with CSS

By Jennifer Kyrnin, About.com

Affecting the background of your pages, tables, and even block elements like the paragraph tags is easier than you think when using Cascading Style Sheets. Using CSS, you can:

  • set an image as a background to an element
  • indicate the tiling position of the background image
  • define the location of that image, like a watermark

background-color

usage:
background-color : <color value>;
background-color : rgb(30%,30%,30%);
background-color : transparent;
Setting the color of your background is easy, and with CSS you can now use both hexadecimal values and RGB values. Plus, you can set some elements to have a transparent background color, so that the background will allow other colors to show through. You can set the background-color on most block elements in current browsers.

Try this out in your Web page:

<p style="background-color: #cc0000; color : #cccccc; width : 200px;">
The background color of this paragraph is a dark red. It was set using a paragraph tag with the style attribute background-color:#cc0000;. e.g.
style="background-color:#cc0000;">
</p>

Examples of background-color.

Before you begin using the following style elements in your documents, you should be aware that these elements are supposed to work on any block-level elements. Be sure to test them in the browsers you support, as some browsers only support these styles on the <body> tag.

background-image

usage:
background-image : (URL);
background-image : ;
Write URL to a specific image to display as the background for your element.

Test this out by putting the following code in your document:

<p style="background-image : url(http://webdesign.about.com/library/graphics/bg-grid.gif); width : 200px;">
This paragraph uses the background-image element to create a grid pattern behind the paragraph. e.g.
style="background-image : url(http://webdesign.about.com/library/graphics/bg-grid.gif);"
</p>

Examples of background-image.

background-attachment

usage:
background-attachment : fixed;
background-attachment : scroll;
If you use a background attachment of fixed, the background image will be held in one place like a watermark. Using the scroll type will set the image to scroll with the rest of the page. This is the default and standard for background images.

Examples of background-attachment.

background-repeat

usage:
background-repeat : repeat;
background-repeat : inherit;
background-repeat : repeat-x
background-repeat : repeat-y
background-repeat : no-repeat
The default behavior of Web backgrounds is to repeat endlessly. Thus background images often have to be very large to insure that they don't repeat incorrectly on larger monitors. This tag allows you to specify that a background image might repeat only on the x or y axis, or not repeat at all.

Use this property on block-level elements as well. Examples of background-repeat.

background-position

usage:
background-position : value;
background-position: vertical horizontal;
Sets the location of the background image with one of these values:

  • top
  • bottom
  • left
  • right
  • center
  • length
  • percentage

Examples of background-position.

Once you understand all the different CSS elements you can use to affect the background of your pages and elements, you can combine them into one CSS element. The "background" element.

background

usage:
background : color url(URL) attachment position repeat;
Follow the instructions for the above elements to define your backgrounds as you would like them.

Examples of background shorthand property.

Previous Features

Explore Web Design / HTML

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Web Design / HTML

  1. Home
  2. Computing & Technology
  3. Web Design / HTML
  4. About.com Web Design A to Z
  5. Web Design Articles R-Z
  6. Web Design/HTML Articles S
  7. CSS Backgrounds Using Cascading Style Sheets to Modify Web Background Colors and Images

©2009 About.com, a part of The New York Times Company.

All rights reserved.