Arctic Sunburn - Music, Guitar, & Entertainment
How to Create a HTML + CSS Calendar
Today I'm going to show you how to create a stylish HTML + CSS calendar. Not that it's a big secret or anything, but maybe it will give you some ideas if you're looking to build one of your own.

First, let's take a look at the HTML code:
<table width="770" border="1" cellspacing="0" cellpadding="8" align="center"> <tr> <td valign="top" width="770" colspan="7"> <div class="calendar-month">March 2008</div> </td> </tr> <tr> <td class="calendar-week-day" valign="top" width="110"> <div class="calendar-header">Sunday</div> </td> <td class="calendar-week-day" valign="top" width="110"> <div class="calendar-header">Monday</div> </td> <td class="calendar-week-day" valign="top" width="110"> <div class="calendar-header">Tuesday</div> </td> <td class="calendar-week-day" valign="top" width="110"> <div class="calendar-header">Wednesday</div> </td> <td class="calendar-week-day" valign="top" width="110"> <div class="calendar-header">Thursday</div> </td> <td class="calendar-week-day" valign="top" width="110"> <div class="calendar-header">Friday</div> </td> <td class="calendar-week-day" valign="top" width="110"> <div class="calendar-header">Saturday</div> </td> </tr> <tr> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> <div class="calendar-date">1</div> </td> </tr> <tr> <td valign="top" width="110"> <div class="calendar-date">2</div> </td> <td valign="top" width="110"> <div class="calendar-date">3</div> </td> <td valign="top" width="110"> <div class="calendar-date">4</div> </td> <td valign="top" width="110"> <div class="calendar-date">5</div> </td> <td valign="top" width="110"> <div class="calendar-date">6</div> </td> <td valign="top" width="110"> <div class="calendar-date">7</div> </td> <td valign="top" width="110"> <div class="calendar-date">8</div> </td> </tr> <tr> <td valign="top" width="110"> <div class="calendar-date">9</div> </td> <td valign="top" width="110"> <div class="calendar-date">10</div> </td> <td valign="top" width="110"> <div class="calendar-date">11</div> </td> <td valign="top" width="110"> <div class="calendar-date">12</div> </td> <td valign="top" width="110"> <div class="calendar-date">13</div> </td> <td valign="top" width="110"> <div class="calendar-date">14</div> </td> <td valign="top" width="110"> <div class="calendar-date">15</div> </td> </tr> <tr> <td valign="top" width="110"> <div class="calendar-date">16</div> </td> <td valign="top" width="110"> <div class="calendar-date">17</div> <div class="calendar-holiday">St. Patrick's Day</div> </td> <td valign="top" width="110"> <div class="calendar-date">18</div> </td> <td valign="top" width="110"> <div class="calendar-date">19</div> </td> <td valign="top" width="110"> <div class="calendar-date">20</div> </td> <td valign="top" width="110"> <div class="calendar-date">21</div> <div class="calendar-holiday">Good Friday</div> </td> <td valign="top" width="110"> <div class="calendar-date">22</div> </td> </tr> <tr> <td valign="top" width="110"> <div class="calendar-date">23</div> <div class="calendar-holiday">Easter</div> </td> <td valign="top" width="110"> <div class="calendar-date">24</div> <div class="calendar-holiday">Easter Monday</div> </td> <td valign="top" width="110"> <div class="calendar-date">25</div> </td> <td valign="top" width="110"> <div class="calendar-date">26</div> </td> <td valign="top" width="110"> <div class="calendar-date">27</div> </td> <td valign="top" width="110"> <div class="calendar-date">28</div> </td> <td valign="top" width="110"> <div class="calendar-date">29</div> </td> </tr> <tr> <td valign="top" width="110"> <div class="calendar-date">30</div> </td> <td valign="top" width="110"> <div class="calendar-date">31</div> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> <td valign="top" width="110"> </td> </tr> </table>
In the initial 'table' tag you can play with the settings (border, cellspacing, cellpadding, and width) for slightly different results (if you change the width, you'll want to change the width in each td cell accordingly). I think it looks best without any cellspacing.

Now let's take a look at the CSS classes. The calendar doesn't look anywhere near as nice without them.
.calendar-month { font-family: Georgia, Times, serif; font-size: 28px; font-weight: bold; font-variant: small-caps; margin 3px; } .calendar-header { font-family: Georgia, Times, serif; text-align: center; font-size: 14px; font-weight: bold; margin: 3px; } .calendar-week-day { background-color: #c0ef91; } .calendar-date { font-family: Georgia, Times, serif; font-size: 14px; float: left; margin-right: 5px; } .calendar-holiday { font-family: Georgia, Times, serif; font-size: 9px; font-style: italic; text-align: left; }
The calendar-month class sets the font properties for the header (month/year). The calendar-header class sets the font properties for each of the week days. The calendar-week-day sets the background color for each td cell containing the week days. The calendar-date class sets the font properties for dates 1 through 31. Notice how this class floats the numbers to the left, enabling any surrounding text to wrap around the number. The calendar-holiday class is used for any text that's not within the calendar-date class (i.e. dates). In this example, the aforementioned calendar-holiday class is used to display holidays.

I happen to like the font settings, but you can change the properties to your liking. The calendar-week-day class is only necessary if you want to display a background color for the week days. You can change this to any color you like.

Here's the end result:

March 2008
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
St. Patrick's Day
18
19
20
21
Good Friday
22
23
Easter
24
Easter Monday
25
26
27
28
29
30
31

Tables were originally intended for projects like this one, and frankly I don't know of a more efficient way of putting together a calendar for websites. Tables aren't largely recommended for layout purposes (which is not what this tutorial is about), but in general all browsers still support them.
This article was written by David Andrew Wiebe, web designer, graphic designer, guitarist, bassist, keyboardist, songwriter, guitar instructor, and music expert. Interested in winning a free CD? Click here for more info!
Questions? Comments? Suggestions? Recommendations? Email me.

Published on February 22, 2008
GOOGLE SEARCH
GOOGLE AD