Expanding Efficiency with Cascading Style Sheets - dummies

Expanding Efficiency with Cascading Style Sheets

By Richard Mansfield

Defining a style in one location as CSS does has several advantages. First, it eliminates redundancy: You don’t have to keep specifying its font size and color each time you use the <h1> tag in your document, for example. That makes Web page code easier to read and to modify later. If you’re familiar with computer programming, think of a simple CSS style rule as something like a programming language constant: You specify, for example, the local tax rate by making up a name such as LocalTax, and then assigning a value to it like this: Constant LocalTax = .07. Thereafter, throughout your program, you don’t need to repeatedly specify the .07. You merely use the constant’s name LocalTax.

Similarly, after you’ve defined a CSS headline style, you can thereafter merely use the class name for that style, no matter how lengthy and complex that style might be. In this example, you use no class name, so every H1 headline is rendered with this style:

h1 { font-size:16pt color:blue;}


A second advantage of gathering all style definitions into a single location is that you can more easily make global changes. What if you decided to change all the H1 headlines to red instead of blue? If you didn’t use a style sheet, you would have to search for all H1 elements throughout the entire Web site’s HTML files and modify each of those elements in turn.

But if you had the foresight to use a style sheet, you need only change the single definition of the style for H1 in the style sheet itself. The specs are automatically applied throughout the HTML. In other words, just make this change from blue to red in the style sheet:

H1 { font-size:16pt color:red;}

All the headlines between the <h1> and </h1> tags throughout the entire Web site are now displayed as red text.

Changing Web design for the better

HTML originally was designed to work something like an outline, specifying the structure of a document, without too much attention paid to the actual visual style, or design, of the document. An outline merely organizes ideas hierarchically: A, B, C, and so on are the major ideas. Within those categories, you have subdivisions such as 1, 2, 3, 4 and even lower divisions such as a, b, c, d and so on. The equivalent outline structure in HTML is described with various headline levels such as H1, H2, H3, and so on.

HTML was supposed to simply define content: This is body text, this is a headline, this is a table, and so on. But Web designers naturally wanted to offer ever more compelling, visuallyattractive Web pages. After all, the Internet more often competes with lively television ads than with dry, highly structured, academic journals. HTML began to grow willy-nilly by adding many special formatting elements and attributes such as italics and color. This inflation of tags made creating, reading, and modifying HTML increasingly cumbersome. Separating the content (structure) from the page’s design and layout became necessary. Enter CSS. When you use CSS, the HTML is left to primarily handle the structure and the CSS file contains the styles defining how the HTML elements look.

Also, CSS also offers the Web page designer features unavailable in plain HTML. CSS gives a designer much greater control over the appearance of a Web page.

Being ready for anything

Of course, you’ll never have absolute control over Web pages if you create sites for the Internet. There will never be a truly stable, single, predictable display for Web pages. Why? Because, like some celebrities, a Web page never knows where it’s going to end up from minute to minute. It has to be prepared to be on display in all kinds of situations.

A Web page might be shown on a Pocket PC PDA screen — with very few pixels and in black and white. Or it might be shown on the huge Diamond Vision display in Hong Kong, which is longer than a Boeing 747, or even the Jumbotron screen in Toronto’s Skydome, which measures 110 feet wide by 33 feet tall.

Not only do you have to consider huge differences in size, but also in aspect ratio (shape). Many computer monitors are still the traditional square shape, but increasingly Internet users are switching to widescreen monitors — wider than they are high, like a movie screen — to better display HDTV and DVDs. For Internet users, widescreen just means you see more horizontal information per page. Web pages designed with absolute (unchanging) positioning leave several inches of empty white space along the right side of a widescreen monitor. What would Vincent do?

How would Van Gogh have dealt with the problem of designing a picture of a vase of sunflowers that might be shown on a widescreen Jumbotron, but also on a little square monitor?

The basic solution to this problem is to specify size and position in relative rather than absolute terms. For example, instead of saying, “The sunflower is 2 inches high and is located 12 inches from the left side,” (an absolute specification), you say, “The sunflower is 6 percent large and 35 percent from the left side” (a relative specification). Other ways of specifying sizes relatively include pixels (which are the smallest units of information that a given monitor can display, so they vary from monitor to monitor) or such general terms as x-large or large.

Alert readers might be asking at this point, “Six percent of what?” The percentage is calculated based on the containing block. It can be the browser window (<body>), but it can also be such blocks as a <div> within the <body>. In this example, the containing block is the total size of the browser, but you can also specify percentage for other, smaller, containers within the browser window.

Relative specs translate well into various sizes of displays. A sunflower 6 percent large would be displayed with about 48 pixels on an 800×600 computer monitor, but displayed 18 feet wide on a Jumbotron that’s 300 feet wide.

In other words — when you specify relative measurements or positions — your graphics or text are automatically scaled as necessary to fit whatever size display is being used at the time.

Of course, if you’re building pages for an intranet site, you might well know that everyone in your office network is required to use the same size screen, the same browser, the same operating system, and allowed no family photos in their cubicle. In those situations where uniformity is enforced across the entire company, you can provide absolute specifications, but such situations are relatively rare.