HTML Cookbook Basic HTML Course

It's Easy!

CSS Step By Step


Alternate Version

Background Repeat

In our last lesson, we learned how to display background images with CSS in web pages. Now we will learn how to control how a background image repeats.

In straight HTML, background images automatically tile (repeat) to fill up a whole webpage.

With CSS, you can control a background image so it repeats horizontally, vertically, both, or not at all.


Background Repeat Declarations

The style statements to control how a background image repeats are:

background-repeat : repeat
background-repeat : repeat-x
background-repeat : repeat-y
background-repeat : no-repeat

What do the values do?

repeat repeats the image downward and to the right from the starting position (image fills the whole page or element)

repeat-x repeats the image to the right of the starting position (image tiles horizontally)

repeat-y repeats the image downward from the starting position (image tiles vertically) - this can be used with border backs

no-repeat keeps the background image form repeating - you will only see the image itself once (no tiling at all)


How do I use it?

You can use background-repeat in an element declaration, as a class or with the style attribute:

Declaration (stars across the top of a page)
body {
background-image:url(stars.gif);
background-repeat: repeat-x;
}


Class (apply to an element on your page)
.stars {
background-image:url(stars.gif);
background-repeat: repeat-x;
}


Style Attribute (add to a tag on your page)
style="background-image:url(stars.gif);  background-repeat: repeat-x;"

Examples

I made a separate page showing background=repeat:repeat-x.

I also made a separate page that shows how to use background-repeat : repeat-y to keep border background images from tiling on wide monitors.


WebTV Limitation

Background repeat can be seen on pc's for any element, but WebTV only supports background-repeat in the body element. This means that background images will tile in div's and paragraphs when viewed on WebTV regardless of background-repeat.

Assignment

Make a sample page and use background-repeat to control a background image.

Our Mantra:
I love CSS!
CSS is my friend!
I AM the mighty creator!

Happy coding!
Gnubee

Links
Tips
Tools
References
Rules
HTML Help Gnubee - Newsgroup
HTML Course Contents (Home)

Valid XHTML 1.0!

Valid CSS!