5. Background colors and images using CSSWe have a nice, pristine style sheet, itching for some rules. Now what? One of the first things we do when we set up the appearance of a web page, is to set the background. For whatever reason, the default color of a web page background is a ghastly gray (your opinion as to its aesthetic qualities may vary). Our First RuleA style sheet is simply a set of rules, which determine which elements are to be affected by the rule, and define how any affected elements should appear. Creating the SelectorIn this case, we want to affect the appearance of the For simple elements, that is, any HTML elements, such as So, the selector for pop quiz 1What is the selector for the Creating the PropertiesNow, once we've selected the body using In the case of background color, the property is called background-color. Let's give the background a nice aqua color, say
Save the style sheet, then open your web page (the one we created last lesson) in a browser. Behold a colored background? If not, check the following: Do you have a version 4 browser? Completely baffled? Remember, there's no shame and little expense in using a CSS Editor. Background ImagesLet's get a little fancier, adding a background image. We are also going to see the first example of how style sheets give you more than plain HTML when it comes to style. First we'll specify a background image, then look at these additional features. Our Additional properties for a rule also appear inside the curly braces. Each property is separated by a semicolon. URLs in style sheets are specified in a particular way, using a syntax of the form, So the background image property is specified by
To include this in the rule we created above, we place it inside the braces, and separate the two properties with a semicolon ";".
We can add whitespace, like tabs, spaces or returns for legibility, because style sheets ignore any non essential whitespace. Before we go on, let me note that there is serious bug in Navigator 4, which means that relative URLs are treated as relative to the page (HTML file) not the style sheet. For the background image to appear in both Navigator 4 and Explorer 4, the HTML file and the style sheet file must be in the same folder, at the same level. Save your style sheet, then preview your page in a browser. We've now created a rule that gives the body of a page a background color, and a background image. Let's take a look at these new style sheets features I alluded to above. Beyond TilingWith HTML, background images tile, that is, repeat vertically and horizontally down and across a page. With style sheets, you can specify whether a background image appears once, or tiles - either like traditional background images, or only horizontally or vertically. The image can also appear in a particular position on the page (say centered, or fixed in the bottom left of the page). Tiling can also begin at a specified location, not simply in the top left hand corner.
Exercise 1Now, try creating additional properties that specify how the background image of our body will repeat, and be positioned. You'll find my example answer at the end of the lesson. Special Note Save your style sheet, and preview your handy work in a browser. What have we learnt this hour?The most important thing we learnt was how to make rules. A rule comprises a selector (in this case we learnt about HTML element selectors, soon we will learn about others), and a set of properties. NextNext we will learn about text, setting a default style for the page, as well as setting up headings. Answer to Exercise 1Here is my go, where I place the background image in the center of the page, and don't repeat it. This gives a nice watermarking effect.
(C)1997-2001 Western Civilisation Pty. Ltd.
|