Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Styling the Document

There are so many possibilities that it's hard to decide what to do first. Still, we have to start somewhere, so let's begin at the top of the calendar and then work on the grid of dates.

Setting the Top Apart

It's probably easiest to set the row of day names apart by reversing the content to be light blue text on a dark blue background. While we're at it, we'll get rid of the table-structure styles:

<style type="text/css">
tr#days th {color: #CCE; background-color: #224;

      font-weight: bold; text-align: center;

      padding: 1px 0.33em;}
</style>

The padding fills out the labels a little better and thus prevents them from jamming up next to each other. Now would be an excellent time to tie together the row of day names and the title immediately above it. Let's give it a light blue background, center and boldface the text, and put a border on the cell that matches the background color for the row of days. While we're at it, let's bump up the font-size just a bit.

<style type="text/css">
tr#days th {color: #CCE; background-color: #224;
   font-weight: bold; text-align: center;
   padding: 1px 0.33em;}
tr#title th {background: #AAC; color: black;

      border: 1px solid #224; font-size: 120%;}
</style>

Now we have a calendar top that all fits together (see Figure 3.4). Of course, if you don't like blue, you can always change the color values, but you might want to wait until we're done to decide that.

03css04.jpg

Figure 3.4 The calendar now has a well-styled top.

Creating the Grid

Even though the main part of the calendar, the grid of days, is part of a table, there aren't any borders between the numbers yet. We can fix that, but it isn't as simple as you might think at first. For example, we could simply give every td element a border, but the results aren't likely to be quite what we want (see Figure 3.5):

tr#title th {background: #AAC; color: black;
   border: 1px solid #224; font-size: 120%;}
td {border: 1px solid gray;}
</style>
03css05.jpg

Figure 3.5 Borders on table cells don't overlap, so many appear to be doubled in thickness.

Because each cell has four borders of its own, any place two cells adjoin each other, their borders are going to be placed right next to each other. Therefore, inside the table, the borders between cells add up to be 2 pixels thick, whereas the ones along the outside are only 1 pixel thick.

There are two ways to deal with this. One is to only style two borders—say, the right and bottom borders—and make sure the other two borders have no width. The other way is to manipulate the colors of the borders so that the borders all look roughly the same but each cell still has all four borders "turned on." We're going to choose the latter route for reasons that will become obvious later in this project.

tr#title th {background: #AAC; color: black;
   border: 1px solid #224; font-size: 120%;}
td {border: 1px solid gray;
   border-color: #BBB #EEE #EEE #BBB;}
</style>

We're working in shades of light gray to produce a subtle inset-border effect. Therefore, the top and left borders get a darker shade than the bottom and right borders. (We're assuming that the virtual light source is coming from the top left.) The problem is that the right borders on the last column will be a very light gray, making it look like the calendar grid isn't closed. To fix this, we'll set a different style for the right border of only the cells in that column (see Figure 3.6):

td border: 1px solid gray;
   border-color: #BBB #EEE #EEE #BBB;}
