|
|
|||||||||||||||||||
Controlling Background Images and ColoursThis tutorial shows you how to work with the various CSS background properties. Using CSS, you can specify things like:
Adding a nice coloured or graphical background to your Web pages is a quick and easy way to spice up your site. However, it's also very easy to make Web pages look horrible, or even unreadable with the wrong type of background! The section at the end of the tutorial will help you avoid some of the common pitfalls associated with backgrounds. Specifying background coloursThe background-color property lets you set the background colour for an HTML element. For example:
p { background-color: brown }
This is a paragraph with a brown background colour. Setting a background imageYou can use the background-image property to specify a background image for an HTML element. For example, to set a background image for a Web page, you could use something like this:
<html>
<head>
<style>
body
{
background-image: url("teepees.jpg")
}
</style>
</head>
<body>
<h1>Here is my Web Page with a background image!</h1>
</body>
</html>
Click Here to see this example in action! Note that you can also use the background-image property (and indeed any of the background properties) on tables, table cells, paragraphs, etc. Creating a "watermark" background imageBy default, a page background image will scroll with the page. However, using the background-attachment: fixed property, it's possible to create a "watermark" background that stays fixed in the same place on the screen while the page scrolls:
body
{
background-image: url("teepees.jpg");
background-attachment: fixed
}
To see how this looks, click here. Try scrolling up and down! Specifying how the background image tilesYou can see that the background image repeats itself many times, in order to fill the whole page. You can control this tiling effect using the background-repeat property, as follows:
Examples:
body
{
background-image: url("teepees.jpg");
background-repeat: repeat-x
}
Tiles the image across the page. Click here for a demo.
body
{
background-image: url("teepees.jpg");
background-repeat: repeat-y
}
Tiles the image down the page. Click here for a demo. that is relative to the element's content area. This does notalter the shape of the content area, but instead alters the area inwhich content may be rendered.
body
{
background-image: url("teepees.jpg");
background-repeat: no-repeat
}
Just displays the image once. Click here for a demo. Positioning the background imageCSS gives you control over exactly where your background image is placed within the HTML element. To position your image, use the background-position property as follows:
Examples:
body
{
background-image: url("teepees.jpg");
background-repeat: no-repeat;
background-position: 50% 50%
}
Displays the image in the centre of the page. Click here for a demo.
body
{
background-image: url("teepees.jpg");
background-repeat: no-repeat;
background-position: 100px 50px
}
Displays the image 100 pixels to the right of, and 50 pixels below, the top left corner of the page. Click here for a demo.
body
{
background-image: url("teepees.jpg");
background-repeat: no-repeat;
background-position: center right
}
Displays the image in the centre right of the page. Click here for a demo. Backgrounds - not just for pages!Don't forget that you can use these background properties on most HTML elements - not just the body tag. For example, here is our teepees image being used as a table background: <table style="background-image: url(teepees.jpg)"> <tr> <td><h1>Here are some teepees in a table!</h1></td> </tr> </table> And this is how it renders:
Combining propertiesYou can use the background property to specify all the background properties easily in one go. For example:
body
{
background: url("teepees.jpg") no-repeat center center black
}
This sets the page background to black, with the teepees image in the centre of the page, non-repeating. Click here for a demo. Some notes on style and readabilityImages that weren't meant to tileUnless you're using the background-repeat: no-repeat property, it is important that the image you're using is designed to tile. For example, this page looks horrible, because the background image that was used was not designed to tile! (Don't stare at it too long, or you might go cross-eyed!) Backgrounds that clash with textSome background images or colours may look very pretty, but if they make your page text hard to read, they're probably best avoided. If you want to use a page background image, choose one that is not too "busy", and that contrasts well with the text colour. Your turn!If you'd like to practice putting background images in your pages and page elements, try downloading some backgrounds from our ImageKits. Save the image to a folder on your hard drive somewhere, then link to it from your style sheet. The EndThat's the end of this tutorial. We hope you found it useful. If you're still stuck and would like further help, check out our online Help Forums, where you can get assistance from members of my and other webmasters. If you would like to offer us feedback on this or any of the tutorials, please contact us. Have fun! |
Now let's talk about top and bottom padding. In theory, aninline element with a background color and padding could have thebackground extend above and below the element. Figure 7-61 gives us some idea of what that might looklike. The line height isn't changed, of course, but sincepadding does extend the background, it should be visible, right?
Here's where the famous phrase returns: "there may be directions to fill up the entire background of the document, as shown in Figure 6-22.

This effect can be duplicated in CSS, but CSS contains a great deal more than simple tiling of background images. We'll start with the basics and then work our way up.
The default value of none means about what you'd expect: no image is placed in the background. If you wantfont-weight of each element, we would get the results in Figure 5-13:
<P> 100 <SPAN> 400 <STRONG> 700 <B> 800 </B></STRONG></SPAN>. </P>
The first two weight increases are large because they represent jumps from 100 to 400, and from 400 to boldXML allows you to easily generate XML documents (that contain your information), since it is so structured.
XML documents may be stored in files or databases. When stored in files, XML documents are simply plain text files with tags (and possibly DTDs). It is very easy to save your XML documents to a text file and pass the text file around to other machines, platforms and programs (as long as they can understand the data). In the worst case scenario, XML documents (files) can be viewed in a text editor on just about any platform.
something I said earlier. background-position is the only background property with restrictions, as it may only be applied to block-level and replaced elements; background image positioning cannot be done on inline elements such as hyperlinks.