Organizing Styles and Style Sheets

You've learned a lot in this book about creating styles and style sheets. But when you're designing a site that's meant to last, you can incorporate a few other steps to help you out in the future. The day will come when you need to change the look of the site, tweak a particular style, or hand off your hard work to someone else who'll be in charge. In addition to leaving notes for yourself and others, a little planning and organization within your CSS will make things go more smoothly down the road.

You've already learned the technical aspects of naming different types of selectors—class selectors begin with a. (period) to identify the styles as a class, and ID styles begin with the # symbol. In addition, the names you give IDs and classes must begin with a letter and can't contain symbols like &, *, or !. But beyond those requirements, following some rules of thumb can help you keep track of your styles and work more efficiently:

Often, two or more items on a web page share many similar formatting properties. You may want to use the same border styles to create a frame around a bunch of images on a page. But there may be some formatting differences between those items as well. Maybe you want some images to float to the left and have a right margin, while some photos float to the right and have a left margin (Figure 15-2).

The most obvious solution is to create two class styles, each having the same border properties but different float and margin properties. You then apply one class to the images that should float left and another to the images that should float right. But what if you need to update the border style for all of these images? You'll need to edit two styles, and if you forget one, the images on one side of the page will all have the wrong frames!

There's a trick that works in all browsers that not all designers take advantage of—multiple classes applied to the same tag. This just means that when you use the class attribute for a tag, you add two (or more) class names like this: <div class="note alert">. In this example, the <div> tag receives formatting instructions from both the .note style and the .alert style.

Say you want to use the same border style for a group of images, but some of the images you want floating left and others you want floating right. You'd approach the problem like this:

This technique is useful when you need to tweak only a couple of properties of one element, while leaving other similarly formatted items unchanged. You may want a generic sidebar design that floats a sidebar to the right, adds creative background images, and includes carefully styled typography. You can use this style throughout your site, but the width of that sidebar varies in several instances. Perhaps it's 300 pixels wide on some pages and 200 pixels wide on others. In this case, create a single class style (like .sidebar) with the basic sidebar formatting and separate classes for defining just the width of the sidebar—for example, .w300 and .w200. Then apply two classes to each sidebar: <div class="sidebar w300">.

Adding one style after another is a common way to build a style sheet. But after a while, what was once a simple collection of five styles has ballooned into a massive 150-style CSS file. At that point, quickly finding the one style you need to change is like looking for a needle in a haystack. (Of course, haystacks don't have a Find command, but you get the point.) If you organize your styles from the get-go, you'll make your life a lot easier in the long run. There are no hard and fast rules for how to group styles together, but here are two common methods:

As you read in Chapter 14, you can create different style sheets for different types of displays—maybe one for a screen and another for a printer. But you may also want to have multiple onscreen style sheets, purely for organizational purposes. This takes the basic concept from the previous section—grouping related styles—one step further. When a style sheet becomes so big that it's difficult to find and edit styles, it may be time to create separate style sheets that each serve an individual function. You can put styles used to format forms in one style sheet, styles used for layout in another, styles that determine the color of things in a third, another style sheet for keeping your Internet Explorer hacks, and so on. Keep the number of separate files reasonable since having, say, 30 external CSS files to weed through may not save time at all.

At first glance, it may seem like you'll end up with more code in each web page, since you'll have that many more external style sheets to link to or import—one line of code for each file. Ah, but there's a better approach: Create a single external style sheet that uses the @import directive to include multiple style sheets. Figure 15-3 illustrates the concept.

Here's how to set up this type of arrangement:

  1. Create external style sheets to format the different types of elements of your site.

    For example a color.css file with styles that control the color of the site, a forms.css file that controls form formatting, a layout.css file for layout control, and a main.css file that covers everything else (see the right side of Figure 15-3).

  2. Create an external style sheet and import each of the style sheets you created in step 1.

    You can name this file base.css, global.css, site.css, or something generic like that. This CSS file won't contain any rules. Instead use the @import directive to attach the other style sheets like this:

    @import url(main.css);
    @import url(layout.css);
    @import url(color.css);
    @import url(forms.css);

    That's the only code that needs to be in the file, though you may add some comments with a version number, site name, and so on to help identify this file.

  3. Finally, attach the style sheet from step 2 to the HTML pages of your site using either the <link> tag or the @import method. (See Linking a Style Sheet Using HTML for more on using these methods.) For example:

    <link rel="stylesheet" href="base.css" type="text/css" />

    Now, when a web page loads, the browser loads base.css, which in turn tells the browser to load the four other style sheets.

It may feel like there's a whole lot of loading going on here, but once the browser has downloaded those files and stored them in its cache, it won't have to retrieve them over the Internet again. (See the box on the next page.)

There's another benefit to using a single external style sheet to load several other style sheets: If you decide later to further divide your styles into additional styles sheets, then you won't have to muck around with the HTML of your site. Instead, just add one more @import directive to that gatekeeper style sheet (see step 2). If you decide to take all the styles related to type out of the main.css file and put them in their own type.css file, then you won't need to touch the web pages on your site. Simply open the style sheet with all of the @import directives in it and add one more: @import url(type.css).

This arrangement also lets you have some fun with your site by swapping in different style sheets for temporary design changes. Say you decide to change the color of your site for the day, month, or season. If you've already put the main color-defining styles into a separate color.css file, then you can create another file (like summer_fun.css) with a different set of colors. Then, in the gatekeeper file, change the @import directive for the color.css file to load the new color style file (for example, @import url(summer_fun.css)).