
Backgrounds
We have seen how to set up the basic web page, and how to add text and font effects, now we can add some color to it via a background image and/or background color.
We add the codes for the background image and/or background color in the <body> tag.
For a background color, we use the 'bgcolor=' and for a patterned background we use 'background='.
This is the HTML method, the XHTML method is via CSS
<body bgcolor="#ffffff"> - gives you a colored background (RGB colors)
<body bgcolor="#000000" background="pattern.jpg"> - gives you a patterned background. (Tiled image)
Note: We should always use the bgcolor when we want a patterned background incase the background doesn't display we see the bgcolor. The pattern will always over ride the bgcolor tag.
The tags and tag names should be lower case while the RGB color can be upper or lower case, but the 0 is a number, not the letter.
| Here is an example of how a pattern loads without a bgcolor, it tiles or repeats itself over and over until the page is covered, this happens so fast you wouldn't actually see it happening. |
For this pattern to be used a light font color would have to be used, like white or grey, if the background didn't load or took forever to load you wouldn't see any text. If the bgcolor was set to black (#000000) or dark blue (#000080) and font set to white (#FFFFFF) we would still be able to read the page while the background was loading.
The #number is the RGB color, we use these to tell the browser which color we want, there are 216 browser safe colors (Which can be found on the "Color chart" page), the # has to be included before the RGB code, just like we used with the font tags.
RGB is what colors are made out of - Red, Green, Blue
Background images tile through out the page, to make a side bordered background it's best to make the image at least 40 pixels high by at least 1300 pixels wide, remember we are making this for ALL size screens.
Animated backgrounds aren't advised as they make it hard to read, plus are usually slow in loading, remember it's the content of your site that you want people to see, not what fancy graphics you could find on the web. Some people surf with graphics turned off, so don't rely on your background or graphics for anything important. We'll see more on this later.
Fixed background images can be fun. What is a fixed background? It's a background that's fixed :), it just sits there while the contents of the page move up and down which can give a nice effect if the background image is suitable. Never use 60's style patterned backgrounds with this trick as it makes reading the content hard. This is sort of going against what I want to do, make it work on all browsers ... But here is a little trick for the Internet Explorer AND Netscape fans, a fixed background, Netscape doesn't support Internet Explorer's version, so a Style sheet (CSS) is needed to do the same. This works on most versions of the above mentioned browsers but not all. Here is the code for the fixed background:
<html>
<head>
<title>title goes here</title>
<style type="text/css">
<!--
body {background-color:#ffffff; background-image: url("yourbg.jpg"); background-attachment: fixed; }
-->
</style>
</head>
<body>
page contents here
</body>
</html>
This works in version 5 and up, NS4.7 will display the background image but wont fix it. I should point out this is just one of the many "thrilly" things that can be done with HTML, it has no real purpose.
To set up a left bordered background the best way is to use a table with an invisible spacer with a pre-made image or we can just color the left table cell (see the Tables section for more on this).
Page contents go here, it may look a wide gap until you add your content, which will align it to your desired width. |
This is the code you need, but will need to be changed to suit your border size, I'll show you how in a second.
<html>
<head>
<title>Your title</title>
</head>
<body bgcolor="??????" background="???.???">
<table border="0" width="100%" cellspacing="0" cellpadding="0"><tr>
<td><img border="0" width="110" height="2" src="clearspacer.gif"></td>
<td> Page contents go here</td>
</tr></table>
</body>
</html>
You'll notice we have used two widths in this table, the first one in the 'table' tag tells the browser the table is going to be 100% (which fits any size screen). Next we have the td (table data) width telling the browser this cell is going to be a fixed size of (whatever you choose).
Now to set the right width you simply set the border to 1, like so, <table border="1" ...
and adjust the <img width> to suit, when you have it the right width
you change the table border back to 0, <table border="0" ....
Here is an example
width=40
Page contents go here, it may look a wide gap until you add your content, which will align it to your desired width. |
width=100
Page contents go here, it may look a wide gap until you add your content, which will align it to your desired width. |
width=75
Page contents go here, it may look a wide gap until you add your content, which will align it to your desired width. |
A small note, make sure the graphic you are using is over 1300 wide or the background will repeat on itself.
And never use the % for the side border. Make your table 100% and your side cell whatever size you need in pixels (numbers) and it will fit any screen size the same.
To add text, links or graphics (must be within the set width) you can add them in the left table cell like so:
LINK IMAGE LINK ETC |
Page contents go here, it may look a wide gap until you add your content, which will align it to your desired width. |
Which is like this is HTML:
<table border="1" height="50" width="100%" cellspacing="0" cellpadding="0"><tr>
<td bgcolor="#000000" background="yourbackground.jpg"><font color="#ffffff"><img border="0" width="100" height="1" src="clearspacer.gif">
<br><br>LINK<br>IMAGE<br><br>LINK<br>ETC</font></td>
<td><font face="Verdana" size="-1"> Page contents go here, it may look a wide gap until you add your content, which will align it to your desired width.</font></td>
</tr></table>
This page your seeing now is sort of a side bordered set up, I have just used a few more table cells and rows. More on Tables in the "Tables" section
Like side bordered backgrounds, top bordered backgrounds can be done several ways, the easiest is to use a similar set up to the side bordered background but up and down, not side to side. Number one rule, make sure the background graphic is long enough or it repeats on itself (if not use it as a table cell background only, like the example shown). One problem doing it this way is that the table will have a gap around it (which is the default and is different on each browser), we can loose this gap by adding topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" to the body tag like so:
<body bgcolor="#??????" background="?????.???" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
(Graphic would be up here) |
| All your text goes anywhere in here. |
<table border="0" width="100%" cellspacing="0" cellpadding="0"><tr>
<td><img border="0" width="1" height="50" src="clearspacer.gif"></td>
</tr><tr>
<td>All your text goes anywhere in here.</td>
</tr></table>
To add text, graphics (extra), links etc, we add them between the first set of <td> </td> tags.
You can use the <blockquote> </blockquote> tags to take the text off the border or you could simply make the left hand side table or top table cell 5 or 10 pixels wider.
More on Tables in the "Tables" section
Triple layer backgrounds (Opens in new window)
Which in code form looks like:
<html>
<head>
<title>Title of your page</title>
</head>
<body bgcolor="#??????" background="???.???">
<br /><br /><br />
<table width="90%" height="80%" cellpadding="40" cellspacing="0" border="1" bgcolor="#??????" align="center"><tr>
<td width="100%"><br /><br />
<table width="100%" height="100%" border="1" bgcolor="#??????" background="??.???" align="center"><tr>
<td width="100%">
Your Content Goes Here
</td>
</tr></table>
</td>
</tr></table>
</body>
</html>
A little warning, Netscape doesn't like you using a color-graphic-color, it will show up as color-graphic-graphic combination. The best way around this that I have found is to make a 40 x 40 graphic of the color you wish to use, and use that as a background image instead of the bgcolor. The colored graphic will tile so no one will know you are using it.
Common errors
- Typos are always a problem
- Missing the # off a RGB colour can cause it not to display
- Typing the filename in lowercase and having the image with some uppercase can cause problems (best to have everything lowercase)
- Using an image over 10kb will cause your page to be really slow loading
Summary
- We have seen how to use the bgcolor= tag inside the body tag
- We have also seen how it is a good idea to use the bgcolor= when we use the background=
- We have seen how to set up top and side bordered backgrounds using tables
- For this exercise I'd like you to add a patterned background to your page (don't forget the bgcolor=)
- Now for this one I want you to make a mistake (WHAT! I hear you yell), I want you to make a typo in the backgrounds image name so it wont show up and see if your bgcolor makes the text readable. (Had you worried didn't I :). Don't forget to fix your typo when you're done.
- Extra exercise: (this is optional) Make a side or top bordered or even a triple layered background.
- This is optional to see if you know what we have learnt so far. Take the quiz
For more help visit the webmaster-forums.net

