eHow launches Android app: Get the best of eHow on the go.

How To

How to Style a Background Image with CSS

Contributor
By Virginia DeBolt
eHow Contributing Writer
(14 Ratings)
This image could become a page background.
This image could become a page background.

The CSS background-image property can add style, color and design elements to an entire web page or to a small part of a web page. Here's how you achieve it.

Difficulty: Moderately Challenging
Instructions

Things You'll Need:

  • A small image meant for the element you want to have a background-image.

    Add a Background Image to an Entire Web Page

  1. Step 1

    Save a low opacity, less saturated copy of your page background image. The page background should be unobtrusive and not interfere with the contents of the page.

  2. Step 2

    A background can repeat or be set to no-repeat. To make a whole page background of the bales of hay shown above, the best choice is no-repeat. For a whole page background, a large image that is centered both horizontally and vertically on the page with background-position works well.

  3. Step 3
    A browser display of the CSS rule in Step 3.
    A browser display of the CSS rule in Step 3.

    Apply the rule to the body selector to create a whole page background. Here's an example using the settings suggested in Step 2.

    body {
    background-image: url(somebg.jpg);
    background-repeat: repeat;
    background-position: center center;
    }

  4. Step 4

    Adjust the background-position according to your image. For example:

    background-position: left top;

    might be a better choice for some images. That rule places the image horizontally on the left, and vertically at the top.

  5. Add a Background Image to an Individual Web Page Element

  6. Step 1

    With CSS, anything can have a background image: a list, a div, a paragraph, a heading a span. Anything.

  7. Step 2

    Depending on what element you choose for a background image, it can be used as a design element, a navigation aid, to bring contrast or attention to the element or to define a part of a page.

  8. Step 3
    A small section of the hay field can be used as a background for a page element
    A small section of the hay field can be used as a background for a page element

    Use your image editing program to create an image based on your needs and purposes as suggested in Step 2. As an example, here is a small bit of the hay field from the large image.

  9. Step 4
    In a browser, the h1 would look like this.
    In a browser, the h1 would look like this.

    As an example, the image will be used as a background for an h1 element. Here's a possible CSS rule:

    h1 {
    background-image: url(h1bg.jpg);
    background-repeat: repeat;
    background-position: center center;
    }

Tips & Warnings
  • Many other CSS rules can be added to the h1 selector, such as width, font-size, border, padding and so on.
  • A large background image may take a long time to download.

Comments  

mymukki said

Flag This Comment

on 8/7/2009 this will certainly improve a lot of sites!

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Demand Media
eHow_eHow Technology and Electronics