td.sat {border-right: 1px solid #BBB;}
</style>
03css06.jpg

Figure 3.6 Subtle color variations make the grid less obtrusive.

You can see that the cells along the bottom of the grid look sort of "unclosed" because of the light bottom borders. That's how the right side of the calendar would have looked if we hadn't added in the td.sat rule. Instead of creating a similar rule for the bottom of the calendar, though, let's move on to other issues and get back to the bottom of the calendar later.

Distinguishing the Months

Now that we have a grid, we should go about making its contents a little more interesting. First on the agenda is a way to make the non-July days look very different so that it's obvious they aren't part of the month in question. Again, there are a multitude of options. For example, we could just set them to be white text on a white background, making them disappear entirely. Instead, let's blue them out, so to speak.

Here's where all that classing and ID'ing really pays off. Because the non-July days are already part of the classes jun and aug, we can just write styles that apply to those classes (see Figure 3.7):

td.sat {border-right: 1px solid #BBB;}
td.jun, td.aug {background: #AAB; color: #889;

      border: 1px solid #AAB; border-right-color: #99A;}
</style>
03css07.jpg

Figure 3.7 "Blueing" out the other months helps keep the focus on July.

We're using shades of blue to go along with the styling of the calendar top, of course. By setting the text color and the background color close to each other in terms of brightness, the dates are washed out while still remaining relatively readable.

Now that we've styled the non-July days, we can go ahead and cap off the bottom of the grid. To give it a little more weight, let's set a border that's 2 pixels thick and whose color matches the background of the days in August. Fortunately, this is very easy to do because we already labeled the last row in the table (see Figure 3.8):

td.jun, td.aug {background: #AAB; color: #889;
   border: 1px solid #AAB; border-right-color: #99A;}
tr#lastweek td {border-bottom: 2px solid #AAB;}
</style>
03css08.jpg

Figure 3.8 Capping off the calendar with a thickened blue bottom border.

We also could have taken a simpler route and just applied a border to the bottom of the table itself, but that could result in less flexibility later on. In some circumstances, however, applying borders to the table element itself makes a whole lot of sense, so keep it in mind as a potential tool.

Distinguishing the Days

Just for a little extra visual flair, let's set a background on Saturdays and Sundays. Because the month is July and we already have blue and white, how about a nice light red background:

td.sat {border-right: 1px solid #BBB;}
td.sat, td.sun {background: #FDD;}
td.jun, td.aug {background: #AAB; color: #889;
   border: 1px solid #AAB; border-right-color: #99A;}

Notice that we put that rule before the one that washes out the June and August days. If we hadn't, June 30 and August 3 would both have had red backgrounds, thanks to the way our selectors are written.

So now July is nicely red, white, and blue (see Figure 3.9). What about the 4th, which is a national holiday in America? We ought to highlight it. Again, there are two choices. One would be to write a rule with the selector td#jul04, and that would work just fine. However, let's go for a more generic solution: We'll add a holiday class to that day:

<td class="jul thu holiday" id="jul04"><a href="jul04.html">4</a></td>
03css09.jpg

Figure 3.9 Highlighting the weekends in a patriotic fashion.

Now we can write a style that will apply to all holidays:

tr#lastweek td {border-bottom: 2px solid #AAB;}
td.holiday {background: #FAA;

      border-color: #BBB #FCC #FCC #BBB;}
</style>

In addition to the medium-red background, the border colors are set so that the highlighting blends in a little better with the background (see Figure 3.10).

03css10.jpg

Figure 3.10 Making Independence Day stand out.

Altering the Days of July

Now might be a good time to step back and look at the calendar as a whole. Are the individual days styled the way we want them? Should we do anything about the links versus normal text? Do we want to keep the underlining of the linked days? Do we want to change the padding of these table cells?

Let's start by assuming we don't want the linked days to be underlined. The rule to remove the underlines is very simple, but let's make sure it applies only to the links in the calendar:

<style type="text/css">
table#calendar a {text-decoration: none;}
tr#days th {color: #CCE; background-color: #224;
   font-weight: bold; text-align: center;
   padding: 1px 0.33em;}

Goodbye underlines. As a consequence of this decision, we have to come up with a way to make the links stand apart from the days that aren't linked.

There are many ways to handle such a situation, of course, but let's attack it from two directions. First, let's fade out the text of any unlinked days and additionally boldface any linked days. And, just for the heck of it, let's right-align all of the days (see Figure 3.11).

td {color: #777; text-align: right;
   border: 1px solid gray;
   border-color: #BBB #EEE #EEE #BBB;}
td.sat {border-right: 1px solid #BBB;}
td a {font-weight: bold;}
td.sat, td.sun {background: #FDD;}
03css11.jpg

Figure 3.11 The linked days now stand out much more clearly, and all the days have shifted to the right.

The links could stand to be even more obvious, so let's change their color. A navy blue would be nice (not to mention would fit in with the rest of the calendar), and we could set visited links to be purple. In addition, let's make the background of the links a medium yellow when the user hovers over a link.

td a {font-weight: bold;}
table#calendar a:link {color: navy;}

   table#calendar a:visited {color: purple;}

   table#calendar a:hover {background: #FF6;}
td.sat, td.sun {background: #FDD;}

Unfortunately, the hover effect will only appear behind the actual text of the link, not the entire table cell. There are a number of ways to correct this problem, but most of them use highly complicated JavaScript in an attempt to support Navigator 4.x. Let's use the much simpler CSS way of making the links block-level elements. That way, they'll fill up the whole cell and neatly solve our problem (see Figure 3.12).

td.sat {border-right: 1px solid #BBB;}
td a {font-weight: bold; display: block; margin: 0;}
td a:link {color: navy;}
03css12.jpg

Figure 3.12 The linked days now stand out much more clearly.

Making Today a Major Highlight

With everything else basically done, now we just need to highlight the current date. For testing purposes, we'll assume it's July 16. Let's go with a yellow background and a black border to really make today obvious (see Figure 3.13):

td.holiday {background: #FAA; border-color: #BBB #FCC #FCC #BBB;}
td#jul16 {background-color: yellow; border: 1px solid black;}
</style>

03css13.jpg

Figure 3.13 Highlighting the current date.

Now we see why it was a good idea to set all four borders for every cell. Had we not done so, setting the border on the current date could have thrown off the layout of the table due to our setting all four borders for the current date.

Placing the Calendar in an Existing Document

Because we've been styling a document that contains only the calendar, we've been able to get a little lazy about our selectors. Consider the following rule from our style sheet, for example:

td {color: #777; text-align: right;
   border: 1px solid gray;
   border-color: #BBB #EEE #EEE #BBB;}

This works fine in our test document, but imagine applying the rule to a page that had the calendar placed somewhere on it. We don't want all of the text in every single table cell in the whole page to be grayed out and right aligned!

There is really only one fix, but fortunately it's a simple one. We need to add the string table#calendar at the front of any selector that doesn't already contain it. Listing 3.1 shows the changes this causes in the context of the entire style sheet. It's necessary to make all of these changes because, if we don't, the differences in specificity could cause the styles to change as rules begin to override each other in unexpected ways.

Example 3.1. The Full Style Sheet, Showing Altered Selectors

<style type="text/css">
table#calendar a {text-decoration: none;}
table#calendar tr#days th {color: #CCE; background-color: #224;
   font-weight: bold; text-align: center;
   padding: 1px 0.33em;}
table#calendar tr#title th {background: #AAC; color: black;
   border: 1px solid #242; font-size: 120%;}
table#calendar td {color: #777; text-align: right;
   border: 1px solid gray;
   border-color: #BBB #EEE #EEE #BBB;}
table#calendar td.sat {border-right: 1px solid #BBB;}
table#calendar a {font-weight: bold; display: block; margin: 0;}
table#calendar a:link {color: navy;}
table#calendar a:visited {color: purple;}
table#calendar a:hover {background: #FF6;}
table#calendar td.sat, table#calendar td.sun {background: #FDD;}
table#calendar td.jun, table#calendar td.aug {
   background: #AAB; color: #889;
   border: 1px solid #AAB; border-right-color: #99A;}
table#calendar tr#lastweek td {border-bottom: 2px solid #AAB;}
table#calendar td.holiday {background: #FAA;
   border-color: #BBB #FCC #FCC #BBB;}
table#calendar td#jul16 {background-color: yellow;
   border: 1px solid black;}
</style>

Looking at the resulting rules, it might have been better to give the table an id value of cal or maybe smcal, just to keep the selectors shorter. Nevertheless, these kinds of selectors are necessary when you're styling just one portion of a document. In this case, the addition of table#calendar keeps the calendar styles confined to just that piece of the document.

  • + Share This
  • 🔖 Save To Your Account