Jegsworks: Free Online Computer Lessons Logo:Jegsworks Jan's Illustrated Computer Literacy 101


Home > Resources & Examples > Jan's CSS > Backgrounds
Icon: Arrow - Previous page Previous     NextIcon: Arrow - Next page

Jan's CSS: Backgrounds

With CSS we can now add a background color or image to any HTML element. It can be quite useful to apply a background to a DIV element, whole table, table cell, or SPAN tag.

A background can be a color or an image or both. If the image has transparent parts, the background color will show through. You must decide whether or not you want the image to tile across the area entirely or just one line across or one line down or centered. These characteristics can be set separately or in a combined BORDER property.

Colors can be set with HTML color code #rrggbb, RGB color code rgb(rrr,ggg,bbb), or a named color.

Examples: Background Color

This paragraph has a background color with a different background in this SPAN.

CSS Syntax:

The example below shows the three choices for how to set background color to the light blue showing behind the code examples.

HTML code: background-color: #F0F8FF;
RGB value: background-color: rgb(240, 258, 255);
Name:      background-color: aliceblue;

Examples: Background Image

The default is to tile an image across the whole page. You usually want to use an image that creates a smooth effect, like of clouds or water or fabric. The example below uses a leaf so that it is easy to see what is happening. A centered image can be used as a watermark.





Tiled

 
 
 
Tiled only across.

 
 
 
Not tiled at all.
background:
url(leaf.gif);
background: url(leaf.gif) repeat-x; background: url(leaf.gif) no-repeat;


Tiled down the left


Centered over a colored background.


Centered at the right.
background:
url(leaf.gif)
repeat-y;
background: #b4c29a url(leaf.gif) center no-repeat; background: url(leaf.gif) center right no-repeat;

Image of the results:

Image of the results of the examples

More examples of background Icon: Page from another part of the site in action.